A Generic error occurred in GDI+ in Bitmap.Save method
When either a Bitmap object or an Image object is constructed from a
file, the file remains locked for the lifetime of the object. As a
result, you cannot change an image and save it back to the same file
where it originated.
http://support.microsoft.com/?id=814675
A generic error occurred in GDI+, JPEG Image to MemoryStream
Image.Save(..) throws a GDI+ exception because the memory stream is closed
http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html
EDIT:
just writing from memory...
save to an 'intermediary' memory stream, that should work
e.g. try this one - replace
Bitmap newBitmap = new Bitmap(thumbBMP);
thumbBMP.Dispose();
thumbBMP = null;
newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg);
with something like:
string outputFileName = "...";
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
thumbBMP.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
A generic error occurred in GDI+ during Bitmap.Save
Try this below
Please check whether memory stream
didn't disposed
until bmpOptimized
image getting save in the desired location. If problem persist, then Check if path exists.
A Generic error occurs at GDI+ at Bitmap.Save() after using SaveFileDialog
Finally I could find what was wrong in my code and would like to mention it here as I think it may be useful to someone....
As I have given a relative path in tempImg.Save, and after the user clicks 'Save' in SaveFileDialog, the actual path for tempImg.Save become :
Path specified by SaveFileDialog + the relative path
automatically.
Thus if the path does not exist, this error occurs.
Thanks every one for the answers.
A generic error occurred in GDI+ During a Bitmap Save
Your problem is the Format16bppGrayScale
i don't think the GDI
supports it very well.
Basically if you just create the Bitmap in Format16bppGrayScale
, and save it with nothing else, it still gives the error.
I have taken the liberty to rewrite your method Format32bppPArgb
private unsafe static void Main(string[] args)
{
var height = 600;
var width = 600;
var p1 = new Point(0, 0);
var p2 = new Point(width, 0);
var p3 = new Point(width / 2, height);
var r = new Random();
var p = new Point(r.Next(0, width), r.Next(0, width));
using (var im = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
{
var data = im.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
var sc0 = (int*)data.Scan0;
var pLen = sc0 + height * width;
var black = Color.Black.ToArgb();
var white = Color.White.ToArgb();
for (var pI = sc0; pI < pLen; pI++)
*pI = black;
for (long i = 0; i < width * height; i++)
{
Point tp;
switch (r.Next(0, 3))
{
case 0:
tp = new Point((p1.X + p.X) / 2, (p1.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
case 1:
tp = new Point((p2.X + p.X) / 2, (p2.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
case 2:
tp = new Point((p3.X + p.X) / 2, (p3.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
}
}
im.UnlockBits(data);
im.Save(@"D:\img.png", ImageFormat.Png);
}
}
Result
You can convert it after the fact if you want, add pepper and salt to taste
Also if you get rid of all the points and cache the colors this will be a bit faster
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+ when attempting to use Image.Save
Thank you to Simon Whitehead for answering this in the comments. He said, "3) Make sure the file is not in use by anything else (including your code)."
So the problem was that my own code was using the item.Image object, and was preventing GDI+ to call the dispose() method on it. The solution was to copy the object into a new object, then use that object to "Write." The resulting code is as follows:
try
{
using (Bitmap tempImage = new Bitmap(item.Image))
{
tempImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (Exception e)
{
Debug.WriteLine("DEBUG::LoadImages()::Error attempting to create image::" + e.Message);
}
A generic error occurred in GDI+ exception when saving bitmap
When using the Save method of the Bitmap
you should ensure that the directory exist. In the first case your directory is this:
This directory should exist in your system
C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\ErrorLog
But in the second case (when using Directory.GetCurrentDirectory
method) your directory should be something like this (it may have a extera Debug
or Release
folder before the ErrorLog
)
These directories should not exist in your system (depending you are in Debug or Release Mode)
C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\Debug\ErrorLog
C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\Release\ErrorLog
So bitmap.Save
throws error because the directory does not exists in your system.
C# Generic Error with GDI+ on Bitmap.Save
You are correct in that it is locked by your program and therefore you can't write to it. It's explained on the msdn-page for the Bitmap class (http://msdn.microsoft.com/en-us/library/3135s427.aspx).
One way around this (from the top of my head, might be easier ways though) would be to cache image in a memorystream first and load it from there thus being able to close the file lock.
static void Main(string[] args)
{
MemoryStream ms = new MemoryStream();
using(FileStream fs = new FileStream(@"I:\tmp.jpg", FileMode.Open))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, buffer.Length);
}
Bitmap bitmap = new Bitmap(ms);
// do stuff
bitmap.Save(@"I:\tmp.jpg");
}
A generic error occured in GDI+ while trying to save a bitmap
You can replace your last line
captureBitmap.Save(@"C:\Screenshot.jpg", ImageFormat.Jpeg);
to
string outputFileName = @"C:\Screenshot.jpg";
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
captureBitmap.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
Reason and LINK for further read
Related Topics
How to Use a Reserved Keyword as an Identifier in My JSON Model Class
How to Convert an Escaped JSON String Within a JSON Object
How to Decode a Unicode Character in a String
Differencebetween Declarative and Imperative Paradigm in Programming
Simple State MAChine Example in C#
Error - Unable to Access the Iis Metabase
List<T> Orderby Alphabetical Order
Synchronously Waiting for an Async Operation, and Why Does Wait() Freeze the Program Here
How to Handle Click Event in Button Column in Datagridview
Dropdownlist in MVC 4 with Razor
Web API Put Request Generates an Http 405 Method Not Allowed Error
Difference Between Invoke and Dynamicinvoke