简体   繁体   English

访问位图的字节数组

[英]Access to bytes array of a Bitmap

1- In Windows CE, I have a Bitmap object in C#. 1-在Windows CE中,我在C#中有一个Bitmap对象。

2- I have a C function in an extern dll that expects as parameters the pointer to a bytes array that represents an image in RGB565 format, width and height. 2-我在extern dll中有一个C函数,该函数期望将指向以RGB565格式,宽度和高度表示的图像的字节数组的指针作为参数。 This function will draw on this array of bytes. 此函数将使用此字节数组。

So I need to pass the byte array pointer of the Bitmap object, but I can find a practical way to get this pointer. 因此,我需要传递Bitmap对象的字节数组指针,但是我可以找到一种实用的方法来获取该指针。 One way is convert this Bitmap into a bytes array using a memory stream or something else, but it will create a new bytes array, so I will keep in memory both object, the Bitmap and the bytes array, but I don't want it because the few available memory, that's why I need to access to the bytes array of the bitmap object, not create a new bytes array. 一种方法是使用内存流或其他方法将此位图转换为字节数组,但是它将创建一个新的字节数组,因此我将对象,位图和字节数组都保留在内存中,但我不希望这样做因为可用内存很少,所以我需要访问位图对象的bytes数组,而不是创建新的bytes数组。

Anyone can help me? 有人可以帮助我吗?

You can do something like this where image is your Bitmap: 您可以执行以下操作,其中图像是您的位图:

Rectangle area = (new Rectangle(0, 0, image.width, image.height));
BtimapData bitmapData = image.LockBits(area, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
stride = bitmapData.Stride;
IntPtr ptr = bitmapData.Scan0;

I understand that you don't want to copy RGB values of the Bitmap to another array but that is the best solution. 我知道您不想将位图的RGB值复制到另一个数组,但这是最好的解决方案。 The array is going to be in memory only while drawing in C code. 该数组仅在使用C代码绘制时才会在内存中。 I have used similar approach in Windows Professional 6 and it didn't introduce a lot of overhead. 我在Windows Professional 6中使用了类似的方法,但是并没有带来很多开销。 There are many FastBitmap implementations available. 有许多可用的FastBitmap实现。 You can check this question on stackoverflow or this implementation 您可以在stackoverflow或此实现上检查此问题

You can use unsafe code to acquire a bitmap data pointer. 您可以使用不安全的代码来获取位图数据指针。

Use the following code: 使用以下代码:

var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
var bmpData =
    bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
    bmp.PixelFormat);

IntPtr ptr = bmpData.Scan0;

byte* bmpBytes = (byte*)ptr.ToPointer();

where bmp is your Bitmap object. 其中bmp是您的Bitmap对象。

You also then can do the opposite: 然后,您也可以执行相反的操作:

ptr = new IntPtr(b);

bmpData.Scan0 = ptr;

thanks to the response of "NikolaD" and "oxilumin" I could solve my problem. 多亏了“ NikolaD”和“ oxilumin”的回应,我才能解决我的问题。

As I am using RGB565 the code was: 当我使用RGB565时,代码为:

imgMap.Image = new Bitmap(imgMap.Width, imgMap.Height, PixelFormat.Format16bppRgb565);
Rectangle area = (new Rectangle(0, 0, imgMap.Width, imgMap.Height));
BitmapData bitmapData = ((Bitmap)imgMap.Image).LockBits(area, ImageLockMode.ReadWrite, PixelFormat.Format16bppRgb565);
IntPtr ptrImg = bitmapData.Scan0;

where: imgMap is a PictureBox 其中:imgMap是一个PictureBox

thanks again 再次感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM