0

I'm having a problem creating a bitmap from a byte array in vb.net.
I get the pixel data from a GoPro as 1 bit per pixel data.
It looks like this:

0 0 0 0 0 0 0 0 1 1 5D 6C 63 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 FF FC 4 0 1C 70 71 C1 0 4 E 0 36 DA DB 61 77 74 4 0 36 D8 DB 63 77 74 11 0 36 D8 DB 63 77 74 E 0 36 DA DB 63 77 74 20 80 36 D8 DB 61 77 74 9F 20 1C 70 71 C1 0 4 40 40 0 0 0 1 FF FC 3F 80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 FF FF FF FF FF FF FF F0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 C 36 3 E3 F0 0 0 0 C F6 7 FB FC 0 0 0 C C0 6 1B C 0 0 0 D 80 0 1B C 0 0 0 F 80 3 FB C 0 0 0 E 0 3 E3 C 0 0 0 D 80 6 3 C 0 0 0 D C0 6 1B C 0 0 0 C C0 6 1B C 0 0 0 C 30 3 E3 F0 0 0 0 C 30 3 E3 F0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 FF FF FF FF FF FF FF F0 FF FF FF FF FF FF BF F0 F0 7 FF FF FF FB BB F0 E0 3 9F E0 7F FD F7 F0 E7 3 1F E0 7F FF 1F F0 E7 2 1F E7 FF FE EF F0 E7 2 1F E7 FF F2 C9 F0 E0 2 1F E7 FF FE 8F F0 E0 2 1F E7 FF FF 1F F0 E0 3 1F E7 FF FD F7 F0 E0 3 9F E7 FF FB BB F0 F0 7 FF FF FF FF BF F0 FF FF FF FF FF FF FF F0 FF FF FF FF FF FF FF F0 0

The first 15 bytes are the repetition of the request. This and the last byte are cut off

With the following code I convert the data into a bitmap:

Private Function GetBitmap(lcdContend) As Bitmap
    Dim lcdBytes(599) As Byte
    Dim split = lcdContend.Split(" ")

    For lci = 0 To 599
        lcdBytes(lci) = Convert.ToByte(split(lci + 15), 16) Xor 255
    Next

    Dim img = New Bitmap(64, 75)

    Dim bmd As BitmapData = img.LockBits(
       New Rectangle(0, 0, img.Width, img.Height),
       ImageLockMode.WriteOnly,
       PixelFormat.Format1bppIndexed)

    Marshal.Copy(lcdBytes, 0, bmd.Scan0, lcdBytes.Length)

    img.UnlockBits(bmd)

    'img.RotateFlip(RotateFlipType.Rotate180FlipX)

    Return img
End Function

The bitmap is 64px wide and 75px high and is 600 byte in size I then load the bitmap into a PictureBox. The result looks like this: Error display

The whole picture is shifted 4 pixels to the left. If I rotate and flip the image, the whole picture is also shifted 4 pixels to the left. flipped How do I get the missing 4 pixels column on the Image? I've tried so much, including converting to 24bpp. Everything results in the same result

PS: The data is okay, I converted it into bits by hand and drew a bitmap from it by hand. All the data is there.

I thank you!

sepp89117
  • 11
  • 2
  • 1
    You pasted here 622 bytes (and also mangled, there are unreadable chars among the text). If you skip 15 and take 600, you have `6 * 8 = 48` pixels that are left alone. Check the data you posted, the data you received and why on earth do you get a string and not a byte array to begin with? Or, are you reading the output as a string? – Jimi Dec 06 '20 at 14:57
  • I get the data from some kind of Arduino through the serial port. Yes, I read this as a string. I split this based on the spaces. The result is 616 bytes (after converting to byte). The first 15 bytes are skipped and the last is cut off. 600 bytes á 8 bit = 4800px. The picture has 64 x 75 pixels = 4800px. – sepp89117 Dec 06 '20 at 15:46
  • You pasted **622** bytes here. – Jimi Dec 06 '20 at 15:50
  • Thank you for your answer. OK. I don't know where the error is with the pasted bytes. When I step through the program, I see that I get 616 strings in "split". After the for loop I have 600 bytes in "lcdBytes". I converted this into bits on a trial basis and drew a bitmap by hand. The result is a perfect bitmap. Or am I misunderstanding something else? – sepp89117 Dec 06 '20 at 15:56
  • Again: you pasted **622** bytes. Thus this data is not correct (plus, as mentioned, you also pasted in unprintable chars). To you need help fixing the - apparently - wrong data you pasted in the question? Why don't you copy it and try to split it? BTW, read bytes as bytes, not strings. And, possible, just the bytes you need to reproduce the image. – Jimi Dec 06 '20 at 16:05
  • Sorry, I don't know where the error was while copying the bytes. Now it's 616 bytes. My mistake – sepp89117 Dec 06 '20 at 16:19
  • I think your problem is simply that the image is 60 pixels wide, not 64. 60 pixels at 1bpp needs (a minimum of) 7.5 bytes to store. Obviously the Arduino can't send half a byte, so it sends the 8th byte with the 4 low order bits set to 0. Even if it didn't clear them, it's safe to ignore them if you're happy that the width is indeed 60 pixels. – stevec Dec 06 '20 at 16:28
  • Hello Stevec, thanks for the food for thought. The display is really only 60px in width. Thank you! I could have looked for a long time ... – sepp89117 Dec 06 '20 at 16:38
  • `Dim img = New Bitmap(64, 75)` - This makes a 32bpp bitmap though. You need to specify the pixel format in the constructor. And, yea, you need to keep the stride (bytes per line) versus the width (pixels per line) into account. – Nyerguds Dec 08 '20 at 08:45
  • Everything works normally. I reduced the width to 60px. That's right. The bitmap data has PixelFormat.Format1bppIndexed. – sepp89117 Dec 09 '20 at 10:10

1 Answers1

1

The display is only 60px in width. My mistake. So everything is right. Thanks stevec

sepp89117
  • 11
  • 2