A Generic error occured GDI+ and Create High Quality Thumbnail

Wuiihh… what a pain in the ass. I’ve got a scenario to generate 3 different size of thumbnail on single upload

Microsoft is so lazy to named the error on GDI. so if something goes wrong it’s ‘A Generic error occurred in GDI+’ message.

After struggling on this. which can be caused by anything… from Folder permission to Not disposed correctly.

I’ve tried everything and in the final end, it’s because the naming of the file.. Weird… so i changed from xxx_AvBig.jpg to just xxxavbig.jpg . It runs correctly .

What a Not scientific Reason..

And then another problem is i Use this Image.CreateThumbnail , which will not Create a nice high quality thumbnail , it’s crappy..

The solution comes from this Guy

So to conclude the pattern would be:

   1:   using (Bitmap originalImage = new Bitmap(myFile.InputStream))
   2:  {
   3:  //ex:
   4:                  int width = 250;
   5:                  double ratio = Convert.ToDouble(250) / Convert.ToDouble(originalImage.Width);
   6:                  int height = Convert.ToInt32(ratio * originalImage.Height);
   7:  // superior image quality
   8:                  using (Bitmap bmpResized = new Bitmap(width, height))
   9:                  {
  10:                      string thumbnailFileName = fileUserBig;
  11:                      using (Graphics g = Graphics.FromImage(bmpResized))
  12:                      {
  13:                          g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  14:                          g.DrawImage(originalImage,
  15:                                      new Rectangle(Point.Empty, bmpResized.Size),
  16:                                      new Rectangle(Point.Empty, originalImage.Size),
  17:                                      GraphicsUnit.Pixel);
  18:                      }
  19:                      bmpResized.Save(thumbnailFileName);
  21:                  }             
  22:  //do another thumbnail here like above …
  23:  }


Make sure that the height or width new size is not Small enough or Height is larger than width. i used fixed height and width for the small one. or it will throw that ‘gdi’ again


 I've got that Throw Exception with 'generic error again' this time i change all the Image.FromFile and Bitmap x=New Bitmap(filepath) To

using ( FileStream fs = new FileStream( filename, FileMode.Open ) )
bmp = (Bitmap)Image.FromStream( fs );

 Hmm also add the GC.waitforpendingfinalizer(); and GC.collect(); on the spot where it throws that GDI

Because The File Handle Sometimes Take A Time to Released or Not disposed Properly.

Share this post: | | | |
Published Sunday, November 8, 2009 12:17 PM by cipto
Filed under:


No Comments