在.net 中调用win32API :GetBitmapBits ,获取位图的颜色数组,发送给打印机。

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

    Win32 API  GetBitmapBits函数的定义:
None.gif GetBitmapBits
None.gifThe GetBitmapBits function copies the bitmap bits of a specified device
- dependent bitmap into a buffer. 
None.gif
None.gifNote  This function 
is  provided only  for  compatibility with  16 - bit versions of Windows. Applications should use the GetDIBits function. 
None.gif
None.gifLONG GetBitmapBits(
None.gif  HBITMAP hbmp,      
//  handle to bitmap
None.gif
  LONG cbBuffer,      //  number of bytes to copy
None.gif
  LPVOID lpvBits      //  buffer to receive bits
None.gif
);
None.gifParameters
None.gifhbmp 
None.gif[
in ] Handle to the device - dependent bitmap. 
None.gifcbBuffer 
None.gif[
in ] Specifies the number of bytes to copy from the bitmap into the buffer. 
None.giflpvBits 
None.gif[
out ] Pointer to a buffer to receive the bitmap bits. The bits are stored  as  an array of  byte  values. 
None.gif

   下面是我的代码:

    在.net 中定义的拖管方法:
None.gif [DllImport( " gdi32 " , CharSet = CharSet.Auto)]
None.gif        
static   extern   bool  GetBitmapBits(IntPtr imgFile, int  cbBuf, out   byte [] ImgByte);

  其中的第一个参数 IntPtr  我是调用Image.GetHbitmap()取得,但取到的值都是很大的整型,感觉不正常。
None.gif System.Drawing.Bitmap bitmap = new  System.Drawing.Bitmap( " d:\\test.bmp " );
None.gif            IntPtr inp
= bitmap.GetHbitmap();

  调用托管方法

None.gif System.Drawing.Bitmap bitmap = new  System.Drawing.Bitmap( " d:\\test.bmp " );
None.gif            IntPtr inp
= bitmap.GetHbitmap();
None.gif            
byte [] imgByte = new   byte [ 1024 ];
None.gif            GetBitmapBits(inp,
0 , out  imgByte);
None.gif            
foreach ( byte  img  in  imgByte)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                Console.WriteLine(img.ToString());
ExpandedBlockEnd.gif            }

None.gif            Console.Read();
  
    以上代码总是出错,找不到原因,现在有以下几个问题请教
    1、以上代码在哪个地方错了?
    2、GetBitmapBits中的最后一个参数是LPVOID,在.net 中用一个byte数组是否可行?
    3、第二个参数是指要复制的字节长度,在我没有得到颜色数组的情况下,我应该给这个参数一个什么值?

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

转载于:https://www.cnblogs.com/cwbboy/archive/2005/07/13/192020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值