Solved

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

Posted on 2013-11-11
1
863 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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