Error: A Generic Error occured in GDI+
-
Hi Dear, Here I am trying to upload a photo.
protected void LoadImage() {
string UserName = Session["UserName"].ToString();byte\[\] Data = Profile.GetImageData(UserName); //Stmt Correct where profile is class if (Data != null) { MemoryStream stream = new MemoryStream(Data); Bitmap bitmap = null; bitmap = new Bitmap(stream); bitmap.Save(Server.MapPath(@"~\\UserPhoto.bmp"), system.Drawing.Imaging.ImageFormat.Bmp); //Error:A Generic Error occurred in GDI+ bitmap.Dispose(); imgPhoto.ImageUrl = @"~\\UserPhoto.bmp"; } }
I have created virtual directory and trying to open it directly from browser
-
Hi Dear, Here I am trying to upload a photo.
protected void LoadImage() {
string UserName = Session["UserName"].ToString();byte\[\] Data = Profile.GetImageData(UserName); //Stmt Correct where profile is class if (Data != null) { MemoryStream stream = new MemoryStream(Data); Bitmap bitmap = null; bitmap = new Bitmap(stream); bitmap.Save(Server.MapPath(@"~\\UserPhoto.bmp"), system.Drawing.Imaging.ImageFormat.Bmp); //Error:A Generic Error occurred in GDI+ bitmap.Dispose(); imgPhoto.ImageUrl = @"~\\UserPhoto.bmp"; } }
I have created virtual directory and trying to open it directly from browser
Hi, what is that tilde doing there? Here is a standard reply I found in the attic: Most, if not all, errors inside GDI+ are reported as "generic problem occurred in GDI+". If the affected line is an Image.Save chances are your path is incorrect or inaccessible, your disk is full, or your destination file exists and is locked. If you load an image from a file, most of the time the file remains locked as long as the Image is alive. This would prevent you from saving an image to the same path. It applies to Image.FromFile, and probably also to PictureBox.ImageLocation The one exception I am aware of is when you use Image.FromStream. So I suggest one of these two workarounds:
Bitmap bm=null;
{
Bitmap bm1=Image.FromFile(filepath);
bm=new Bitmap(bm1);
bm1.Dispose();
}
// bm is OK now, and bm1 is disposed of and out of scope, the file isn’t locked.or
Bitmap bm=null;
using (FileStream stream=File.OpenRead("image.jpeg")) {
bm=Image.FromStream(stream);
}:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
-
Hi Dear, Here I am trying to upload a photo.
protected void LoadImage() {
string UserName = Session["UserName"].ToString();byte\[\] Data = Profile.GetImageData(UserName); //Stmt Correct where profile is class if (Data != null) { MemoryStream stream = new MemoryStream(Data); Bitmap bitmap = null; bitmap = new Bitmap(stream); bitmap.Save(Server.MapPath(@"~\\UserPhoto.bmp"), system.Drawing.Imaging.ImageFormat.Bmp); //Error:A Generic Error occurred in GDI+ bitmap.Dispose(); imgPhoto.ImageUrl = @"~\\UserPhoto.bmp"; } }
I have created virtual directory and trying to open it directly from browser
First you should be very careful when you use GDI+ combined with ASP.NET.Microsoft warns that some GDI+ features may will cause some unexpected problems when are used with ASP.NET. Why don't you write your own HTTP HttpHandler that handles images load from the database for example?
Life is a stage and we are all actors!
-
Hi Dear, Here I am trying to upload a photo.
protected void LoadImage() {
string UserName = Session["UserName"].ToString();byte\[\] Data = Profile.GetImageData(UserName); //Stmt Correct where profile is class if (Data != null) { MemoryStream stream = new MemoryStream(Data); Bitmap bitmap = null; bitmap = new Bitmap(stream); bitmap.Save(Server.MapPath(@"~\\UserPhoto.bmp"), system.Drawing.Imaging.ImageFormat.Bmp); //Error:A Generic Error occurred in GDI+ bitmap.Dispose(); imgPhoto.ImageUrl = @"~\\UserPhoto.bmp"; } }
I have created virtual directory and trying to open it directly from browser
Along the lines of the other recommendations, taking the image data and constructing a
Bitmap
of it seems to be extraneous. If you have the image data already, then there is no benefit in using that data to construct aBitmap
other than being able to invokeBitmap.Save()
. All you really need to do is write the data directly to disk:using (FileStream fs = new FileStream(Server.MapPath(@"~\UserPhoto.bmp"), FileMode.CreateNew))
{
fs.Write(Data, 0, Data.Length);
}This also has the benefit of removing GDI from the equation. So, if you still get an exception you will atleast be able to catch it as some sort of IO Exception and get more meaningful information about the problem.
Hold on a second here... Don't you think you might be putting the horse ahead of the cart?
-
Hi, what is that tilde doing there? Here is a standard reply I found in the attic: Most, if not all, errors inside GDI+ are reported as "generic problem occurred in GDI+". If the affected line is an Image.Save chances are your path is incorrect or inaccessible, your disk is full, or your destination file exists and is locked. If you load an image from a file, most of the time the file remains locked as long as the Image is alive. This would prevent you from saving an image to the same path. It applies to Image.FromFile, and probably also to PictureBox.ImageLocation The one exception I am aware of is when you use Image.FromStream. So I suggest one of these two workarounds:
Bitmap bm=null;
{
Bitmap bm1=Image.FromFile(filepath);
bm=new Bitmap(bm1);
bm1.Dispose();
}
// bm is OK now, and bm1 is disposed of and out of scope, the file isn’t locked.or
Bitmap bm=null;
using (FileStream stream=File.OpenRead("image.jpeg")) {
bm=Image.FromStream(stream);
}:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
In ASP.NET the tilde at the beginning of a path represents the local file system path to the root of the application. Without it you'd have no way for the server to reliably access the local file system relevant to the application. So, for example if I have an ASP.NET application on my local machine named "TestApp", when I call
Server.MapPath("~");
I would get something like "C:\Inetpub\wwwroot\TestApp".Hold on a second here... Don't you think you might be putting the horse ahead of the cart?