I'm unsure whether to post this in VB.net or Graphics. *
I have been messing with bitmap graphics for a while previously tried the "SetPixel" and "LockBits" methods.
SetPixel was way too slow for the intended use (graphics engine).
LockBits gave me issues with memory management.
I therefore ended up trying to create a bitmap from a MemoryStream (byte array).
My issue is that the setpixel function I created for it doesn't seem to update pixel values properly.
Maybe my bitmap header is not valid ? I've based it on this wiki article (Example 2 At Bottom): https://en.wikipedia.org/wiki/BMP_file_format
Hopefully someone can help with this or show me an efficient way of drawing bitmaps from byte arrays.
Thanks,
KBC
I have been messing with bitmap graphics for a while previously tried the "SetPixel" and "LockBits" methods.
SetPixel was way too slow for the intended use (graphics engine).
LockBits gave me issues with memory management.
I therefore ended up trying to create a bitmap from a MemoryStream (byte array).
My issue is that the setpixel function I created for it doesn't seem to update pixel values properly.
vb.net Code:
Dim Mybitmap As New Bitmap(1024, 768) Dim ArrB() As Byte Private Function CreateBMPv4Header(ByVal BmpWidth As Integer, ByVal BmpHeight As Integer) As Byte() Dim BytesInBMP As Integer = (BmpWidth * BmpHeight) * 4 Dim ByteArray(122 + BytesInBMP) As Byte Const s_ColorPlanes As Short = 1 Const s_BitsPerPixel As Short = 32 ' --- BMP HEADER '(0-2) ID Field ByteArray(0) = 66 : ByteArray(1) = 77 '(2-6) Total Size Array.Copy(BitConverter.GetBytes(ByteArray.Length), 0, ByteArray, 2, 4) '(6-10) Application Specific Array.Copy(BitConverter.GetBytes(0), 0, ByteArray, 6, 4) '(10-14) Start Of Pixel Data Array.Copy(BitConverter.GetBytes(122), 0, ByteArray, 10, 4) ' --- DIB HEADER '(14-18) DIB Header Size Array.Copy(BitConverter.GetBytes(108), 0, ByteArray, 14, 4) '(18-22) Width In Pixels Array.Copy(BitConverter.GetBytes(BmpWidth), 0, ByteArray, 18, 4) '(22-26) Height In Pixels Array.Copy(BitConverter.GetBytes(BmpHeight), 0, ByteArray, 22, 4) '(26-28) Number Of Color Planes Array.Copy(BitConverter.GetBytes(s_ColorPlanes), 0, ByteArray, 26, 2) '(28-30) Number Of Bits Per Pixel Array.Copy(BitConverter.GetBytes(s_BitsPerPixel), 0, ByteArray, 28, 2) '(30-34) Compression Method Array.Copy(BitConverter.GetBytes(3), 0, ByteArray, 30, 4) '(34-38) Raw Pixel Array Size Array.Copy(BitConverter.GetBytes(BytesInBMP), 0, ByteArray, 34, 4) '(38-42) Print Size Px/M Horizontal Array.Copy(BitConverter.GetBytes(1600), 0, ByteArray, 38, 4) '(42-46) Print Size Px/M Vertical Array.Copy(BitConverter.GetBytes(1000), 0, ByteArray, 42, 4) '(46-50) Color Palette Value Array.Copy(BitConverter.GetBytes(0), 0, ByteArray, 46, 4) '(50-54) Important Colors Array.Copy(BitConverter.GetBytes(0), 0, ByteArray, 50, 4) '(54-58) Red Channel Bit Mask ByteArray(54) = 0 : ByteArray(55) = 0 : ByteArray(56) = 255 : ByteArray(57) = 0 '(58-62) Green Channel Bit Mask ByteArray(58) = 0 : ByteArray(59) = 255 : ByteArray(60) = 0 : ByteArray(61) = 0 '(62-66) Blue Channel Bit Mask ByteArray(62) = 255 : ByteArray(63) = 0 : ByteArray(64) = 0 : ByteArray(65) = 0 '(66-70) Alpha Channel Bit Mask ByteArray(66) = 0 : ByteArray(67) = 0 : ByteArray(68) = 0 : ByteArray(69) = 255 '(70-74) Windows Color Space Endian ByteArray(70) = 32 : ByteArray(71) = 110 : ByteArray(72) = 105 : ByteArray(73) = 87 '(74-122) Pad Unused Info Until 122 For I = 74 To ByteArray.Length - 1 ByteArray(I) = 0 Next Return ByteArray End Function Public Sub SetPixel(ByVal Pixel As Point, ByVal PixelColor As Color) Dim ByteLocation As Integer = 122 + ((Pixel.Y * 1024) + (Pixel.X * 4)) ArrB(ByteLocation) = 255 'PixelColor.A ArrB(ByteLocation + 1) = 0 'PixelColor.R ArrB(ByteLocation + 2) = 0 'PixelColor.G ArrB(ByteLocation + 3) = 0 'PixelColor.B End Sub Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint For I = 13 To 533 SetPixel(New Point(I, 8), Color.White) Next Dim TStream As New MemoryStream(ArrB) Mybitmap = Bitmap.FromStream(TStream) Dim G As Graphics = e.Graphics G.DrawImage(Mybitmap, New Point(0, 0)) Mybitmap.Dispose() TStream.Dispose() End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ArrB = CreateBMPv4Header(1024, 768) End Sub
Maybe my bitmap header is not valid ? I've based it on this wiki article (Example 2 At Bottom): https://en.wikipedia.org/wiki/BMP_file_format
Hopefully someone can help with this or show me an efficient way of drawing bitmaps from byte arrays.
Thanks,
KBC