简体   繁体   中英

how to maintain transparency of png image downloaded from url using c#

We are working on the C# application where we download images from our server. As of we are working fine for the jpeg images but the png images with transparency get added with white patch in place of the transparent part. I tried below code :

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;
    }

The image that i get when we download it from the URL it comes with white patch. I guess its adding white patch in place of transparent part but how can i stop it to do so. Is there any way that it should directly detect the and download image in proper format without playing with images.

i tried this _HttpWebRequest.Accept = "image/png,image/*"; so as it should accept png image and maintain aspect ratio but its doesn't seem to be working for me.

Any help is deeply appreciated.

Thanking you, Santosh Upadhayay.

What are you doing with the images? If you are saving them to files or something you don't want to convert them to Image objects, read the raw bytes from the stream and save them to a file using a FileStream or File.WriteAllBytes.

The easiest way to download an image, or any other file from a C# program is to use the WebClient class' DownloadFile method. In the code below we create a file name and path for the image on the local machine, then create an instance of the WebClient class, then we call the DownloadFile method passing the URL to the image, and the file name and path.

string fileName = string.Format("{0}{1}", tempDirectory, @"\strip.png");
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(imageUrl, fileName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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