C# 如何使用c维护从url下载的png图像的透明度#
我们正在开发C#应用程序,从服务器下载图像。 到目前为止,我们正在为jpeg图像进行良好的处理,但是带有透明度的png图像添加了白色补丁,以代替透明部分。我尝试了以下代码:C# 如何使用c维护从url下载的png图像的透明度#,c#,image,visual-studio-2010,image-processing,httpwebrequest,C#,Image,Visual Studio 2010,Image Processing,Httpwebrequest,我们正在开发C#应用程序,从服务器下载图像。 到目前为止,我们正在为jpeg图像进行良好的处理,但是带有透明度的png图像添加了白色补丁,以代替透明部分。我尝试了以下代码: public Image DownloadImage(string _URL) { Image _tmpImage = null; try { // Open a connection System.Net.HttpWe
public Image DownloadImage(string _URL)
{
Image _tmpImage = null;
try
{
// Open a connection
System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);
_HttpWebRequest.AllowWriteStreamBuffering = true;
// You can also specify additional header values like the user agent or the referer: (Optional)
_HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
_HttpWebRequest.Referer = "http://www.google.com/";
// set timeout for 20 seconds (Optional)
_HttpWebRequest.Timeout = 40000;
_HttpWebRequest.Accept = "image/png,image/*";
// Request response:
System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();
// Open data stream:
System.IO.Stream _WebStream = _WebResponse.GetResponseStream();
// convert webstream to image
_tmpImage = Image.FromStream(_WebStream);
// Cleanup
_WebResponse.Close();
_WebResponse.Close();
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
return null;
}
return _tmpImage;
}
当我们从URL下载它时,我得到的图像带有白色补丁。我猜它添加了白色补丁来代替透明部分,但我怎么能阻止它这么做呢。是否有任何方式,它应该直接检测和下载图像在适当的格式,而不玩图像
我尝试了这个\u HttpWebRequest.Accept=“image/png,image/*”所以它应该接受png图像并保持纵横比,但它似乎不适合我
非常感谢您的帮助
谢谢你,
Santosh Upadhayay.你在用这些图像做什么?如果要将其保存到文件或不想将其转换为图像对象的其他文件中,请从流中读取原始字节,然后使用FileStream或file.WRITEALBYTES将其保存到文件中。您对图像做了什么?如果要将它们保存到文件或其他不想将其转换为图像对象的文件中,请从流中读取原始字节,然后使用FileStream或file.WriteAllBytes将它们保存到文件中。从C程序下载图像或任何其他文件的最简单方法是使用WebClient类的DownloadFile方法。在下面的代码中,我们在本地计算机上为映像创建文件名和路径,然后创建WebClient类的实例,然后调用DownloadFile方法,将URL传递给映像,以及文件名和路径
string fileName = string.Format("{0}{1}", tempDirectory, @"\strip.png");
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(imageUrl, fileName);
从C程序下载图像或任何其他文件的最简单方法是使用WebClient类的DownloadFile方法。在下面的代码中,我们在本地计算机上为映像创建文件名和路径,然后创建WebClient类的实例,然后调用DownloadFile方法,将URL传递给映像,以及文件名和路径
string fileName = string.Format("{0}{1}", tempDirectory, @"\strip.png");
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(imageUrl, fileName);
当您在Windows中查看文件时,或者当您将图像分配给
PictureBox
时,它是否显示白色补丁?此外,请尝试在图像
对象中设置PixelFormat
,您要将流写入其中,请尝试其中一些,您将需要一个使Alpha可用的对象,在文件浏览器和我附加的地方。它只能用白色补丁下载Sir,我尝试设置pixelformat,它返回System.Drawing.Imaging.pixelformat.Format32bppArgb。当您在Windows中查看文件时,或者当您将图像分配给PictureBox
时,它是否显示白色补丁,尝试在要将流写入的图像
对象中设置PixelFormat
,尝试其中一些,您将需要一个使Alpha可用的对象。对于科林爵士来说,在文件浏览器中以及在我附加的位置。下载时只需使用白色补丁。我尝试设置pixelformat,结果返回System.Drawing.Imaging.pixelformat.Format32bppArgb。