如何在硒 c# 中将 Blob 图像转换为位图图像

Posted

技术标签:

【中文标题】如何在硒 c# 中将 Blob 图像转换为位图图像【英文标题】:How do I convert Blob Image to Bitmap Image in selenium c# 【发布时间】:2021-10-10 11:49:57 【问题描述】:

我想将 Blob 图像转换为 base64 - 但我不确定如何实现这一目标

html 元素

<img src="blob:https://sample.cloud.co.uk/b636a5fd-b3ee-45de-9314-1f20b006d3b4"  style="max-width: 600px; position: relative; top: 0px; left: 0px; max-height: 600px;">

有人可以帮我获取位图或base64图像

【问题讨论】:

【参考方案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) ; 
       
    


【讨论】:

以上是关于如何在硒 c# 中将 Blob 图像转换为位图图像的主要内容,如果未能解决你的问题,请参考以下文章

从json中的mysql获取Blob图像并转换为位图

如何在xamarin android中将位图图像转换为gif?

如何在 EmguCV 中将图像转换为矩阵,然后将矩阵转换为位图?

如何在颤动中将 Blob 转换为图像

在 .NET 2.0 中将位图转换为一个多页 TIFF 图像

在android中将图像作为BLOB上传到SQL