【问题标题】:How do I convert Blob Image to Bitmap Image in selenium c#如何在硒 c# 中将 Blob 图像转换为位图图像
【发布时间】:2021-10-10 11:49:57
【问题描述】:
我想将 Blob 图像转换为 base64 - 但我不确定如何实现这一目标
HTML 元素
<img src="blob:https://sample.cloud.co.uk/b636a5fd-b3ee-45de-9314-1f20b006d3b4" alt="" style="max-width: 600px; position: relative; top: 0px; left: 0px; max-height: 600px;">
有人可以帮我获取位图或base64图像
【问题讨论】:
标签:
c#
selenium-webdriver
blob
【解决方案1】:
首先,尝试像这样读取img标签的src属性:
var pictureNode = webDriver.FindElement(By.CssSelector("FOO_SELECTOR"));
var pictureURI = pictureNode.GetAttribute("src");
然后将图像下载为字节数组并保存为 BMP 或 JPEG 格式:
using (WebClient webClient = new WebClient())
{
byte [] data = webClient.DownloadData(pictureURI);
using (MemoryStream mem = new MemoryStream(data))
{
using (var yourImage = Image.FromStream(mem))
{
// If you want it as Png
yourImage.Save("path_to_your_file.png", ImageFormat.Png) ;
// If you want it as Jpeg
yourImage.Save("path_to_your_file.jpg", ImageFormat.Jpeg) ;
}
}
}