Link to home
Start Free TrialLog in
Avatar of kmerlo
kmerlo

asked on

ASP.net - Generic Error In GDI occurs when saving jpg image.

I am using the attached code to generate a 50 X 50 .jpg thumbnail for an uploaded image, then store the path in a database table.  At our current hosting company, this works without issue.  We are moving to a new company, though, and are redesigning the site, using the new host as a test environment for the new build.  There, this code does not work.  The following error is thrown:

A generic error occurred in GDI+.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.


Here is the stack trace:
[ExternalException (0x80004005): A generic error occurred in GDI+.]
   System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +377630
   System.Drawing.Image.Save(String filename, ImageFormat format) +69
   imageconverter.makethumb() +978
   imageconverter.Page_Load(Object sender, EventArgs e) +7
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

I've looked this up, and it appears to have something to do with the bitmap being locked, but I am unsure how to go about fixing it.  Any help would be appreciated!


oldheight = originalimg.Height
            oldwidth = originalimg.Width
 
            newheight = (oldheight / oldwidth) * 50
 
            Dim thumb As New System.Drawing.Bitmap(50, newheight)
 
 
            Dim gr_dest As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(thumb)
 
            gr_dest.DrawImage(originalimg, 0, 0, 50, newheight)
 
            thumbpath = "/ihost/thumbs/" & itemno(x) & "thumb.jpg"
 
            thumb.Save(Server.MapPath(thumbpath), ImageFormat.Jpeg)
 
            cmdstr = "UPDATE DataAuctions SET Thumb = '" & thumbpath & "' WHERE ID =" & itemno(x)
            sqlcommand = New OleDbCommand(cmdstr, myconn)
            rs = sqlcommand.ExecuteReader
            rs.Close()

Open in new window

Avatar of CB_Thirumalai
CB_Thirumalai
Flag of India image

I think the Server.Mappath requires only the path, but, thumbpath gives the file name as well
            thumbpath = "/ihost/thumbs/" & itemno(x)
 
            thumb.Save(Server.MapPath(thumbpath)  & "thumb.jpg", ImageFormat.Jpeg)

what is coming as itemno(x), is it a folder name or you want to prefix it with the thumb.jpg?
Avatar of kmerlo
kmerlo

ASKER

Itemno (x) is an integer- the unique field from the database- you are correct, I am using it as a prefix for the filename.  The filename should be "123thumb.jpg", "125thumb.jpg", etc.
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kmerlo

ASKER

I've contacted the hosting company...hopefully they won't have any problem with giving the permissions to the folder...
SOLUTION: A generic error occurred in GDI+
I'm not sure "WHY" this works ... but it solved my problems.
 
Caveat: Below uses [ImageFormat.Bmp] If you're reading/writing another format, you'll need to tweak.
 
Caveat: During the write operation a temporary file named [temporaryToDefeatGDIerror.bmp] is momentarily created, copied then deleted.
 
Here's a general Function class which does the real work.
------------------------------------------------------------------------------------
class fn
{
    public static void SaveImageToFile(string DrvPath, string FileExt, Bitmap bm)
    {
        string actualDPFEwanted = DrvPath + FileExt;
        if (File.Exists(actualDPFEwanted) == true) File.Delete(actualDPFEwanted);
        string tempDPFE = DrvPath + "temporaryToDefeatGDIerror.bmp";
        if (File.Exists(tempDPFE) == true) File.Delete(tempDPFE);
        bm.Save(tempDPFE, ImageFormat.Bmp);
        File.Copy(tempDPFE, actualDPFEwanted);
        File.Delete(tempDPFE);
    }
    public static Bitmap GetImageDisconnectedFromFile(string DrvPathFileExt)
    {
        Image Iret = (Image)new Bitmap(1,1);
        if (File.Exists(DrvPathFileExt) == true)
        {
            FileInfo FI = new FileInfo(DrvPathFileExt);
            if (FI.Length > 1)
            {
                Stream s = File.Open(DrvPathFileExt, FileMode.Open);
                Iret = Image.FromStream(s);
                s.Close();
            }
        }
        Bitmap BM = new Bitmap(Iret);
        Iret.Dispose();
        GC.Collect(0, GCCollectionMode.Forced);
        return BM;
    }
}
------------------------------------------------------------------------------------
and here's a sample of how I use each fn.call's
------------------------------------------------------------------------------------
private void ReadOne()
{
    string DP = String.Format("{0}\\ImageSink\\", Path.GetDirectoryName(Application.ExecutablePath));
    if (!Directory.Exists(DP)) Directory.CreateDirectory(DP);
    if (File.Exists(DP + "a.bmp") == true) picturebox1.Image = fn.GetImageDisconnectedFromFile(DP + "a.bmp");
}
 
private void WriteOne()
{
    string DP = String.Format("{0}\\ImageSink\\", Path.GetDirectoryName(Application.ExecutablePath));
    if (!Directory.Exists(DP)) Directory.CreateDirectory(DP);
    if (!(picturebox1.Image == null)) fn.SaveImageToFile(DP, "a.bmp", (Bitmap)picturebox1.Image);
}
------------------------------------------------------------------------------------
 
Enjoy y'all
Frederick Volking
 
I've set the permissions and still am getting this same error when I try to use an XCeed tool.