0

I am trying to use Format16bppGrayScale, but I keep getting an 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;
        unsafe
        {
            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;
            }

        }
        bmpRGB.UnlockBits(bmpData);
        return bmpRGB;

    }

Update: Karsten's code

 private Bitmap GenerateDummy16bitImage(byte[] temp)
    {
        int i2 = 0;

        Bitmap b16bpp = new Bitmap(KeepWidth, KeepHeight, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);

        var rect = new Rectangle(0, 0, KeepWidth, KeepHeight);
        var bitmapData = b16bpp.LockBits(rect, ImageLockMode.WriteOnly, b16bpp.PixelFormat);
        // Calculate the number of bytes required and allocate them.
        var numberOfBytes = bitmapData.Stride * KeepHeight;
        var bitmapBytes = new short[KeepWidth * KeepHeight];
        // Fill the bitmap bytes with random data.
        var random = new Random();
        for (int x = 0; x < KeepWidth; x++)
        {
            for (int y = 0; y < KeepHeight; y++)
            {

                var i = ((y * KeepWidth) + x); // 16bpp

                // Generate the next random pixel color value.
                var value = (short)temp[i2]; //(short)random.Next(5);

                bitmapBytes[i] = value;         // GRAY
                i2++;
            }
        }
        // Copy the randomized bits to the bitmap pointer.
        var ptr = bitmapData.Scan0;
        Marshal.Copy(bitmapBytes, 0, ptr, bitmapBytes.Length);

        // Unlock the bitmap, we're all done.
        b16bpp.UnlockBits(bitmapData);

        return b16bpp;
    }
Axel
  • 29
  • 7
  • Which line throws the exception? I don't know if Format16bppGrayScale is supported. See [Set individual pixels in .NET Format16bppGrayScale image](https://stackoverflow.com/a/3499911/719186). – LarsTech Jun 22 '19 at 18:54
  • 1
    This format is not really supported. – TaW Jun 22 '19 at 18:59
  • All I am not getting the error in that function, but later on when the program tries to display it as a video . I know the problem is in that function because, It works fine when I set it up as Format24bppRgb. but I really want to use white - black , not any other colors. – Axel Jun 22 '19 at 19:12
  • taW, What does that mean "This format is not really supported"? Why would they give it to you as a option, if you cannot use it? – Axel Jun 22 '19 at 19:14
  • Right, that's exactly what I meant: it is in the format list but isn't supported, at least not with the normal commands. maybe they planned to support it one day but monitors never got around to support it either, so.. – TaW Jun 22 '19 at 19:28
  • Taw, is there anyway I can just get white-black colors? right now, I am see bright pink - dark green – Axel Jun 22 '19 at 21:36
  • _right now, I am see bright pink - dark green_ - Where do you see that?? – TaW Jun 23 '19 at 00:27
  • Did you see [Karsten's answer here](https://stackoverflow.com/questions/26721809/generate-16-bit-grayscale-bitmapdata-and-save-to-file) ? – TaW Jun 23 '19 at 00:35
  • Taw I see that in my video player – Axel Jun 23 '19 at 05:47
  • How can you see it when you get an exception?? – TaW Jun 23 '19 at 07:08
  • I can get it to work when I use Format24bppRgb. – Axel Jun 23 '19 at 13:30
  • Taw, I just tried Karsten's code and I am getting the same exception when the code goes to display it as a video in my video player. I also post to show the code sample i used from Karsten – Axel Jun 23 '19 at 14:07
  • .Net has no support for colour components larger than 1 byte. Simply divide it by 256 and use 8bpp grayscale. – Nyerguds Jul 06 '19 at 12:49

0 Answers0