这段时间处理打印问题,需要得到一个黑白的位图文件的颜色信息。 目前只发现使用Win32的API: GetBitmapBits函数可以实现。但经过N次的试验后,总是失败。
Win32 API GetBitmapBits函数的定义:
下面是我的代码:
在.net 中定义的拖管方法:
其中的第一个参数 IntPtr 我是调用Image.GetHbitmap()取得,但取到的值都是很大的整型,感觉不正常。
调用托管方法
以上代码总是出错,找不到原因,现在有以下几个问题请教
1、以上代码在哪个地方错了?
2、GetBitmapBits中的最后一个参数是LPVOID,在.net 中用一个byte数组是否可行?
3、第二个参数是指要复制的字节长度,在我没有得到颜色数组的情况下,我应该给这个参数一个什么值?
希望有了解的朋友能帮助我。研究了1天了。没找到答案。谢谢!
Win32 API GetBitmapBits函数的定义:
GetBitmapBits
The GetBitmapBits function copies the bitmap bits of a specified device-dependent bitmap into a buffer.
Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetDIBits function.
LONG GetBitmapBits(
HBITMAP hbmp, // handle to bitmap
LONG cbBuffer, // number of bytes to copy
LPVOID lpvBits // buffer to receive bits
);
Parameters
hbmp
[in] Handle to the device-dependent bitmap.
cbBuffer
[in] Specifies the number of bytes to copy from the bitmap into the buffer.
lpvBits
[out] Pointer to a buffer to receive the bitmap bits. The bits are stored as an array of byte values.
The GetBitmapBits function copies the bitmap bits of a specified device-dependent bitmap into a buffer.
Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetDIBits function.
LONG GetBitmapBits(
HBITMAP hbmp, // handle to bitmap
LONG cbBuffer, // number of bytes to copy
LPVOID lpvBits // buffer to receive bits
);
Parameters
hbmp
[in] Handle to the device-dependent bitmap.
cbBuffer
[in] Specifies the number of bytes to copy from the bitmap into the buffer.
lpvBits
[out] Pointer to a buffer to receive the bitmap bits. The bits are stored as an array of byte values.
下面是我的代码:
在.net 中定义的拖管方法:
[DllImport("gdi32", CharSet=CharSet.Auto)]
static extern bool GetBitmapBits(IntPtr imgFile,int cbBuf,out byte[] ImgByte);
static extern bool GetBitmapBits(IntPtr imgFile,int cbBuf,out byte[] ImgByte);
其中的第一个参数 IntPtr 我是调用Image.GetHbitmap()取得,但取到的值都是很大的整型,感觉不正常。
System.Drawing.Bitmap bitmap=new System.Drawing.Bitmap("d:\\test.bmp");
IntPtr inp=bitmap.GetHbitmap();
IntPtr inp=bitmap.GetHbitmap();
调用托管方法
System.Drawing.Bitmap bitmap=new System.Drawing.Bitmap("d:\\test.bmp");
IntPtr inp=bitmap.GetHbitmap();
byte[] imgByte=new byte[1024];
GetBitmapBits(inp,0,out imgByte);
foreach(byte img in imgByte)
{
Console.WriteLine(img.ToString());
}
Console.Read();
IntPtr inp=bitmap.GetHbitmap();
byte[] imgByte=new byte[1024];
GetBitmapBits(inp,0,out imgByte);
foreach(byte img in imgByte)
{
Console.WriteLine(img.ToString());
}
Console.Read();
以上代码总是出错,找不到原因,现在有以下几个问题请教
1、以上代码在哪个地方错了?
2、GetBitmapBits中的最后一个参数是LPVOID,在.net 中用一个byte数组是否可行?
3、第二个参数是指要复制的字节长度,在我没有得到颜色数组的情况下,我应该给这个参数一个什么值?
希望有了解的朋友能帮助我。研究了1天了。没找到答案。谢谢!