GDI Related issue - External Exception being thrown in System.Drawing.dll
-
Hi, i am trying to write a title to an image and save it back to its current location but i am having a problem as the following message is being thrown: A generic error occured in GDI+ A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll System.Drawing A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll System.Drawing Error code: -2147467259 " at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)\r\n at System.Drawing.Image.Save(String filename, ImageFormat format)\r\n at ZooSystem.ImageViewer.ApplyChanges() in E:\\ZooSystem\\\ImageViewer.cs:line 520" I have checked the permissions for each of the images in the folder and they all have FullControl/Read/Write/Modify permissions but the folder has "Read-Only" enabled. The folder was created on the Internal Hard drive of the computer and I am using Windows Vista Home Premium 32-bit. I am using MS VS 2008 Pro edition which i run in Admin mode. If i save the images to another folder say one of the Public folders then the above exception is not thrown. But I would like to save my images to the same folder if possible? How could i go about doing this? I would appreciate if somebody could help me with the above. I have been trying to fix it for past 2 hours. Thanks, Here is the code i am using:
private void ApplyChanges()
{
//check if the destination folder in the textbox is blank in order to save a single image
//using the Save-file dialog feature.
if(txtBoxOut.Text.Equals("")
{
//write the title to the image by calling a write method//show the save file dialog and check if result is OK.
//call the custom save method
if(sfd.ShowDialog()==DialogResult.OK)
{this.SaveTheImage(sfd.FileName,this.bmp); }
}
else if(txtBoxOut.Text != "")
{string destFolder = txtBoxOut.text
foreach(string filePath in filePaths)
{
this.bmp = Image.FromFile(filePath);
string fileName = Path.GetFileName(filePath);try { //write the title to the global image //check folder if(destFolder.EndsWith("\\\\") { //use the inbuilt save method this.bmp.Save(destFolder+fileName+ImageFormat.Jpeg); } else { this.bmp.Save(destFolder+"\\\\"+fileName+ImageFormat.Jpeg); } }
-
Hi, i am trying to write a title to an image and save it back to its current location but i am having a problem as the following message is being thrown: A generic error occured in GDI+ A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll System.Drawing A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll System.Drawing Error code: -2147467259 " at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)\r\n at System.Drawing.Image.Save(String filename, ImageFormat format)\r\n at ZooSystem.ImageViewer.ApplyChanges() in E:\\ZooSystem\\\ImageViewer.cs:line 520" I have checked the permissions for each of the images in the folder and they all have FullControl/Read/Write/Modify permissions but the folder has "Read-Only" enabled. The folder was created on the Internal Hard drive of the computer and I am using Windows Vista Home Premium 32-bit. I am using MS VS 2008 Pro edition which i run in Admin mode. If i save the images to another folder say one of the Public folders then the above exception is not thrown. But I would like to save my images to the same folder if possible? How could i go about doing this? I would appreciate if somebody could help me with the above. I have been trying to fix it for past 2 hours. Thanks, Here is the code i am using:
private void ApplyChanges()
{
//check if the destination folder in the textbox is blank in order to save a single image
//using the Save-file dialog feature.
if(txtBoxOut.Text.Equals("")
{
//write the title to the image by calling a write method//show the save file dialog and check if result is OK.
//call the custom save method
if(sfd.ShowDialog()==DialogResult.OK)
{this.SaveTheImage(sfd.FileName,this.bmp); }
}
else if(txtBoxOut.Text != "")
{string destFolder = txtBoxOut.text
foreach(string filePath in filePaths)
{
this.bmp = Image.FromFile(filePath);
string fileName = Path.GetFileName(filePath);try { //write the title to the global image //check folder if(destFolder.EndsWith("\\\\") { //use the inbuilt save method this.bmp.Save(destFolder+fileName+ImageFormat.Jpeg); } else { this.bmp.Save(destFolder+"\\\\"+fileName+ImageFormat.Jpeg); } }
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]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
Hi, i am trying to write a title to an image and save it back to its current location but i am having a problem as the following message is being thrown: A generic error occured in GDI+ A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll System.Drawing A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll System.Drawing Error code: -2147467259 " at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)\r\n at System.Drawing.Image.Save(String filename, ImageFormat format)\r\n at ZooSystem.ImageViewer.ApplyChanges() in E:\\ZooSystem\\\ImageViewer.cs:line 520" I have checked the permissions for each of the images in the folder and they all have FullControl/Read/Write/Modify permissions but the folder has "Read-Only" enabled. The folder was created on the Internal Hard drive of the computer and I am using Windows Vista Home Premium 32-bit. I am using MS VS 2008 Pro edition which i run in Admin mode. If i save the images to another folder say one of the Public folders then the above exception is not thrown. But I would like to save my images to the same folder if possible? How could i go about doing this? I would appreciate if somebody could help me with the above. I have been trying to fix it for past 2 hours. Thanks, Here is the code i am using:
private void ApplyChanges()
{
//check if the destination folder in the textbox is blank in order to save a single image
//using the Save-file dialog feature.
if(txtBoxOut.Text.Equals("")
{
//write the title to the image by calling a write method//show the save file dialog and check if result is OK.
//call the custom save method
if(sfd.ShowDialog()==DialogResult.OK)
{this.SaveTheImage(sfd.FileName,this.bmp); }
}
else if(txtBoxOut.Text != "")
{string destFolder = txtBoxOut.text
foreach(string filePath in filePaths)
{
this.bmp = Image.FromFile(filePath);
string fileName = Path.GetFileName(filePath);try { //write the title to the global image //check folder if(destFolder.EndsWith("\\\\") { //use the inbuilt save method this.bmp.Save(destFolder+fileName+ImageFormat.Jpeg); } else { this.bmp.Save(destFolder+"\\\\"+fileName+ImageFormat.Jpeg); } }
Windows probably has the bitmap locked if you are saving the file over itself. If you are trying to save the image over itself. First, create a copy of the image and dispose of the original bitmap. Then apply the title to the copy and save it over the original image. Once disposed, it should not give you any problems. you can use the Bitmap(Bitmap) constructor in order to make a copy.
-Bryan My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
-
Windows probably has the bitmap locked if you are saving the file over itself. If you are trying to save the image over itself. First, create a copy of the image and dispose of the original bitmap. Then apply the title to the copy and save it over the original image. Once disposed, it should not give you any problems. you can use the Bitmap(Bitmap) constructor in order to make a copy.
-Bryan My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
Hi, Thanks for the advice. I have checked the amount of space on my HDD and i have 255 GB free. I am trying to save the images back to a folder which is located in "Pictures" folder. I tried to make a copy of the image but it still throws an exception. My write method writes the title to the global image after i have created the image. I am using Filestream as advised. I have wrapped the whole block of code when using the "using" directive. I also dispose the global image after i have made a copy of it. I used the clone method and then use the cloned image to be saved. But i still get the same error message. Any thoughts? Here is a segment of the code:
else if(txtBoxOut.Text != "")
{string destFolder = txtBoxOut.text
foreach(string filePath in filePaths)
{
using(FileStream fileStream = File.OpenRead(filePath))
{
this.bmp = Image.FromStream(fileStream);
string fileName = Path.GetFileName(filePath);try { //write the title to the global image by using a custom write method //create a clone Image newImage = (Image)this.bmp.Clone(); this.bmp.Dispose(); //check folder if(destFolder.EndsWith("\\\\") { //use the inbuilt save method this.newImage.Save(destFolder+fileName+ImageFormat.Jpeg); } else { this.newImage.Save(destFolder+"\\\\"+fileName+ImageFormat.Jpeg); } } catch(Exception e) { Console.WriteLine(e.message.ToString()); } }
}
} -
Hi, Thanks for the advice. I have checked the amount of space on my HDD and i have 255 GB free. I am trying to save the images back to a folder which is located in "Pictures" folder. I tried to make a copy of the image but it still throws an exception. My write method writes the title to the global image after i have created the image. I am using Filestream as advised. I have wrapped the whole block of code when using the "using" directive. I also dispose the global image after i have made a copy of it. I used the clone method and then use the cloned image to be saved. But i still get the same error message. Any thoughts? Here is a segment of the code:
else if(txtBoxOut.Text != "")
{string destFolder = txtBoxOut.text
foreach(string filePath in filePaths)
{
using(FileStream fileStream = File.OpenRead(filePath))
{
this.bmp = Image.FromStream(fileStream);
string fileName = Path.GetFileName(filePath);try { //write the title to the global image by using a custom write method //create a clone Image newImage = (Image)this.bmp.Clone(); this.bmp.Dispose(); //check folder if(destFolder.EndsWith("\\\\") { //use the inbuilt save method this.newImage.Save(destFolder+fileName+ImageFormat.Jpeg); } else { this.newImage.Save(destFolder+"\\\\"+fileName+ImageFormat.Jpeg); } } catch(Exception e) { Console.WriteLine(e.message.ToString()); } }
}
}Hi, In this bit of code you still have the stream open because you are doing your bitmap operations inside your using. Windows may still have hold of the bitmap. Copy the bitmap inside the using and do the other stuff outside of the using. See if that works. something like this...
using(FileStream fileStream = File.OpenRead(filePath))
{
this.bmp = Image.FromStream(fileStream);
string fileName = Path.GetFileName(filePath);
Image newImage = (Image)this.bmp.Clone();
this.bmp.Dispose();
}if(destFolder.EndsWith("\\"))
.
.
.-Bryan My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
-
Hi, Thanks for the advice. I have checked the amount of space on my HDD and i have 255 GB free. I am trying to save the images back to a folder which is located in "Pictures" folder. I tried to make a copy of the image but it still throws an exception. My write method writes the title to the global image after i have created the image. I am using Filestream as advised. I have wrapped the whole block of code when using the "using" directive. I also dispose the global image after i have made a copy of it. I used the clone method and then use the cloned image to be saved. But i still get the same error message. Any thoughts? Here is a segment of the code:
else if(txtBoxOut.Text != "")
{string destFolder = txtBoxOut.text
foreach(string filePath in filePaths)
{
using(FileStream fileStream = File.OpenRead(filePath))
{
this.bmp = Image.FromStream(fileStream);
string fileName = Path.GetFileName(filePath);try { //write the title to the global image by using a custom write method //create a clone Image newImage = (Image)this.bmp.Clone(); this.bmp.Dispose(); //check folder if(destFolder.EndsWith("\\\\") { //use the inbuilt save method this.newImage.Save(destFolder+fileName+ImageFormat.Jpeg); } else { this.newImage.Save(destFolder+"\\\\"+fileName+ImageFormat.Jpeg); } } catch(Exception e) { Console.WriteLine(e.message.ToString()); } }
}
}when you want to create a new file, unrelated to existing, already open, files all stuff about locked files, file streams, etc is irrelevant. What is relevant though is:
destFolder+fileName+ImageFormat.Jpeg
is non-sense. Store that in a string variable and look at its value. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
when you want to create a new file, unrelated to existing, already open, files all stuff about locked files, file streams, etc is irrelevant. What is relevant though is:
destFolder+fileName+ImageFormat.Jpeg
is non-sense. Store that in a string variable and look at its value. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Yes, Luc is right... Wow, i don't believe i didn't see that. :doh:
-Bryan My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
-
Hi, In this bit of code you still have the stream open because you are doing your bitmap operations inside your using. Windows may still have hold of the bitmap. Copy the bitmap inside the using and do the other stuff outside of the using. See if that works. something like this...
using(FileStream fileStream = File.OpenRead(filePath))
{
this.bmp = Image.FromStream(fileStream);
string fileName = Path.GetFileName(filePath);
Image newImage = (Image)this.bmp.Clone();
this.bmp.Dispose();
}if(destFolder.EndsWith("\\"))
.
.
.-Bryan My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
Hi, I tried to do the above but an exception thrown when i try to write the title. The newImage is global in my case because the title has to be written this way... I try to check outside the using block if the contents in the textbox has been changed and if it has i call the ApplyTitle method which is meant to write the title. But now it throws the exception when i try to write the title. The exception is also thrown again when i try to save the cloned image :(
-
when you want to create a new file, unrelated to existing, already open, files all stuff about locked files, file streams, etc is irrelevant. What is relevant though is:
destFolder+fileName+ImageFormat.Jpeg
is non-sense. Store that in a string variable and look at its value. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Hi, Thanks for your assistance, Just looked at this post after i posted my previous post. Duh me, I can see that its writing jpeg twice. I have changed this so its like C:\.......\image.jpg But I tried to save the file and I still get the same exception. By the way I have done what Bryan advised me to do. I am going to revert my changes i have made apart from the change i have made to the save method. *Update* I have reverted the changes and i still get the same exception. I have checked my ApplyTitle method and all it is doing is setting the user specified value of the title propertyitem of the image. Any further advice would be much appreciated. Thanks
modified on Friday, March 5, 2010 7:21 PM
-
Hi, i am trying to write a title to an image and save it back to its current location but i am having a problem as the following message is being thrown: A generic error occured in GDI+ A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll System.Drawing A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll System.Drawing Error code: -2147467259 " at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)\r\n at System.Drawing.Image.Save(String filename, ImageFormat format)\r\n at ZooSystem.ImageViewer.ApplyChanges() in E:\\ZooSystem\\\ImageViewer.cs:line 520" I have checked the permissions for each of the images in the folder and they all have FullControl/Read/Write/Modify permissions but the folder has "Read-Only" enabled. The folder was created on the Internal Hard drive of the computer and I am using Windows Vista Home Premium 32-bit. I am using MS VS 2008 Pro edition which i run in Admin mode. If i save the images to another folder say one of the Public folders then the above exception is not thrown. But I would like to save my images to the same folder if possible? How could i go about doing this? I would appreciate if somebody could help me with the above. I have been trying to fix it for past 2 hours. Thanks, Here is the code i am using:
private void ApplyChanges()
{
//check if the destination folder in the textbox is blank in order to save a single image
//using the Save-file dialog feature.
if(txtBoxOut.Text.Equals("")
{
//write the title to the image by calling a write method//show the save file dialog and check if result is OK.
//call the custom save method
if(sfd.ShowDialog()==DialogResult.OK)
{this.SaveTheImage(sfd.FileName,this.bmp); }
}
else if(txtBoxOut.Text != "")
{string destFolder = txtBoxOut.text
foreach(string filePath in filePaths)
{
this.bmp = Image.FromFile(filePath);
string fileName = Path.GetFileName(filePath);try { //write the title to the global image //check folder if(destFolder.EndsWith("\\\\") { //use the inbuilt save method this.bmp.Save(destFolder+fileName+ImageFormat.Jpeg); } else { this.bmp.Save(destFolder+"\\\\"+fileName+ImageFormat.Jpeg); } }
Eagle32 wrote:
Console.WriteLine(e.message.ToString());
No Not like that. Message is already a string, calling ToString() on it doesn't do anything but waste CPU cycles. The net result is still a single line of text. What you should do is
e.ToString()
that will provide all available information. Do it now, and tell us what it gives you. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
Eagle32 wrote:
Console.WriteLine(e.message.ToString());
No Not like that. Message is already a string, calling ToString() on it doesn't do anything but waste CPU cycles. The net result is still a single line of text. What you should do is
e.ToString()
that will provide all available information. Do it now, and tell us what it gives you. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Hi, Thanks, Here is what i get:
System.Runtime.InteropServices.ExternalException was caught
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at ZooSystem.ImageViewer.ApplyChanges() in E:\ZooSystem\ImageViewer.cs:line 520
InnerException:I suspect it is something to do with folder permissions but i have checked the folder and file permissions. I am saving the animal pictures back to its original location which is C:\Users\\Pictures\Animals Now in the code it looks like this C:\\Users\\<username>\\Pictures\\Animals.
-
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]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
According to Documentation[^] "You must keep the stream open for the lifetime of the Image." so should not the second snippet throw an exception?
Giorgi Dalakishvili #region signature My Articles Browsing xkcd in a windows 7 way[^] #endregion
-
Hi, Thanks, Here is what i get:
System.Runtime.InteropServices.ExternalException was caught
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at ZooSystem.ImageViewer.ApplyChanges() in E:\ZooSystem\ImageViewer.cs:line 520
InnerException:I suspect it is something to do with folder permissions but i have checked the folder and file permissions. I am saving the animal pictures back to its original location which is C:\Users\\Pictures\Animals Now in the code it looks like this C:\\Users\\<username>\\Pictures\\Animals.
Just an odd question. What happens when you try to save the image in the same folder but a different name? Also, what happens if you go to this folder in Windows explorer and try to create a file? like a new text document or the like. Does UAC gripe at you or something?
-Bryan My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
-
According to Documentation[^] "You must keep the stream open for the lifetime of the Image." so should not the second snippet throw an exception?
Giorgi Dalakishvili #region signature My Articles Browsing xkcd in a windows 7 way[^] #endregion
Hi Giorgi, Thanks. I missed that line in the documentation, and no it has never thrown me an Exception; AFAIK once the pixels are loaded, file (and now also the stream) access is needed only when extra information is asked for, (e.g.the JPEG tags), which I don't ask for unless I use Image.FromFile(). Theoretically I now must change my standard reply to always create a copy of the first image, then dispose of it. Grr. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
Just an odd question. What happens when you try to save the image in the same folder but a different name? Also, what happens if you go to this folder in Windows explorer and try to create a file? like a new text document or the like. Does UAC gripe at you or something?
-Bryan My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.
-
Your welcome. :thumbsup:
-Bryan My latest programming adventure was coding the multimedia features for the Rip Ride Rockit coaster at Universal Studios Florida. I love my job.