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.InteropServ ices.Exter nalExcepti on: 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(O bject sender, EventArgs e) +7
System.Web.UI.Control.OnLo ad(EventAr gs e) +99
System.Web.UI.Control.Load Recursive( ) +50
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +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!
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.InteropServ
Here is the stack trace:
[ExternalException (0x80004005): A generic error occurred in GDI+.]
System.Drawing.Image.Save(
System.Drawing.Image.Save(
imageconverter.makethumb()
imageconverter.Page_Load(O
System.Web.UI.Control.OnLo
System.Web.UI.Control.Load
System.Web.UI.Page.Process
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()
May be this link also be helpful
http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=21701
http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=21701
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(actualDPFEwan ted) == true) File.Delete(actualDPFEwant ed);
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 GetImageDisconnectedFromFi le(string DrvPathFileExt)
{
Image Iret = (Image)new Bitmap(1,1);
if (File.Exists(DrvPathFileEx t) == 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}\\ImageS ink\\", Path.GetDirectoryName(Appl ication.Ex ecutablePa th));
if (!Directory.Exists(DP)) Directory.CreateDirectory( DP);
if (File.Exists(DP + "a.bmp") == true) picturebox1.Image = fn.GetImageDisconnectedFro mFile(DP + "a.bmp");
}
private void WriteOne()
{
string DP = String.Format("{0}\\ImageS ink\\", Path.GetDirectoryName(Appl ication.Ex ecutablePa th));
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'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
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(actualDPFEwan
string tempDPFE = DrvPath + "temporaryToDefeatGDIerror
if (File.Exists(tempDPFE) == true) File.Delete(tempDPFE);
bm.Save(tempDPFE, ImageFormat.Bmp);
File.Copy(tempDPFE, actualDPFEwanted);
File.Delete(tempDPFE);
}
public static Bitmap GetImageDisconnectedFromFi
{
Image Iret = (Image)new Bitmap(1,1);
if (File.Exists(DrvPathFileEx
{
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}\\ImageS
if (!Directory.Exists(DP)) Directory.CreateDirectory(
if (File.Exists(DP + "a.bmp") == true) picturebox1.Image = fn.GetImageDisconnectedFro
}
private void WriteOne()
{
string DP = String.Format("{0}\\ImageS
if (!Directory.Exists(DP)) Directory.CreateDirectory(
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.
thumbpath = "/ihost/thumbs/" & itemno(x)
thumb.Save(Server.MapPath(
what is coming as itemno(x), is it a folder name or you want to prefix it with the thumb.jpg?