【发布时间】:2019-08-26 10:15:36
【问题描述】:
我的程序陷入了死胡同。我在内存中有一个由 DIB 位图的 RGB 值组成的简单数组(没有BITMAPFILEHEADER)。该数组是在 C++ 中生成的,但我尝试在 VB.NET 中显示它。我不想使用 GDI+,因为我需要原始速度。
这是我的代码(文件中的图像没有标题,宽度:1920 和高度:100,24 位,总大小 6220804):
Dim bData As Byte()
Dim br As BinaryReader = New BinaryReader(File.OpenRead("img1.bmp"))
bData = br.ReadBytes(br.BaseStream.Length) 'no headers just raw data
Dim g As Graphics = Me.CreateGraphics() 'System.Drawing.Graphics.FromImage(bmp) 'or PictureBox1.CreateGraphics()
Dim hdc As IntPtr = g.GetHdc()
Dim bmi As New BITMAPINFO
bmi.bmiheader = New BITMAPINFOHEADER
'Now we fill up the bmi (Bitmap information variable) with all the necessary data
bmi.bmiheader.biSize = 40 'Size, in bytes, of the header (always 40)
bmi.bmiheader.biPlanes = 1 'Number of planes (always one)
bmi.bmiheader.biBitCount = 24 'Bits per pixel (always 24 for image processing)
bmi.bmiheader.biCompression = 0 'Compression: none or RLE (always zero)
bmi.bmiheader.biWidth = 1920
bmi.bmiheader.biHeight = 100
bmi.bmiheader.biSizeImage = 6220804
Dim memHDC As IntPtr = CreateCompatibleDC(hdc)
StretchDIBits(memHDC, 0, 0, 1920, 100, 0, 0, 1920, 100, bData, bmi, 0, 13369376) ' Copy RGB values on an intermediary HDC
BitBlt(hdc, 0, 0, 1920, 100, memHDC, 0, 0, 13369376) 'Print directly from the memHDC
这是我的结构:
<StructLayout(LayoutKind.Sequential)>
Structure RGBQUAD
Public rgbBlue As Byte
Public rgbGreen As Byte
Public rgbRed As Byte
Public rgbReserved As Byte
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Class BITMAPINFOHEADER
Public biSize As Int32
Public biWidth As Int32
Public biHeight As Int32
Public biPlanes As Int16
Public biBitCount As Int16
Public biCompression As Int32
Public biSizeImage As Int32
Public biXPelsPerMeter As Int32
Public biYPelsPerMeter As Int32
Public biClrUsed As Int32
Public biClrImportant As Int32
End Class
<StructLayout(LayoutKind.Sequential)>
Private Structure BITMAPINFO
Dim bmiheader As BITMAPINFOHEADER
Dim bmiColors As RGBQUAD
End Structure
我测试了几乎所有可能的变量组合、HDC 和图形。没有任何作用!我哪里失败了?
注意:StretchDIBits 和 BitBlt 似乎成功了
【问题讨论】:
-
你在哪里将数据分配给
bmp? -
我不使用 bmp。这是茶的juat。我想得到一个hdc然后在那个hdc上bitblt然后在picturebox1中使用bmp
-
根本没有犯错的余地
-
你有很多硬编码的图像属性(宽度、高度等)。您确定这些值对于数据数组是正确的吗?您是否还确定字节数组不以 DIB 标头开头?您会在没有某种方式确定它代表什么的情况下检索此类数据,这似乎很奇怪。如果您确定数据,您是否尝试过使用 Lockbits 类型的方法(或者在最坏的情况下为 Bitmap.SetPixel)将数据写入位图以确定是否可以创建有效的位图图像?一旦你有一个已知的方法工作,然后尝试 StretchDIBits 和 BitBlt。
-
这只是一个测试场景。稍后我将在文件或数组中包含 BITMAPINFOHEADER。我不想使用 SetPixel 因为它很慢。我现在正在重新测试数据以确保它没有损坏。此外,当创建此数组时,我在结构中使用了高度的正值。我不知道这是否会影响 vb.net 代码。