从页面复制图像会产生黑色图像

从页面复制图像会产生黑色图像

本文介绍了从页面复制图像会产生黑色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序从一个网页复制图像并保存在本地。在某些网页上,保存的图像是一个完全黑色的屏幕。首先我认为这是一个问题的代码,没有采取好的图片。所以我开始调查。我手动去这些页面,并试图复制图像(右键单击,复制图像),它仍然返回一个黑色图像。有人可以告诉我如何从代码绕过这个?

  IHTMLDocument2 doc =(IHTMLDocument2)webBrowser1.Document.DomDocument; 
IHTMLControlRange imgRange =(IHTMLControlRange)((HTMLBody)doc.body).createControlRange();

foreach(IHTMLImgElement img in doc.images)
{
if(img.alt!=my image alt)
continue;
imgRange.add((IHTMLControlElement)img);

imgRange.execCommand(Copy,false,null);

使用(Bitmap bmp =(Bitmap)Clipboard.GetDataObject()。GetData(DataFormats.Bitmap))
{
if(bmp!= null)
{
bmp.Save(testimg.jpg);
}
}
}


解决方案>

该图像具有透明背景。

因此,图像中的每个像素都是黑色的,除了它们中的大多数是完全透明的。



由于 .jpg 文件不支持透明度,因此将其保存为 .jpg 会生成黑色图像。 / p>

如果将其保存为 .png 文件(它支持透明度),它应该可以工作。


I have a program that copies images from a webpage and saves them locally. On certain webpages the saved image is a completely black screen. First I thought it was a problem in the code that didn't take the good picture. So I started investigating. I went manually to those pages and tried to copy the image(right click, copy image) and it still returned a black image. Can someone tell me how can I bypass this from code? Here is the current code, which works fine for most of the pictures

IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.DomDocument;
        IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();

        foreach (IHTMLImgElement img in doc.images)
        {
            if (img.alt != "my image alt")
                continue;
            imgRange.add((IHTMLControlElement)img);

            imgRange.execCommand("Copy", false, null);

            using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
            {
                if (bmp != null)
                {
                    bmp.Save("testimg.jpg");
                }
            }
        }
解决方案

That image has a transparent background.
Therefore, every pixel in the image is black, except that most of them are fully transparent.

Since .jpg files do not support transparency, saving it as a .jpg results in a black image.

If you save it as a .png file (which does support transparency), it should work.

这篇关于从页面复制图像会产生黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:53