求一个类似功能的c#函数~~就是类似bitblt的
c#中有无这样类似的函数,就是把窗体上的自己画的图象拷贝到内存dc中。在vc中用bitblt可以实现。那在c#可以怎么实现呢。
注:不是从屏幕CopyFromScreen。这个函数有这样的传输功能。但是他是相对屏幕来说的。我要求的是自己的窗体。
------解决方案--------------------
你调用非托管代码不就可以了
[DllImport("gdi32.dll")]
private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, Int32 dwrop);
private void btnGetScreen_Click(object sender, EventArgs e)
{
//抓取屏幕
/*
Rectangle rScreen = new Rectangle();
rScreen = Screen.GetWorkingArea(this);
Graphics g1 = this.CreateGraphics();
Image theImage = new Bitmap(rScreen.Width, rScreen.Height, g1);
Graphics g2 = Graphics.FromImage(theImage);
IntPtr ihandle = g1.GetHdc();
IntPtr iSrc = g2.GetHdc();
BitBlt(iSrc, 2, 0, rScreen.Width, rScreen.Height, ihandle, 0, 0, 13369376);
g1.ReleaseHdc(ihandle);
g2.ReleaseHdc(iSrc);
theImage.Save(@"c:\Kevin.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show("抓取成功!");
* */
//抓取窗体
Rectangle rForm = new Rectangle();
rForm = this.Bounds;
Graphics g1 = this.CreateGraphics();
Image theImage = new Bitmap(rForm.Width, rForm.Height, g1);
Graphics g2 = Graphics.FromImage(theImage);
IntPtr ihandle = g1.GetHdc();
IntPtr iSrc = g2.GetHdc();
BitBlt(iSrc, 2, 0, rForm.Width, rForm.Height, ihandle, 0, 0, 13369376);
g1.ReleaseHdc(ihandle);
g2.ReleaseHdc(iSrc);
theImage.Save(@"c:\Kevin.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show("抓取成功!");
}
------解决方案--------------------我是直接调用的bitblt来搞定这个问题的
C# code
private void button1_Click(object sender, System.EventArgs e)
{
Graphics g1 = this.CreateGraphics();//获得窗体图形对象
Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);//创建位图图形对象
IntPtr dc1 = g1.GetHdc();//获得窗体的上下文设备
IntPtr dc2 = g2.GetHdc();//获得位图文件的上下文设备
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);//写入到位图
g1.ReleaseHdc(dc1);//释放窗体的上下文设备
g2.ReleaseHdc(dc2);//释放位图文件的上下文设备
MyImage.Save(@"c:\Captured.jpg", ImageFormat.Jpeg);//保存为jpeg文件
MessageBox.Show("保存图片结束!");
}