简体   繁体   English

在C#中设置下载图像的调色板

[英]Setting color palette of downloaded image in C#

I'm trying to crawl a large number of images from my old website because it's soon going to be shut down. 我正在尝试从旧网站中抓取大量图片,因为它将很快被关闭。 All of the images are in JPEG format, all of them are actually downloaded from the website, but some of them are shown in a green-pink accent on my local (Windows) computer. 所有图像均为JPEG格式,实际上都是从网站下载的,但是其中一些以绿色粉红色字体显示在我的本地(Windows)计算机上。

I found out none of the corrupted pictures have color-space metadata and embedded color profile, but I'm not sure this is the problem, since I'm not familiar with image processing. 我发现损坏的图片都没有色彩空间元数据和嵌入的色彩配置文件,但是由于我不熟悉图像处理,因此我不确定这是问题所在。 I couldn't find any setting in C# to set the color profile to RGB or something similar. 我在C#中找不到任何将颜色配置文件设置为RGB或类似设置的设置。 This is my code: 这是我的代码:

private static Image GetImageFromUrl(string url)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
            {
                using (Stream stream = httpWebReponse.GetResponseStream())
                {
                    return Image.FromStream(stream);
                }
            }
        }
        catch (WebException e)
        {
            return null;
        }
    }

    private static void SaveImage(string folderName, string fileName, Image img)
    {
        if (img == null || folderName == null || folderName.Length == 0)
        {
            return;
        }
        string path = "D:\\Files\\" + folderName;
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        using (img)
        {
            img.Save("D:\\Files\\" + folderName + "\\" + fileName, ImageFormat.Jpeg);
        }
    }

SaveImage(folderName, fileName, GetImageFromUrl(resultUrl));

This is how the pictures look like in the browser (left) and when downloading with this program (right): 这是图片在浏览器中(左)以及使用该程序下载时(右)的样子:

图片

Thank you for your help. 谢谢您的帮助。

OK it seems this problem can be bypassed by saving the file stream directly without converting it to an Image object on the local machine. 好的,似乎可以通过直接保存文件流而不将其转换为本地计算机上的Image对象来绕过此问题。 The corrupt color palette stays, but somehow the Windows Picture Viewer is able to display the saved images correctly (no strange color accents) and so does Photoshop etc. 损坏的调色板会保留下来,但是Windows Picture Viewer能够以某种方式正确显示保存的图像(没有奇怪的颜色突显),Photoshop也是如此。

As suggested by Jason Watkins in comments, my code looks like this now: 正如Jason Watkins在评论中建议的那样,我的代码现在看起来像这样:

private static void SaveImageFromUrl(string folderName, string fileName, string url)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
            {
                using (Stream stream = httpWebReponse.GetResponseStream())
                {
                    //need to call this method here, since the image stream is not disposed
                    SaveImage(folderName, fileName, stream);
                }
            }
        }
        catch (WebException e)
        {
            Console.WriteLine("Image with URL " + url + " not found." + e.Message);
        }

    }

    private static void SaveImage(string folderName, string fileName, Stream img)
    {
        if (img == null || folderName == null || folderName.Length == 0)
        {
            return;
        }
        string path = "D:\\Files\\" + folderName;
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        using (var fileStream = File.Create("D:\\Files\\" + folderName + "\\" + fileName))
        {
            img.CopyTo(fileStream);
            //close the stream from the calling method
            img.Close();
        }
    }

SaveImageFromUrl(folderName, fileName, resultUrl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM