System :: Drawing :: Bitmap to unsigned char * [英] System::Drawing::Bitmap to unsigned char *

查看:329
本文介绍了System :: Drawing :: Bitmap to unsigned char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#.NET库,从相机抓取帧。我需要发送这些帧到一个本机应用程序,从 unsigned char * 的图像。

I have a C# .NET library that grabs frames from a camera. I need to send those frames to a native application that takes images from an unsigned char*.

我最初将框架设为 System :: Drawing :: Bitmap

到目前为止,我可以从位图中检索 byte [] $ c>。我的测试是用分辨率400 * 234的图像,我应该得到400 * 234 * 3字节,以获得到24bpp一个RGB图像要求。

So far I can retrieve a byte[] from the Bitmap. My test is done with an image of resolution 400*234, I should be getting 400*234*3 bytes to get to the 24bpp a RGB image requires.

但是,我得到一个大小为11948的 byte []

However, I'm getting a byte[] of size 11948.

这是我从 Bitmap 转换为 byte []

private static byte[] ImageToByte(Bitmap img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

:Drawing :: Bitmap 到RGB 无符号字符*

推荐答案

这需要使用lockBits方法,这里是一个代码示例:

This has to be done using the lockBits method, here is a code example:

    Rectangle rect = new Rectangle(0, 0, m_bitmap.Width, m_bitmap.Height);
    BitmapData bmpData = m_bitmap.LockBits(rect, ImageLockMode.ReadOnly,
        m_bitmap.PixelFormat);

    IntPtr ptr = bmpData.Scan0;
    int bytes = Math.Abs(bmpData.Stride) * m_bitmap.Height;
    byte[] rgbValues = new byte[bytes];
    Marshal.Copy(ptr, rgbValues, 0, bytes);
    m_bitmap.UnlockBits(bmpData);
    GCHandle handle = GCHandle::Alloc(rgbValues, GCHandleType::Pinned);
    unsigned char * data = (unsigned char*) (void*) handle.AddrOfPinnedObject();
    //do whatever with data

这篇关于System :: Drawing :: Bitmap to unsigned char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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