这段时间处理打印问题,需要得到一个黑白的位图文件的颜色信息。 目前只发现使用Win32的API: GetBitmapBits函数可以实现。但经过N次的试验后,总是失败。

    Win32 API  GetBitmapBits函数的定义:
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。GetBitmapBits
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。The GetBitmapBits function copies the bitmap bits of a specified device
-dependent bitmap into a buffer. 
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。Note  This function 
is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetDIBits function. 
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。LONG GetBitmapBits(
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。  HBITMAP hbmp,      
// handle to bitmap
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。
  LONG cbBuffer,     // number of bytes to copy
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。
  LPVOID lpvBits     // buffer to receive bits
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。
);
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。Parameters
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。hbmp 
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。[
in] Handle to the device-dependent bitmap. 
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。cbBuffer 
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。[
in] Specifies the number of bytes to copy from the bitmap into the buffer. 
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。lpvBits 
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。[
out] Pointer to a buffer to receive the bitmap bits. The bits are stored as an array of byte values. 
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。

   下面是我的代码:

    在.net 中定义的拖管方法:
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。[DllImport("gdi32", CharSet=CharSet.Auto)]
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。        
static extern bool GetBitmapBits(IntPtr imgFile,int cbBuf,out byte[] ImgByte);

  其中的第一个参数 IntPtr  我是调用Image.GetHbitmap()取得,但取到的值都是很大的整型,感觉不正常。
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。System.Drawing.Bitmap bitmap=new System.Drawing.Bitmap("d:\\test.bmp");
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。            IntPtr inp
=bitmap.GetHbitmap();

  调用托管方法

在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。System.Drawing.Bitmap bitmap=new System.Drawing.Bitmap("d:\\test.bmp");
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。            IntPtr inp
=bitmap.GetHbitmap();
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。            
byte[] imgByte=new byte[1024];
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。            GetBitmapBits(inp,
0,out imgByte);
在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。            
foreach(byte img in imgByte)
            Console.Read();
  
    以上代码总是出错,找不到原因,现在有以下几个问题请教
    1、以上代码在哪个地方错了?
    2、GetBitmapBits中的最后一个参数是LPVOID,在.net 中用一个byte数组是否可行?
    3、第二个参数是指要复制的字节长度,在我没有得到颜色数组的情况下,我应该给这个参数一个什么值?

       希望有了解的朋友能帮助我。研究了1天了。没找到答案。谢谢!

相关文章:

  • 2021-08-21
  • 2022-12-23
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
猜你喜欢
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-06-09
  • 2021-04-04
  • 2021-12-08
  • 2022-01-14
相关资源
相似解决方案