Solved

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

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

Accepted Solution

by:
csalerno earned 0 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now