Screen Capturing a Form in .NET - Using GDI and GDI+

 

Figure 1 - Form captured into a jpeg file

In most cases, GDI+ speeds up your programming of Graphics because (1) It is not a thin veneer over the Windows SDK (2) It makes sense.   However, whenever you lose granularity to create a simpler to use architecture, you tend to lose some functionality.  Form Capture is one of these cases. In the cases where you say to yourself: "Hey!  I could do that in GDI, why can't I do that in GDI+??".  The answer is you can.

C# has an attribute that allows you to bring in virtually any pre-millenium Windows SDK dll.  This, of course, includes the famed gdi.dll.  The way to do this is shown below using the DllImportAttribute:

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

The way to do form capture in GDI is to get the device context to the screen and bit blast it to a Bitmap in memory.  The GDI external declaration for bit blasting is shown below (Note that this call has been converted to the equivalent call in C# so it can be used in our program):

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
); 

In order to use this GDI call in GDI+ you need to have hooks into the device contexts.  Luckily GDI+ provides a method of getting the GDI device context through the GDI+ graphics object:

IntPtr dc1 = aGraphicsObject.GetHdc();

Now we'll put the whole thing together in our Form.  Below is the series of calls needed to get the image from the form and stick it in a bitmap using the following steps:

  • Get a graphics object to the Form on the screen
  • Create an empty bitmap the size of the Form's Client Area
  • Get the Device Context for the Form
  • Get the Device Context for the Bitmap
  • Bit Blast the Form on the screen into the Bitmap
  • Release the Device Context for the Form
  • Release the Device Context for the Bitmap
  • Save the Image into a jpeg File

Remember, its important to release the GDI device context after your done using it to make the GDI call(s), otherwise GDI+ will have problems. Below is the full code for capturing the forms client area into a jpeg file:

private void Capture_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);
MessageBox.Show("Finished Saving Image");

Happy Bit Blasting!

http://www.c-sharpcorner.com/UploadFile/mgold/ScreenCapturingaForm09162005063420AM/ScreenCapturingaForm.aspx

"divide and grow: capturing huge diversity in crowd images with incrementally" 是一个标题,该标题是基于一种方法或者技术,用于在人群图像中捕捉巨大的多样性。该方法通过逐步分割和增长的方式来实现。 在传统的人群图像处理中,通常会面临到一个挑战,即人群中的个体数量巨大且多样性丰富。这使得对整个人群图像进行全局处理变得困难。 "divide and grow"方法通过将人群图像分割成更小的区域,并在每个区域中逐步增加分析的粒度来解决这个问题。 首先,该方法会将人群图像划分成多个重叠的子区域。然后,对每个子区域进行单独的分析,以捕捉到区域内的个体。这种分割使得处理的任务更加可管理,同时也可以提高对于人群中不同个体的检测能力。接下来,"divide and grow"方法会逐步增加分析的粒度,即进一步细分每个子区域,以更准确地捕捉到更多的个体。 通过这种逐步分割和增长的方式,"divide and grow"方法可以较好地捕捉到人群图像中的巨大多样性。这种方法可以帮助在人群中准确地检测和识别各种不同类型的个体,例如不同年龄、性别、服装等。 总之,"divide and grow: capturing huge diversity in crowd images with incrementally"是一种用于处理人群图像中巨大多样性的方法。通过逐步分割和增长的方式,该方法可以有效地捕捉到人群中各种不同类型的个体。这种方法可以在人群图像处理和识别中具有广泛的应用潜力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值