Format16bppGrayScale : a generic error occurred in gdi+ RRS feed

  • Question

  • I am trying to use Format16bppGrayScale, but I keep getting error: "a generic error occurred in gdi+". I have a monochromatic camera that is giving me a 16 bit value and I want to store it in Format16bppGrayScale so I can keep my image from the camera. the 16 bit value is coming in an array that is two array indexes per one 16 bit value:

    public  Bitmap ByteToImage1(byte[] blob)
                Bitmap bmpRGB = new Bitmap(KeepWidth, KeepHeight, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
                Rectangle rect = new Rectangle(0, 0, KeepWidth, KeepHeight);
                BitmapData bmpData = bmpRGB.LockBits(rect, ImageLockMode.WriteOnly, bmpRGB.PixelFormat);
                int padding = bmpData.Stride - 3 * KeepWidth;
                    int i = 0;
                    byte* ptr = (byte*)bmpData.Scan0;
                    for( int y= 0; y < KeepHeight; y++)
                            for(int x = 0; x < KeepWidth; x++)
                            ptr[1] = blob[i+1];
                            ptr[0] = blob[i];
                            i = i + 2;
                            ptr += 2;
                        ptr += padding;
                return bmpRGB;

    Saturday, June 22, 2019 6:30 PM

All replies

  • Try replacing 3 with 2.

    But, the image format does not seem to be fully supported: https://social.msdn.microsoft.com/Forums/vstudio/en-US/10252c05-c4b6-49dc-b2a3-4c1396e2c3ab. Maybe you can use an alternative format (grey colours are available in other image formats).

    Saturday, June 22, 2019 7:30 PM
  • I tried the Try replacing 3 with 2. but got the same error 

    what do you mean alternative format ?

    Saturday, June 22, 2019 10:01 PM
  • can anyone help me?
    Monday, June 24, 2019 4:02 AM
  • Hi Btb4198,

    Thank you for posting here.

    Based on my test,  I could not reproduce your problem. I think it is related to keepwith ,keepheight and blob. If you want to convert imge to byte array,  you could look at the following link.


    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 24, 2019 5:09 AM
  • I gave you code using Marshal.Copy that worked.  I entered and tested it before I posted it.  Why are you trying to redo it with unsafe pointer stuff?

    And as I have tried to tell you SEVERAL TIMES, your 16-bit pixels are 2 byes wide.  You keep trying to make them 3 bytes, but I would think you could figure out that 16 bits makes 2 bytes.

    And I also told you that, also the system will let you create a 16-bit grayscale image, few of the drawing functions will work with that format.  You can't display it.  You can't draw on it.

    You also still haven't answered the basic question: What do you expect to do with this bitmap after you create it?  What is the end purpose?

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, June 24, 2019 5:31 AM