Intermittent System.Drawing / GDI + "General" error

I have an ASPX page that displays everything in its line as vertical text and returns PNG. It works great.

I have exactly one client that has encountered a problem. Every few days, the page stops working and throws a terrible GDI + "general" error.

Error: System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(Stream stream, ImageFormat format) ... 

I do not know why the error occurs, or why it eventually goes away. I was able to download the test ASPX file to my installation, which used the same code with several options to see if I could identify the problem. I found that if I changed ImageFormat from Png to Jpeg, the error went away.

I could, apparently, change the product for rendering JPEG instead of PNG. However, I have no way of knowing if this will ultimately lead to intermittent errors, as of now.

Does anyone know what might cause such a problem? Thank you The code is below.

Refresh . The client server is the core of Windows Server 2008 R2 with IIS 7.5, and my application runs on .NET 4.0.

  protected void Page_Load(object sender, EventArgs e) { byte[] image = GetImageBytes(this.Text); if (image != null) { Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "image/png"; Response.OutputStream.Write(image, 0, image.Length); } } private byte[] GetImageBytes(string text) { var font = new Font("Tahoma", 11, FontStyle.Bold, GraphicsUnit.Pixel); // Create an image the size of the text we are writing Bitmap img = new Bitmap(1,1); var graphics = Graphics.FromImage(img); int width = (int)graphics.MeasureString(text, font).Width; int height = (int)graphics.MeasureString(text, font).Height; img = new Bitmap(img, new Size(width, height)); // Draw the text onto the image graphics = Graphics.FromImage(img); graphics.Clear(Color.Transparent); graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.DrawString(text, font, new SolidBrush(Color.Black), 0, 0); graphics.Flush(); // Rotate the image to be vertical img.RotateFlip(RotateFlipType.Rotate270FlipNone); var stream = new System.IO.MemoryStream(); img.Save(stream, ImageFormat.Png); stream.Position = 0; return stream.ToArray(); } 
+4
source share
1 answer

I would say flash can be a problem

Forces to execute all pending graphic operations and immediately returns them, without waiting for the completion of operations.

This item is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

can you try this to find out if you have a problem?

  private byte[] GetImageBytes(string text) { using (var font = new Font("Tahoma", 20, FontStyle.Bold, GraphicsUnit.Pixel)) using (var img = new Bitmap(1, 1)) { int width; int height; using (var graphics = Graphics.FromImage(img)) { width = (int)graphics.MeasureString(text, font).Width; height = (int)graphics.MeasureString(text, font).Height; } using (var realImg = new Bitmap(img, new Size(width, height))) { using (var graphics = Graphics.FromImage(realImg)) { graphics.Clear(Color.Transparent); graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.DrawString(text, font, new SolidBrush(Color.Black), 0, 0); } realImg.RotateFlip(RotateFlipType.Rotate270FlipNone); using (var stream = new System.IO.MemoryStream()) { realImg.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Position = 0; return stream.ToArray(); } } } } 
+3
source

Source: https://habr.com/ru/post/1490967/


All Articles