A Generic Error Occurred In Gdi Bitmap Save Png
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss c# bitmap save a generic error occurred in gdi+ the workings and policies of this site About Us Learn more about
A Generic Error Occurred In Gdi C# Image Save
Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow
A Generic Error Occurred In Gdi Image Save Vb Net
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
Gdiplus Bitmap Save Png
other. Join them; it only takes a minute: Sign up A generic error occurred in GDI+, JPEG Image to MemoryStream up vote 198 down vote favorite 38 This seems to be a bit of an infamous error all over the web. So much so that I have been unable to find an answer to my problem as my how to solve a generic error occurred in gdi+ scenario doesn't fit. An exception gets thrown when I save the image to the stream. Weirdly this works perfectly with a png but gives the above error with jpg and gif which is rather confusing. Most similar problem out there relate to saving images to files without permissions. Ironically the solution is to use a memory stream as I am doing.... public static byte[] ConvertImageToByteArray(Image imageToConvert) { using (var ms = new MemoryStream()) { ImageFormat format; switch (imageToConvert.MimeType()) { case "image/png": format = ImageFormat.Png; break; case "image/gif": format = ImageFormat.Gif; break; default: format = ImageFormat.Jpeg; break; } imageToConvert.Save(ms, format); return ms.ToArray(); } } More detail to the exception. The reason this causes so many issues is the lack of explanation :( System.Runtime.InteropServices.ExternalException was unhandled by user code Message="A generic error occurred in GDI+." Source="System.Drawing" ErrorCode=-2147467259 StackTrace: at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(Stream stream, ImageFormat format) at Caldoo.Infrastructure.PhotoEditor.ConvertImageToByteArray(Image imageToConvert) in C:\Users\Ian\SVN\Caldoo\Caldoo.Coordinator\PhotoEditor.cs:line 139 at Caldoo.Web.Controllers.PictureController.Croppable() in C:\Users\Ian\SVN\Caldoo\Caldoo.Web\Controllers\PictureController.cs:line 132 at lambda_method(ExecutionScope , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.
> .NET Framework Class Libraries Question 0 Sign in to vote Hello All,I a generic error occurred in gdi in c# windows application was just going to play around by generating some bitmaps programatically.I a generic error occurred in gdi+. asp.net c# started off with this simple example, expecting everything to go smoothly, but have run into system.runtime.interopservices.externalexception (0x80004005): a generic error occurred in gdi+. a strange error.The following code is by no means good, just something simple and complete I would expect to work:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream System.Windows.Forms;namespace BitmapOutput{ public partial class Form1 : Form { ///
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 http://www.hanselman.com/blog/TheWeeklySourceCode50ALittleOnAGenericErrorOccurredInGDIAndTroubleGeneratingImagesOnWithASPNET.aspx 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 https://weblog.west-wind.com/posts/2006/oct/19/common-problems-with-rendering-bitmaps-into-aspnet-outputstream 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 a generic 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 a generic error 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.Fr
- 1078 Comments - 13790 RSS Feed Rick's Sites Rick's GitHub Projects Rick's FoxPro Web Log West Wind Message Board CodePaste.net GeoCrumbs.net Rick's Products Markdown Monster WebSurge Html Help Builder Web Monitor Find this content useful? Consider making a small donation to show your support. Tweets by @RickStrahl Common Problems with rendering Bitmaps into ASP.NET OutputStream October 19, 2006 - from Maui, Hawaii 31 comments Tweet Raise your hand if you’ve loaded an image from a Resource (or a BLOB database field): Bitmap bm2 = this.GetGlobalResourceObject("Resources", "_BitMap") as Bitmap; Response.ContentType = "image/jpeg"; bm2.Save(Response.OutputStream, ImageFormat.Jpeg); Response.End(); and finding out that you can’t save the image! You get a not so happy error: A generic error occurred in GDI+. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.Source Error: Line 41: Line 42: Response.ContentType = "image/jpeg";Line 43: bm2.Save(Response.OutputStream, ImageFormat.Jpeg);Line 44: Response.End();Line 45: Source File: c:\projects2005\Articles\Internationalization\Test.aspx.cs Line: 43 I know this isn’t the first time I’ve done this and scratched my head and go WTF. The bitmap’s there but it won’t save, not to the output stream or to disk (even with permissions for it). So what gives? The documentation is not real clear on this, although MSDN has a little blurp on not saving to the same stream: Remarks You should avoid saving an image to the same stream that was used to construct it. Doing so might damage the stream. I don’t think this applies here – the Bitmap resource comes from a database field’s raw byte stream into a memory stream from which the image is loaded in my custom Resource Provider out of a database. The original stream is long gone, but maybe that’s just the problem… Whatever the problem is, the code above doesn’t work. The solution is to create a second bitmap and basically copy the image into a new Bitmap object and then save to the outputstream: Bitmap bm2 = this.GetGlobalResourceObject("Resources", "_BitMap") as Bitmap; Bitmap bm3 = new Bitmap(bm2); Response.ContentType = "image/jpeg"; bm3.Save(Response.OutputStream, ImageFormat.Jpeg); bm2.Dispose(); bm3.Dispose();