【发布时间】:2020-05-30 13:48:06
【问题描述】:
我正在尝试向名为 GL 的 OpenGL 控件显示位图图像(我使用 SharpGL 作为包装器)。在实践中,文本字符串被写入 GDI+ 图片框,从中获取位图。 GLBitmap 函数需要一个 Byte() 数组作为输入;我将位图转换为 Byte() 数组。似乎不起作用,因为我在屏幕上看到一堆点。我还将 PictureBox 图像保存到磁盘上的 Bmp 文件并交叉检查它是否具有所需的内容。所以,Bmp 图像在这里似乎不是问题。
实际的 OpenGL 显示
从以下代码创建的 Bmp 文件
想知道我做错了什么。有人会有建议吗?提前致谢。
VB.Net 代码如下所示。
' Create Bitmap & Graphics context for string
'
' iWidth and iHeight are the dimensions of the bitmap;
' they are a power of 2.
Dim SharpBMap As Bitmap = New Bitmap(iWidth, iHeight)
Dim SharpGraf As Graphics = Graphics.FromImage(SharpBMap)
' Draw text string to SharpGraf PictureBox
'
SharpGraf.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
SharpGraf.Clear(Color.White)
SharpGraf.DrawString(TxtStr, CurrFont, CurrBrush, 0, 0, CurrFormat)
' OutSharpGL is the Picture box to which the text string is sent
OutSharpGL.Image = SharpBMap
' Save the bitmap to disk: when the file is viewed, the image is Ok.
SharpBMap.Save(FilNameStr)
' Set the color
Gl.Color(1.0f, 1.0f, 1.0f)
' Set the Raster position
Gl.RasterPos(0, 0)
' Transfer the Bitmap
Gl.Bitmap(iWidth, iHeight, 0.0f, 0.0f, 0.0f, 0.0f, BitmapToByte(SharpBMap))
' Function to convert a Bitmap to a Byte() array
Friend Function BitmapToByte(ByRef Bmp As Bitmap) As Byte()
Dim converter As New ImageConverter()
Return DirectCast(converter.ConvertTo(Bmp, GetType(Byte())), Byte())
End Function
【问题讨论】:
-
取
Bitmap.LockBits()'s 生成的BitmapData字节数组。那是图像数据。请参阅此处的方法:Analyze colors of an Image,您在哪里看到Marshal.Copy(imageData.Scan0, buffer, 0, buffer.Length),buffer是您的家伙。如那里所述,更喜欢 32Bit 格式,因此也可以采用生成新的 32BitArgb 位图的方法。