在 C# 中设置下载图像的调色板
Setting color palette of downloaded image in C#
我正在尝试从我的旧网站上抓取大量图片,因为它很快就会被关闭。所有图片均为 JPEG 格式,所有图片实际上都是从网站上下载的,但其中一些在我本地 (Windows) 计算机上显示为绿色和粉红色。
我发现 none 损坏的图片有颜色-space 元数据和嵌入的颜色配置文件,但我不确定这是问题所在,因为我不熟悉图像加工。我在 C# 中找不到任何设置来将颜色配置文件设置为 RGB 或类似的东西。
这是我的代码:
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));
这是图片在浏览器中(左)和使用此程序下载时(右)的样子:
感谢您的帮助。
好的,似乎可以通过直接保存文件流而不将其转换为本地计算机上的 Image
对象来绕过这个问题。损坏的调色板仍然存在,但不知何故 Windows 图片查看器能够正确显示保存的图像(没有奇怪的颜色强调),Photoshop 等也是如此。
正如 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);
我正在尝试从我的旧网站上抓取大量图片,因为它很快就会被关闭。所有图片均为 JPEG 格式,所有图片实际上都是从网站上下载的,但其中一些在我本地 (Windows) 计算机上显示为绿色和粉红色。
我发现 none 损坏的图片有颜色-space 元数据和嵌入的颜色配置文件,但我不确定这是问题所在,因为我不熟悉图像加工。我在 C# 中找不到任何设置来将颜色配置文件设置为 RGB 或类似的东西。 这是我的代码:
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));
这是图片在浏览器中(左)和使用此程序下载时(右)的样子:
感谢您的帮助。
好的,似乎可以通过直接保存文件流而不将其转换为本地计算机上的 Image
对象来绕过这个问题。损坏的调色板仍然存在,但不知何故 Windows 图片查看器能够正确显示保存的图像(没有奇怪的颜色强调),Photoshop 等也是如此。
正如 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);