Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-11
1
Medium Priority
?
886 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

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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