Bitmap.save Memorystream Gdi Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this a generic error occurred in gdi+. bitmap.save c# site About Us Learn more about Stack Overflow the company Business Learn more
How To Solve A Generic Error Occurred In Gdi+
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
A Generic Error Occurred In Gdi In C# Windows Application
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Image.Save(..) throws
System.runtime.interopservices.externalexception (0x80004005): A Generic Error Occurred In Gdi+
a GDI+ exception because the memory stream is closed up vote 72 down vote favorite 29 i've got some binary data which i want to save as an image. When i try to save the image, it throws an exception if the memory stream used to create the image, was closed before the save. The reason i do this is because i'm dynamically creating getencoderclsid images and as such .. i need to use a memory stream. this is the code: [TestMethod] public void TestMethod1() { // Grab the binary data. byte[] data = File.ReadAllBytes("Chick.jpg"); // Read in the data but do not close, before using the stream. Stream originalBinaryDataStream = new MemoryStream(data); Bitmap image = new Bitmap(originalBinaryDataStream); image.Save(@"c:\test.jpg"); originalBinaryDataStream.Dispose(); // Now lets use a nice dispose, etc... Bitmap2 image2; using (Stream originalBinaryDataStream2 = new MemoryStream(data)) { image2 = new Bitmap(originalBinaryDataStream2); } image2.Save(@"C:\temp\pewpew.jpg"); // This throws the GDI+ exception. } Does anyone have any suggestions to how i could save an image with the stream closed? I cannot rely on the developers to remember to close the stream after the image is saved. In fact, the developer would have NO IDEA that the image was generated using a memory stream (because it happens in some other code, elsewhere). I'm really confused :( c# image exception gdi+ share|improve this question edited Dec 3 '08 at 8:30 Lasse V. Karlsen 220k64433630 asked Dec 3 '08 at 7:04 Pure.Krome 37.3k69262457 13 Please do not use images for code like this, makes it harder for people to copy th
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About image.save c# Us Learn more about Stack Overflow the company Business Learn more about hiring save bitmap to file c# developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the c# bitmap to byte array Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up A Generic error occured in GDI+ http://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory-stream-is-closed in Bitmap.Save method up vote 26 down vote favorite 8 I am working on to upload and same a thumnail copy of that image in a thumbnail forder. I am using following link: http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx but newBMP.Save(directory + "tn_" + filename); is causing exception "A generic error occurred in GDI+." I have tried to give permission on folder, also tried to use a new separate bmp object when http://stackoverflow.com/questions/15862810/a-generic-error-occured-in-gdi-in-bitmap-save-method saving. Edit: protected void ResizeAndSave(PropBannerImage objPropBannerImage) { // Create a bitmap of the content of the fileUpload control in memory Bitmap originalBMP = new Bitmap(fuImage.FileContent); // Calculate the new image dimensions int origWidth = originalBMP.Width; int origHeight = originalBMP.Height; int sngRatio = origWidth / origHeight; int thumbWidth = 100; int thumbHeight = thumbWidth / sngRatio; int bannerWidth = 100; int bannerHeight = bannerWidth / sngRatio; // Create a new bitmap which will hold the previous resized bitmap Bitmap thumbBMP = new Bitmap(originalBMP, thumbWidth, thumbHeight); Bitmap bannerBMP = new Bitmap(originalBMP, bannerWidth, bannerHeight); // Create a graphic based on the new bitmap Graphics oGraphics = Graphics.FromImage(thumbBMP); // Set the properties for the new graphic file oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the new graphic based on the resized bitmap oGraphics.DrawImage(originalBMP, 0, 0, thumbWidth, thumbHeight); Bitmap newBitmap = new Bitmap(thumbBMP); thumbBMP.Dispose(); thumbBMP = null; // Save the new graphic file to the server newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg); oGraphics = Graphics.FromImage(bannerBMP); // Set the properties for the new graphic file oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the new graphic based on the resized bitmap oGraphics.DrawImage(originalBMP, 0, 0, bannerWidth, bannerHeight); // Save the new graphic fil
GDI+ error' if the stream is closed. Visual Studio Languages , .NET Framework > Visual C# Question 0 Sign in to vote I'm doing a bit of processing of an image, I just need to rotate it, increase the https://social.msdn.microsoft.com/Forums/vstudio/en-US/549db796-0b4d-462f-9dae-b79b9689eba6/saveload-a-bitmap-from-memorystream-imageconverter-generic-gdi-error-if-the-stream-is-closed?forum=csharpgeneral size, and convert to 1 bit-per-pixel. I'm just having ttrouble with the below error. Code is, at its most basic, as follows: Bitmap b = new Bitmap("C:\\somePic.png"); //Increase the length/width b = new Bitmap(b, (int)(b.Width * 1.6), http://www.hanselman.com/blog/TheWeeklySourceCode50ALittleOnAGenericErrorOccurredInGDIAndTroubleGeneratingImagesOnWithASPNET.aspx (int)(b.Height * 1.7)); //rotate it, flip it b.RotateFlip(RotateFlipType.Rotate90FlipX); //Now, for reasons I can't explain, the RoateFlip method converts the format of the bitmap. //The size of it becomes lower so I assume its compressing it somehow? //As a generic such, I am having to Save it and Reload it, to get it back into a 'real' bitmap format. Stream imgStream = new MemoryStream(); b.Save(imgStream, ImageFormat.Bmp); b = new Bitmap(imgStream); //its these lines that cause the error imgStream.Close(); imgStream.Dispose(); //convert it to a byte array to fire at a printer. ImageConverter converter = new ImageConverter(); byte[] test = (byte[])converter.ConvertTo(b, typeof(byte[])); Now the above fails the the converter.ConvertTo(...) line: "A generic error occurred in GDI+" If a generic error I remove the Close()/Dispose() of the imgStream, then it works. But then of course I have a stream left open... I'm just trying to make sense of why this is the case. or if I could get RotateFlip to keep the image in-tact, thats even better! (or even a more efficient way of getting my original format back). Thursday, August 09, 2012 11:50 AM Reply | Quote Answers 1 Sign in to vote Eyal is correct you need to have the memory stream open when you call ConvertTo. Take a look at this answer there are three links that address this very issue. http://forums.asp.net/t/1766006.aspx/1. Try Something like this. Bitmap b = new Bitmap("C:\\bluetooth-logo.jpg"); //Increase the length/width b = new Bitmap(b, (int)(b.Width * 1.6), (int)(b.Height * 1.7)); //rotate it, flip it b.RotateFlip(RotateFlipType.Rotate90FlipX); //Now, for reasons I can't explain, the RoateFlip method converts the format of the bitmap. //The size of it becomes lower so I assume its compressing it somehow? //As such, I am having to Save it and Reload it, to get it back into a 'real' bitmap format. using (Stream imgStream = new MemoryStream()) { b.Save(imgStream, ImageFormat.Bmp); b = new Bitmap(imgStream); //convert it to a byte array to fire at a printer. ImageConverter converter = new ImageConverter(); byte[] test = (byte[])converter.ConvertTo(b, typeof(byte[])); } Mike Marked as answer by JimmyBee Thursday, August 09
ASP.NET|Source Code Sponsored By I got a nice little Yellow Screen of Death (YSOD) error on some code running under IIS that worked fine when running on the VS Developer Web Server. The error was "A generic error occurred in GDI+" and you know that if an error is generic, it's sure in the heck not specific. My little application takes an overhead map that's stored in a local file, does some calculations from user input and draws an X on the map, then returns the resulting dynamically generated image. There's basically three ways to do images on the server side. Use Native APIs and Interop, which only works in full trust, use System.Drawing, which "isn't supported" or use WPF on the server side, which also, ahem, isn't officially supported. I'm still trying to figure out why, but just to be clear, I used System.Drawing in extremely high traffic sites with no problems. As long as paid close attention to my unmanaged resources, I have never had a problem. I've heard anecdotally of people having trouble with GDI+ (System.Drawing) and switching over to WPF and having no problem with that. As with all things, test what you're doing. There's even some ASP.NET Controls on CodePlex that might help. Now this post can't answer ALL reasons you're getting "a generic error occurred in GDI+" but it can answer mine. In my particular case (and I think this is the most common mistake) I was saving the composited image as a PNG. First, I'll show you a little chunk of a code from 5 years ago that took two images and built a single image from them. public class SomeCheckImageHandler : IHttpHandler{ //some stuff snipped public SomeCheckImageHandler(){} public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpg"; //some stuff snipped GetCheckImageRequest req = new GetCheckImageRequest(); //some stuff snipped, get the params from the QueryString GetCheckImageResponse res = banking.GetCheckImage(req); //some stuff snipped if (res.ImageBack != null) { //merge them into one image using(MemoryStream m = new MemoryStream(res.BackImageBytes)) using(Image backImage = System.Drawing.Image.FromStream(m)) using(MemoryStream m2 = new MemoryStream(res.BrontImageBytes)) using(Image frontImage = System.Drawing.Image.FromStream(m2)) using(Bitmap compositeImage = new Bitmap(frontImage.Width,frontImage.Height+backImage.Height)) using(Graphics compositeGraphics = Graphics.FromImage(compositeImage)) { compositeGraphics.CompositingMode = CompositingMode.SourceCopy; compositeGraphics.DrawImageUnscaled(front