C#将IntPtr转换为位图

时间:2018-02-14 02:21:54

标签: c#

我正在尝试从指纹设备SDK获取位图图像。 SDK为预览图像提供了一个结构,我无法将其转换为Bitmap:

public struct IBSU_ImageData
{
    /* Pointer to image buffer.  If this structure is supplied by a callback function, this pointer 
     * must not be retained; the data should be copied to an application buffer for any processing
     * after the callback returns. */
    public IntPtr Buffer;

    /* Image horizontal size (in pixels). */
    public uint Width;

    /* Image vertical size (in pixels). */
    public uint Height;

    /* Horizontal image resolution (in pixels/inch). */
    public double ResolutionX;

    /* Vertical image resolution (in pixels/inch). */
    public double ResolutionY;

    /* Image acquisition time, excluding processing time (in seconds). */
    public double FrameTime;

    /* Image line pitch (in bytes).  A positive value indicates top-down line order; a negative 
     * value indicates bottom-up line order. */
    public int Pitch;

    /* Number of bits per pixel. */
    public byte BitsPerPixel;

    /* Image color format. */
    public IBSU_ImageFormat Format;

    /* Marks image as the final processed result from the capture.  If this is FALSE, the image is
     * a preview image or a preliminary result. */
    public bool IsFinal;

    /* Threshold of image processing. */
    public uint ProcessThres;
}

public enum IBSU_ImageFormat
{
    IBSU_IMG_FORMAT_GRAY,                                    /* Gray scale image. */
    IBSU_IMG_FORMAT_RGB24,                                   /* 24 bit RGB color image. */
    IBSU_IMG_FORMAT_RGB32,                                   /* True color RGB image. */
    IBSU_IMG_FORMAT_UNKNOWN                                  /* Unknown format. */
}

我尝试过使用Image.FromHbitmap(imageData)但无法正常工作。

我调试并提供IBSU_ImageData内容:

Buffer : 0x1741d020
Width : 1600
Height : 1500
ResolutionX : 500
ResolutionY : 500
FrameTime : 0.100421
Pitch : -1600
BitsPerPixel : 8
Format : IBSU_IMG_FORMAT_GRAY
IsFinal : false
ProcessThres : 2

1 个答案:

答案 0 :(得分:0)

您拥有所需的所有数据。只需根据该数据构建图像。我wrote a function before可以从字节数组和相关信息构建Bitmap。

您可以使用Marshal.Copy将缓冲区内容从Buffer指针复制到pitch * height个字节的数组中。不过,你只需要小心负音调;它表示BMP风格的自下而上的线条。

public static Bitmap GetBitmapFromIBSU(IBSU_ImageData imageData)
{
    // Can't handle this.
    if (imageData.Format == IBSU_ImageFormat.IBSU_IMG_FORMAT_UNKNOWN)
        throw new NotSupportedException();
    Byte[] data = new byte[imageData.Width * Math.Abs(imageData.Pitch)];
    Marshal.Copy(imageData.Buffer, data, 0, data.Length);
    // Write this function yourself. They're both enums so a simple switch-case should do.
    // GRAY probably needs to be seen as indexed 8bpp; then you need to give a generated
    // 256-entry black-to-white colour fade palette to BuildImage in the next step.
    PixelFormat format = ConvertPixelformat(imageData.Format);
    // The function I linked in the description.
    Bitmap image = BuildImage(data, imageData.Width, imageData.Height, imageData.Pitch, format, null, null);
    bitmap.SetResolution(imageData.ResolutionX, imageData.ResolutionY);
    return bitmap;
}