How to find reason for Generic GDI+ error when saving an image?
While I still did not find out the reason what exactly caused the error when saving the image, I found a workaround to apply:
const string i1Path = @"c:\my\i1.jpg";
const string i2Path = @"c:\my\i2.jpg";
var i = Image.FromFile(i1Path);
var i2 = new Bitmap(i);
i2.Save(i2Path, ImageFormat.Jpeg);
I.e. by copying the image internally into a Bitmap
instance and saving this image instead of the original image, the error disappeared.
I'm assuming that by copying it, the erroneous parts the caused the original Save
call to fail are being removed an/or normalized, thus enabling the save operation to succeed.
Interestingly, the so stored image has a smaller file on disk (16 kB) than its original source (26 kB).
A generic error occurred in GDI+, JPEG Image to MemoryStream
OK I seem to have found the cause just by sheer luck and its nothing wrong with that particular method, it's further back up the call stack.
Earlier I resize the image and as part of that method I return the resized object as follows. I have inserted two calls to the above method and a direct save to a file.
// At this point the new bitmap has no MimeType
// Need to output to memory stream
using (var m = new MemoryStream())
{
dst.Save(m, format);
var img = Image.FromStream(m);
//TEST
img.Save("C:\\test.jpg");
var bytes = PhotoEditor.ConvertImageToByteArray(img);
return img;
}
It appears that the memory stream that the object was created on has to be open at the time the object is saved. I am not sure why this is. Is anyone able to enlighten me and how I can get around this.
I only return from a stream because after using the resize code similar to this the destination file has an unknown mime type (img.RawFormat.Guid) and Id like the Mime type to be correct on all image objects as it makes it hard write generic handling code otherwise.
EDIT
This didn't come up in my initial search but here's the answer from Jon Skeet
A generic error occurred in GDI+ while saving image to MemoryStream
As mentioned by Klaus Gütter in the comments, you are not allowed to dispose the stream before the bitmap.
One option to fix this would be to return a clone of the bitmap, that should ensure all the pixel-data is copied to a separate buffer, allowing the original stream to be freed.
using var bmp = new Bitmap(memoryStream);
images.Add(bmp.Clone(new Rectangle(0,0, bmp.Width, bmp.Height), PixelFormat.Format32bppArgb);
Another option could be to just not dispose the memory stream. Since MemoryStream just represents a chunk of managed memory it is safe to rely on the garbage collector for clean up. A good rule of thumb is to always dispose disposable objects, but this is less important when you know that the object only owns managed resources, like memory.
A generic GDI+ error occurs when saving an image
I don't have a reference handy, but it's been my experience that GDI-based graphics, and especially bitmap handling, is only fully supported within a 16-bit signed coordinate system. I.e. drawing coordinates are limited to between -32768 and 32767, and the max width (or height) of a bitmap would be 65535 (since dimensions can't be negative). The latter being consistent with JPEG's maximums.
PNG can theoretically handle much larger (32-bit dimensions), but it wouldn't be surprising if there's some component shared between the two encoders that assumes the JPEG limit. Sorry I can't dig up the reference at the moment, but I expect it's out there if you look hard enough.
A generic error occured in GDI+ while saving image
Quite simply you are confusing paths and filenames.
The problem if could hazzard a guess, you probably have a folder that is your filename, and you are trying to save a file with that same name, which windows forbids
Your code tweaked
var image = $"{obj.ImageName }.jpg";
// get the path, and only the path
string path = HostingEnvironment.MapPath($"/Images/{ImageType}/{ID}/");
// Create directory if needed (from that path)
Directory.CreateDirectory(path,image);
...
// now create the correct full path
var fullPath = Path.Combine(path,fileName);
// save
img.Save(fullPath, ImageFormat.Jpeg);
Unable to resolve A generic error occurred in GDI+ when save image in Web API
There are some possible reason for this problem.
- You need to check whether current user have (IIS user) write permission on your destination folder.
- You need to check whether current file path
objScreencapture.ThumbImagepath
and/orobjScreencapture.FullImagepath
exist or not (except file name).
A generic error occurred in GDI+ when saving Image
Try
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images\stitchedImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
instead of
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg);
The first argument to Image.Save (String, ImageFormat)
is the file name to which to save, not the directory to which to save. Previously you had done
string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");
So @"D:\Naved\BasicDotNet\Images"
must be a directory, not a file.
(Incidentally, I suggest saving the stitched image to a different directory; otherwise, every time you run your code you'll stitch the previous stitch together with the previous contents.)
“A generic error occurred in GDI+” error where save image to file
this happen usually (for me) when the path of the image is wrong ... check yours images paths
How to fix Generic GDI+ error when saving/uploading an image?
I found what is the problem it is about FTP File permission problem, fixed it.
Related Topics
How to Get the Data Type of a Variable in C#
"Async Task Then Await Task" VS "Task Then Return Task"
When Is a C# Value/Object Copied and When Is Its Reference Copied
Write Values in App.Config File
ASP.NET MVC Dropdown List from Selectlist
C# Native Host with Chrome Native Messaging
The Object Cannot Be Deleted Because It Was Not Found in the Objectstatemanager
Parse and Modify a Query String in .Net Core
I Want to Understand the Lambda Expression in @Html.Displayfor(Modelitem => Item.Firstname)
Get an Idatareader from a Typed List
Dynamic MySQL Database Connection for Entity Framework 6
Does Mstest Have an Equivalent to Nunit's Testcase
Delete Specific Line from a Text File
3D Relative Angle Sum Calculation
When Is Optimization Premature