Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15236

VS 2019 Bitmaps From Stream & Bitmap Headers

$
0
0
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.

vb.net Code:
  1. Dim Mybitmap As New Bitmap(1024, 768)
  2.     Dim ArrB() As Byte
  3.  
  4.     Private Function CreateBMPv4Header(ByVal BmpWidth As Integer, ByVal BmpHeight As Integer) As Byte()
  5.  
  6.         Dim BytesInBMP As Integer = (BmpWidth * BmpHeight) * 4
  7.         Dim ByteArray(122 + BytesInBMP) As Byte
  8.  
  9.         Const s_ColorPlanes As Short = 1
  10.         Const s_BitsPerPixel As Short = 32
  11.  
  12.         ' --- BMP HEADER
  13.  
  14.         '(0-2) ID Field
  15.         ByteArray(0) = 66 : ByteArray(1) = 77
  16.         '(2-6) Total Size
  17.         Array.Copy(BitConverter.GetBytes(ByteArray.Length), 0, ByteArray, 2, 4)
  18.         '(6-10) Application Specific
  19.         Array.Copy(BitConverter.GetBytes(0), 0, ByteArray, 6, 4)
  20.         '(10-14) Start Of Pixel Data
  21.         Array.Copy(BitConverter.GetBytes(122), 0, ByteArray, 10, 4)
  22.  
  23.         ' --- DIB HEADER
  24.  
  25.         '(14-18) DIB Header Size
  26.         Array.Copy(BitConverter.GetBytes(108), 0, ByteArray, 14, 4)
  27.         '(18-22) Width In Pixels
  28.         Array.Copy(BitConverter.GetBytes(BmpWidth), 0, ByteArray, 18, 4)
  29.         '(22-26) Height In Pixels
  30.         Array.Copy(BitConverter.GetBytes(BmpHeight), 0, ByteArray, 22, 4)
  31.         '(26-28) Number Of Color Planes            
  32.         Array.Copy(BitConverter.GetBytes(s_ColorPlanes), 0, ByteArray, 26, 2)
  33.         '(28-30) Number Of Bits Per Pixel
  34.         Array.Copy(BitConverter.GetBytes(s_BitsPerPixel), 0, ByteArray, 28, 2)
  35.         '(30-34) Compression Method
  36.         Array.Copy(BitConverter.GetBytes(3), 0, ByteArray, 30, 4)
  37.         '(34-38) Raw Pixel Array Size
  38.         Array.Copy(BitConverter.GetBytes(BytesInBMP), 0, ByteArray, 34, 4)
  39.         '(38-42) Print Size Px/M Horizontal
  40.         Array.Copy(BitConverter.GetBytes(1600), 0, ByteArray, 38, 4)
  41.         '(42-46) Print Size Px/M Vertical
  42.         Array.Copy(BitConverter.GetBytes(1000), 0, ByteArray, 42, 4)
  43.         '(46-50) Color Palette Value
  44.         Array.Copy(BitConverter.GetBytes(0), 0, ByteArray, 46, 4)
  45.         '(50-54) Important Colors
  46.         Array.Copy(BitConverter.GetBytes(0), 0, ByteArray, 50, 4)
  47.         '(54-58) Red Channel Bit Mask
  48.         ByteArray(54) = 0 : ByteArray(55) = 0 : ByteArray(56) = 255 : ByteArray(57) = 0
  49.         '(58-62) Green Channel Bit Mask
  50.         ByteArray(58) = 0 : ByteArray(59) = 255 : ByteArray(60) = 0 : ByteArray(61) = 0
  51.         '(62-66) Blue Channel Bit Mask
  52.         ByteArray(62) = 255 : ByteArray(63) = 0 : ByteArray(64) = 0 : ByteArray(65) = 0
  53.         '(66-70) Alpha Channel Bit Mask
  54.         ByteArray(66) = 0 : ByteArray(67) = 0 : ByteArray(68) = 0 : ByteArray(69) = 255
  55.         '(70-74) Windows Color Space Endian
  56.         ByteArray(70) = 32 : ByteArray(71) = 110 : ByteArray(72) = 105 : ByteArray(73) = 87
  57.         '(74-122) Pad Unused Info Until 122
  58.         For I = 74 To ByteArray.Length - 1
  59.             ByteArray(I) = 0
  60.         Next
  61.  
  62.         Return ByteArray
  63.  
  64.     End Function
  65.  
  66.     Public Sub SetPixel(ByVal Pixel As Point, ByVal PixelColor As Color)
  67.         Dim ByteLocation As Integer = 122 + ((Pixel.Y * 1024) + (Pixel.X * 4))
  68.         ArrB(ByteLocation) = 255 'PixelColor.A
  69.         ArrB(ByteLocation + 1) = 0 'PixelColor.R
  70.         ArrB(ByteLocation + 2) = 0 'PixelColor.G
  71.         ArrB(ByteLocation + 3) = 0 'PixelColor.B
  72.     End Sub
  73.  
  74.     Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
  75.  
  76.         For I = 13 To 533
  77.             SetPixel(New Point(I, 8), Color.White)
  78.         Next
  79.  
  80.         Dim TStream As New MemoryStream(ArrB)
  81.         Mybitmap = Bitmap.FromStream(TStream)
  82.  
  83.         Dim G As Graphics = e.Graphics
  84.         G.DrawImage(Mybitmap, New Point(0, 0))
  85.  
  86.         Mybitmap.Dispose()
  87.         TStream.Dispose()
  88.  
  89.     End Sub
  90.  
  91.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  92.         ArrB = CreateBMPv4Header(1024, 768)
  93.     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

Viewing all articles
Browse latest Browse all 15236

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>