Thursday, May 27, 2010

Generic GDI+ error when trying to save a bitmap

I was trying to read an image from a URL using webclient, but it kept erroring out with a GDI+ error when I was trying to save it. The image in question is Tiff, but the same code worked when it was JPG - strange!!

But anyway I was able to get around this issue after stumbling across the following MSDN article: http://support.microsoft.com/?id=814675

using (Stream ms = new MemoryStream(client.DownloadData("http://image.com")))
{
  Image img = Image.FromStream(ms);
  Bitmap bmp = img as Bitmap;
  Graphics g = Graphics.FromImage(bmp);
  Bitmap bmpNew = new Bitmap(bmp);
  g.DrawImage(bmpNew, new Point(0, 0));
  g.Dispose();
  bmp.Dispose();
  img.Dispose();

  bmpNew.Save("C:\\image.tiff");
}

No comments:

Post a Comment