BitBlt函数

BitBlt 参考地址:

1. MSDN: http://msdn.microsoft.com/en-us/library/dd183370(VS.85).aspx

2. 其他介绍: http://blog.csdn.net/songsu/archive/2009/07/01/4313032.aspx

                   http://blog.csdn.net/wwei466/archive/2008/10/21/3117056.aspx

 

 

书面理解: 

该函数对指定的源或设备环境区域中的像进行位块(bit_block)转换,以传送到目标设备环境。

 

注意点:

该函数是将一副位图从一个场景复制到另一个, 源和目标DC相互必须兼容

 

函数解释:

private static extern bool BitBlt(
        IntPtr hdcDest,   //   handle   to   destination   DC   

        int nXDest,   //   x-coord   of   destination   upper-left   corner   

        int nYDest,   //   y-coord   of   destination   upper-left   corner   

        int nWidth,   //   width   of   destination   rectangle   

        int nHeight,   //   height   of   destination   rectangle   

        IntPtr hdcSrc,   //   handle   to   source   DC   

        int nXSrc,   //   x-coordinate   of   source   upper-left   corner   

        int nYSrc,   //   y-coordinate   of   source   upper-left   corner   

        System.Int32 dwRop   //   raster   operation   code   

      );

简略表达: 目标hDC,  目标矩形X,  目标矩形Y, 目标矩形高, 目标矩形宽, 源hDC,  源X, 源Y,  光栅运算常值 

 

网上查阅:

看到很多提问使用BitBlt,得不到正确的图片,得到的图片是空的(黑的),网上有这样一些评论:

1.  bitblt方法只能截取已经显示在控件上的图片,而不能截取内存中的图片。

2.  并非可见不可见的缘故,若是用PictureBox.CreateGraphics,则必须可见。

http://social.msdn.microsoft.com/Forums/zh-TW/233/thread/74d8c51f-1ed7-4492-91aa-524723a8b07e

 

本人也试验了下:

private PictureBox picB;

private void manualInit() {

            picB = new PictureBox();
            this.Controls.Add(picB);
            picB.Dock = DockStyle.Fill;

}

 

private Bitmap BitBltTest(Bitmap aBmp, Rectangle aRect) {
            this.picB.Image = aBmp;
            this.Refresh();
            Graphics gSrc = Graphics.FromHwnd(this.Handle);
            // Graphics gSrc = this.picB.CreateGraphics();  同上

 

            Bitmap theCopyBmp = new Bitmap(aRect.Width, aRect.Height);
            Graphics gDest = Graphics.FromImage(theCopyBmp);        

            IntPtr hdcSrc = gSrc.GetHdc();
            IntPtr hdcDest = gDest.GetHdc();
            BitBlt(hdcDest, 0, 0, aRect.Width, aRect.Height, hdcSrc, aRect.X, aRect.Y, SRCCOPY);

 

            gSrc.ReleaseHdc();
            gDest.ReleaseHdc();
            gSrc.Dispose();
            gDest.Dispose();

 

            return theCopyBmp;
        }

注意: 源DC需要关联到Hwnd才能正确的绘制出来,pictureBox要加到Form中才能得到Hwnd.

           目标DC似乎没有这个要求,直接从image中获取DC即可。

           其中,本人在给源设置图片后,加了个刷新操作,如果没有执行这个绘制操作,得到的是背景,无图片。

 

很多人,直接PictureBox pic = new PictureBox(); pic.Image=sampleImage; SourceGraphics=pic.CreateGreate();

这样的话,pic Create出来的Graphics不是正确的,所以也无法从源中得到像素信息。

 

对于BitBlt的光栅操作,图形旋转或剪切变换等可以下次可以试试看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值