title: how to maintain transparency of png image downloaded from url using c#
body: <p>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 :</p>
  
  <pre><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;
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>i tried this <strong>_HttpWebRequest.Accept = "image/png,image/*";</strong> so as it should accept png image and maintain aspect ratio but its doesn't seem to be working for me.</p>
  
  <p>Any help is deeply appreciated. </p>
  
  <p>Thanking you,
  Santosh Upadhayay.</p>