如何使用c#将没有BMP头的字节数组RGB图像转换为位图 [英] how to convert byte array RGB image without BMP header to bitmap using c#
本文介绍了如何使用c#将没有BMP头的字节数组RGB图像转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们的程序将WEBP图像格式转换为没有标题的rgb字节数组图像。我希望通过将此字节数组转换为位图图像格式来显示图片框中的这些字节。
我该怎么做?
我的图像是格式为24bpprgb格式的彩色图像...
Our program convert WEBP image format to rgb byte array image without header. and I wanna show these bytes in picture box by convert this byte array to bitmap image format..
how can I do this ?
My image is color image in format24bpprgb format...
推荐答案
由于您没有标题信息,您必须知道源位图的尺寸并创建匹配的位图,然后锁定位并复制根据输入图像格式的原始字节。下面的代码片段可以帮助您:
sourceBitmapclass是一个元容器,您必须从源知识中获取信息。
Since you have no header information, you have to know the dimensions of your source bitmap and create a matching bitmap, then you lock the bits and copy the raw bytes according to the format of your input image. The following snippet should get you on the way:
The sourceBitmap "class" is a meta-container, you have to obtain your information from your source knowledge.
Bitmap myBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height, PixelFormat.Format24bppRgb);
BitmapData myData = myBitmap.LockBits(sourceBitmap.Dimensions, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int sStride = sourceBitmap.Stride;
int dStride = myData.Stride;
// copy the RGB bits from your raw source bitmap to the buffer of
// myBitmap, myData.Scan0 is the first byte of the target buffer.
unsafe
{
byte* src = <your pointer="" to="" the="" raw="" data="">;
byte* dst = (byte*)myData.Scan0.ToPointer();
byte* s;
byte* d;
for(int row = 0; row < sourceBitmap.Height; row++)
{
for(int col = 0; col < sourceBitmap.Width; col++)
{
s = src + row * sStride + col * 3; //24bpp, 3 bytes per pixel
d = dst + row * dStride + col * 3; //24bpp, 3 bytes per pixel
d[0] = s[0];
d[1] = s[1];
d[2] = s[2];
}
}
}
myBitmap.UnlockBits(myData);
</your>
现在您可以使用myBitmap显示图像。
Now you can display the image using myBitmap.
这篇关于如何使用c#将没有BMP头的字节数组RGB图像转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文