GDI+ generic error


M

Martin Wirth

hi guys,

I have a problem with saving a System.Drawing.Bitmap bmp to disk.
This bitmap contains a thumbnail of another bitmap I previously loaded.
Whenever I try to issue "bmp.Save(....., jpeg format)" I get the GDI +
generic error.

I've seen a knowledge base article (814675) describing this issue. However I
cannot resolve it using the techniques presented in the article.

here's some code I use:
try
{
System.Drawing.Bitmap im = new System.Drawing.Bitmap(orig);
int width, height;
width = 150; height = 150;
System.Drawing.Image th = new Bitmap(width, height);
Graphics g = Graphics.FromImage(th);
g.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
g.DrawImage(im, 0, 0, width, height);
g.Dispose();
im.Dispose();
// workaround for "save to same stream" problem.
// KB article 814675
// "PRB: Bitmap and Image Constructor Dependencies"
System.Drawing.Bitmap th2 = new Bitmap(th.Width, th.Height);
Graphics g2 = Graphics.FromImage(th2);
g2.DrawImage(th, 0, 0, th.Width, th.Height);
g2.Dispose();
th.Dispose();
// end of workaround
th2.Save(thumb, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e){MessageBox.Show(this, e.Message, "Exception caught",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

does anyone know what's the problem here ? I 've tried lots of stuff but i
just can't get "thumbnailing" to work ...

Best regards,

martin wirth.
 
Ad

Advertisements

P

Pete Davis

I don't know if this helps, but I ran into this error recently. I was
loading a JPEG from a file and then trying to save it to the database.

I finally discovered that there was something corrupt in the jpeg itself,
despite the fact that it looked fine (another program detected the
corruption but displayed the jpeg just fine as well).

What I did to solve the problem was to convert the jpeg to a bmp. Then I
converted the bmp back into a jpeg and then saved it. Convoluted, but it
took care of the problem.

The code I used (which is very simple) to convert to a bitmap was something
like this:

Bitmap destination = new Bitmap(xSize, ySize);
Graphics gfx = Graphics.FromImage((System.Drawing.Image) destination);
gfx.DrawImage(img, 0, 0, xSize, ySize);
return System.Drawing.Image.FromHbitmap(destination.GetHbitmap());

I then saved the Image as a jpeg.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top