C ++获取从HBITMAP原始像素数据 [英] c++ Get raw pixel data from hbitmap

查看:2061
本文介绍了C ++获取从HBITMAP原始像素数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的使用P / Invoke电话,我想知道,如果有人能指导我如何从一个HBITMAP检索原始像素数据(无符号字符*)。

I am fairly new to using p/invoke calls and am wondering if someone can guide me on how to retrieve the raw pixel data (unsigned char*) from an hbitmap.

这是我的方案:

我加载 .NET位图 C#端对象和发送它的IntPtr到我的非托管C ++方法。有一次,我收到关于 C ++的HBITMAP PTR 的一面,我想访问位图'的像素数据。我已经作出从而重新presents原始像素数据从C#,但是我发现在C#中提取字节[]是相当缓慢的,它接受一个unsigned char型的方法*。这就是为什么我想在PTR位图派位图转换成一个byte []和发送,为我的C ++方法代替。

I am loading a .NET Bitmap object on the C# side and sending it's IntPtr to my unmanaged c++ method. Once I receive the hbitmap ptr on the C++ side, I would like to access the Bitmaps' pixel data. I already made a method that accepts an unsigned char* which represents the raw pixel data from c# however I found extracting the byte[] from the c# is fairly slow. This is why I want to send in the Bitmap ptr instead of converting the Bitmap into a byte[] and sending that to my C++ method.

C#code位图的IntPtr

C# code for getting Bitmap IntPtr

Bitmap srcBitmap = new Bitmap(m_testImage);
IntPtr hbitmap = srcBitmap.GetHbitmap();

C#code导入C ++方法

C# code for importing c++ method

[SuppressUnmanagedCodeSecurityAttribute()]
[DllImport("MyDll.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern int ResizeImage(IntPtr srcImg);

C ++方法,将收到HBITMAP处理程序

C++ method that will receive the Hbitmap handler

int Resize::ResizeImage(unsigned char* srcImg){
    //access srcImgs raw pixel data (preferably in unsigned char* format)
   //do work with that 
   return status;
}

问题:

1)因为我在IntPrt发送,可我的C ++方法的参数是一个无符号字符*?

1) Since I am sending in an IntPrt, can my C++ method parameter be an unsigned char* ?

2)如果没有,我如何从C访问位图的原始数据++?

2) If not, how can I access the bitmap's raw data from c++?

推荐答案

GetHbitmap 方法不检索像素数据。它会产生一个GDI的位图句柄类型 HBITMAP 的。非托管code会收到类型 HBITMAP 的参数。您可以从使用​​GDI调用获得的像素数据。但它不是在本身的原始像素。

The GetHbitmap method does not retrieve pixel data. It yields a GDI bitmap handle, of type HBITMAP. Your unmanaged code would receive that as a parameter of type HBITMAP. You can obtain the pixel data from that using GDI calls. But it is not, in itself, the raw pixels.

其实,我是pretty确保你攻击这个问题的错误的方式。你可能会朝这边来了,因为 GetPixel 的setPixel 是缓慢的。这是真的。事实上,他们的GDI等价也有同感。你需要做的就是使用的 LockBits 。这将允许您在整个像素数据在C#中以高效的方式运行。这个问题的一个很好的说明可以在这里找到: http://bobpowell.net/lockingbits.aspx 。需要注意的是,为了提高效率,这是一种类型的C#code的在不安全code和指针往往是最好的解决方案。

In fact, I'm pretty sure you are attacking this problem the wrong way. You are probably heading this way because GetPixel and SetPixel are slow. This quite true. Indeed, their GDI equivalents are too. What you need to do is to use LockBits. This will allow you to operate on the entire pixel data in C# in an efficient way. A good description of the subject can be found here: http://bobpowell.net/lockingbits.aspx. Note that, for efficiency, this is one type of C# code where unsafe code and pointers is often the best solution.

如果出于某种原因,您仍然希望使用C ++ code,那么你仍然可以使用像素数据来操作 LockBits ,以获得最简单的方法的指针的像素数据。这当然比非托管GDI等值容易得多。

If, for whatever reason, you still wish to operate on the pixel data using C++ code, then you can still use LockBits as the simplest way to get a pointer to the pixel data. It's certainly much easier than the unmanaged GDI equivalents.

这篇关于C ++获取从HBITMAP原始像素数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆