• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

A generic error occured in GDI+ -- NOT permissions related!

Hi - This problem is making me batty.

I have the following method that takes the inputStream of a file upload box, and creates a thumbnail to save.

When the thumbnail.save is called I get the generic GDI+ error. I know it is not permissions based, because I can save the unaltered image to the directory without issue.

Please help, I am completely at a loss of what is going on. I'm not closing the stream anywhere, and it's not permissions, I'm not trying to save a .png, so I don't think it is a stream seek issue.

Thanks,

b

            //Stream spFilePath = upload.postedFile.Inputstream.
            //sFileType = Extenstion of the file being uploaded.
            //iHeight = height for  the Compressed Image
            //iWidth = Width for  the Compressed Image

            public void CreateThumbNail(Stream sPFilePath,string sFileType,int iHeight,int iWidth) {

                  EncoderParameters encodeParams = new EncoderParameters();
                  long[] quality = new long[1];
                  quality[0] = 80;
                  EncoderParameter encoderParam = new EncoderParameter(Encoder.Quality,quality);
                  encodeParams.Param[0] = encoderParam;
                  ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();

                  ImageCodecInfo jpegICI     = null;
                  ImageCodecInfo gifICI     = null;
                  ImageCodecInfo bmpICI     = null;
                  ImageCodecInfo pngICI     = null;

                  for (int x = 0; x < arrayICI.Length; x++) {
                        if (arrayICI[x].FormatDescription.Equals("JPEG") )
                              jpegICI = arrayICI[x];
                        else if(arrayICI[x].FormatDescription.Equals("GIF") )
                              gifICI = arrayICI[x];
                        else if(arrayICI[x].FormatDescription.Equals("BMP") )
                              bmpICI = arrayICI[x];
                        else if(arrayICI[x].FormatDescription.Equals("PNG") )
                              pngICI = arrayICI[x];
                  }
     

                  //Get the image from the  stream.
                  System.Drawing.Bitmap image = (Bitmap)System.Drawing.Image.FromStream(sPFilePath);

               
                  //Scale factor to resize the image.
                  double sImageWidth     = image.Width;
                  double sImageHeight = image.Height;
                  double scaleFactor     = 0;

                  if(iWidth / sImageWidth < iHeight / sImageHeight)
                        scaleFactor = iWidth / sImageWidth;
                  else
                        scaleFactor = iHeight / sImageHeight;

                  int iNewHeight     = Convert.ToInt32(sImageHeight * scaleFactor);
                  int iNewWidth     = Convert.ToInt32(sImageWidth * scaleFactor);
               

                  //Get the thumbnail for the image.
                  System.Drawing.Bitmap thumbNail = new System.Drawing.Bitmap(iNewWidth,iNewHeight);
                  System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumbNail);
                  g.DrawImage(image, new Rectangle(0,0,thumbNail.Width,thumbNail.Height),0,0,image.Width,image.Height,GraphicsUnit.Pixel);
                  image.Dispose();
               
                     
            //TODO:Remove this
            string PathToSaveFile      = System.Web.HttpContext.Current.Server.MapPath("i");
           
           
           
                  //Save the image corresponding to its Type.
                  switch(sFileType) {
                        case "jpg":
                        case "jpeg":
                              thumbNail.Save(PathToSaveFile,jpegICI,encodeParams);
                              break;

                        case "bmp":
                              thumbNail.Save(PathToSaveFile,bmpICI,encodeParams);
                              break;

                        case "gif":
                              thumbNail.Save(PathToSaveFile,gifICI,encodeParams);
                              break;

                        case "png":
                              thumbNail.Save(PathToSaveFile,pngICI,encodeParams);
                              break;

                  }
                                             
            }
0
WillyCornbread
Asked:
WillyCornbread
  • 4
  • 3
1 Solution
 
apb2Commented:
A fiver it is these two lines

 g.DrawImage(image, new Rectangle(0,0,thumbNail.Width,thumbNail.Height),0,0,image.Width,image.Height,GraphicsUnit.Pixel);
               image.Dispose();

Put a try/catch around them

The first line i find a little confusing. You are drawing the image to the graphic, shouldn't this be your line

g.DrawImage(image, new Rectangle(0,0,thumbNail.Width,thumbNail.Height));

The second line (and the one i belive you're getting the error you are disposing the initial image, you should be disposing the graphic

g.Dispose();

try this (my c# is flaky at best)

try
{
   g.DrawImage(image, new Rectangle(0,0,thumbNail.Width,thumbNail.Height));
}
catch (Exception ex)
{
e.Graphics.DrawString(
ex.ToString(),
new Font("Arial", 8),
Brushes.Black,
new PointF(0, 0));
}
finally
{
 g.Dispose();
}

hth

apb2
0
 
WillyCornbreadAuthor Commented:
Thanks, I made the changes you suggested, but I am still getting the same error on the .save method. The lines wrapped in the try catch are not throwing any errors.

Any other ideas?

b
0
 
apb2Commented:
Bugger

try just saving it a jpg initialy before the using your switch statement i.e.

simply try this:

thumbNail.Save(PathToSaveFile, ImageFormat.Jpeg);

if that works change it back, and debug your save lines.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
WillyCornbreadAuthor Commented:
Okay, more info...

If I change the current save method:

thumbNail.Save(PathToSaveFile,jpegICI,encodeParams);
to
thumbNail.Save(PathToSaveFile+"/THUMBTEST.jpg");

it saves the thumbnail fine, so the problem must be either the encodeParams, or the codec jpegICI, but I can't figure out what the issue is, and my debugging gives me nothing but a generice error, all the params look correct.

b
0
 
apb2Commented:
Excellent

Instead of adding you encode parameters on the save declare them before you draw the image this should be a work around, giving you the quality you want ie.


System.Drawing.Bitmap thumbNail = new System.Drawing.Bitmap(iNewWidth,iNewHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumbNail);


g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality

g.DrawImage(image, new Rectangle 0,0,thumbNail.Width,thumbNail.Height));

So you can rewrite you're code using the

public void Save(string, ImageFormat);  /overload

what you did wrong i can't say. but it obviously faulters on the the encode parameters

0
 
WillyCornbreadAuthor Commented:

That worked, and the quality is what I wanted, thanks for the workaround, and at this point, I don't even care what I did wrong in using the EncoderParams array, it's time to put this to bed and move on.

Thanks again for the great help :)

b

0
 
apb2Commented:
No bother

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now