Solved

.Net C# A generic error occurred in GDI+.

Posted on 2013-11-11
1
844 Views
Last Modified: 2013-11-12
I get this error when converting a Bitmap image to a byte array. This code is used in both a desktop application and windows services and I only get the error while using the method in a service.

What Am I doing wrong?

            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(OutImgInfo.Width, OutImgInfo.Height, pFormat);
           

            //Create a BitmapData and Lock all pixels to be written   
            System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);

            //Copy the data from the byte array into BitmapData.Scan0   
            Marshal.Copy(NewArray, 0, bmpData.Scan0, NewArray.Length);

            //Unlock the pixels   
            bmp.UnlockBits(bmpData);

            // Need to remove GDI+ generic errors
            //System.Drawing.Bitmap tempBmp = new System.Drawing.Bitmap(bmp);


            using (MemoryStream ms = new MemoryStream() )
            {
                try 
	                {	        
		                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
                        bmp.Dispose();
	                }
                    catch (SystemException)
                    {
                    }                

                //Return the byte[]
                return ms.GetBuffer().ToArray<byte>();
            }

Open in new window

0
Comment
Question by:csalerno
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
csalerno earned 0 total points
ID: 39642284
My code, above the snippet, was not supporting 8-bit image format. I added the below instead of it falling thru to the default and the error was corrected.

                case 8:
                    pFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
                    break;
                default:
                    pFormat = System.Drawing.Imaging.PixelFormat.Format16bppGrayScale;
                    break;
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question