Bitmap Memorystream Error
Contents |
GDI+ error' if the stream is closed. Visual Studio Languages , .NET Framework > Visual C# Question 0 Sign in to vote I'm doing a c# bitmap parameter is not valid memorystream bit of processing of an image, I just need to rotate it, a generic error occurred in gdi c# image save increase the size, and convert to 1 bit-per-pixel. I'm just having ttrouble with the below error. Code is, at system.runtime.interopservices.externalexception (0x80004005): a generic error occurred in gdi+ 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), (int)(b.Height * 1.7)); //rotate it, flip it b.RotateFlip(RotateFlipType.Rotate90FlipX); //Now, for convert bitmap to byte array c# 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. Stream imgStream = new MemoryStream(); b.Save(imgStream, ImageFormat.Bmp); b = new Bitmap(imgStream); //its these lines that cause
Image Save C#
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 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
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
C# Image From Byte Array
site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x 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(..) 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 throws 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 http://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory-stream-is-closed creating 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
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 Us Learn more about Stack Overflow the company Business Learn http://stackoverflow.com/questions/34120973/a-generic-error-occurred-in-gdi-when-saving-bitmap-to-memorystream more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags https://stackoverflow.com/questions/3919766/error-loading-bitmap-from-memorystream Users Badges Ask Question x 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 A Generic Error occurred in GDI+ when saving bitmap to MemoryStream up vote 6 down vote favorite 1 I have some code that is working perfectly a generic on several machines (development, QA, UAT). Unfortunately, on production I'm getting "A Generic Error occurred in GDI+" on the bmp.Save(ms, ImageFormat.Png); As a result, I'm assuming you won't be able to reproduce the problem, but maybe someone can spot my error. A few notes, I've searched around a lot for common solutions, note that this is saving to a MemoryStream so the file permission issues most people suggest do not apply, nor does the "bmp is locked a generic error while open" solution because again, I'm writing somewhere else. Finally, it's not because png requires a seekable stream because MemoryStream is seekable. Note, if I change it to ImageFormat.Jpeg it works fine. I'm only having a problem with PNGs. I found mention of the registry key HKEY_CLASSES_ROOT\CLSID\{FAE3D380-FEA4-4623-8C75-C6B61110B681} potentially being the problem due to permissions. As a result, I set the key to allow Everyone to have read access to this key, no change. public static MemoryStream GenerateImage(string text) { MemoryStream ms = new MemoryStream(); using (Bitmap bmp = new Bitmap(400,400)) { bmp.Save(ms, ImageFormat.Png); ms.Position = 0; } return ms; } Here is the full stack trace: [ExternalException (0x80004005): A generic error occurred in GDI+.] System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +616457 WP.Tools.Img.GenerateImage(String text) +383 Note: my question already enumerates the solutions in the proposed duplicate. None are the issue. If they were it would fail for JPEG as well. c# gdi+ share|improve this question edited Dec 6 '15 at 18:52 asked Dec 6 '15 at 18:23 dman2306 1,2841516 2 Possible duplicate of GDI+ exception saving a Bitmap to a MemoryStream –Thomas Weller Dec 6 '15 at 18:29 1 Maybe related: stackoverflow.com/questions/1053052/… –Thomas Weller Dec 6 '15 at 18:30 1 @Thomas Weller I don't believe that is the same problem. First I'm not freeing the memory anywhere, second if that were the problem I'd expe
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 Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x 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 Error loading Bitmap from MemoryStream up vote 1 down vote favorite I'm trying to load images (pdfs and Word documents) from a memory stream so I can manipulate them before they get added to a pdf. Whenever I try and load a bitmap or a GIF I get one of the dreaded GDI+ errors. This call... System.Drawing.Bitmap myImage = System.Drawing.Image.FromStream(docStream); Generates this error... System.Runtime.InteropServices.ExternalException occurred Message="A generic error occurred in GDI+." Source="System.Drawing" ErrorCode=-2147467259 StackTrace: at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) at System.Drawing.Image.FromStream(Stream stream) and this call... System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(docStream); Generates this error... System.ArgumentException occurred Message="Parameter is not valid." Source="System.Drawing" StackTrace: at System.Drawing.Bitmap..ctor(Stream stream) The code works for png, jpg and tif, but not for bmp or GIF. It's basically: MemoryStream docStream = GetMemoryStream(filePath); // The next line errors... System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(docStream); private MemoryStream GetMemoryStream(string file) { FileStream fs = null; Byte[] bytes; int len; fs = File.OpenRead(file); len = (int)fs.Length - 1;