Use C # To create a screen capture program
With the further development of Microsoft. Net's strategy, various new technologies and concepts emerge one after another, especially the launch of C # language, which has a significant impact on the industry. C # is an efficient language. It has both the RAD feature of VB and the efficiency of C ++. Most importantly, it is based on the. Net Framework and has powerful advantages in graphics and images, network programming, etc. Therefore, it is likely to become one of the main programming languages in the future.
This article uses an example to show you how to program graphics and Images Using C. Some basic knowledge of GDI + programming is used in the instance. GDI + evolved from GDI and is a new graphic image programming mechanism in the. Net environment. It not only has some basic functions of GDI programming, but also has been expanded a lot to make it more powerful. Using GDI in earlier versions of Visual Studio is quite complex and requires a lot of work. Currently, in GDI +, Microsoft has helped us solve many problems. Therefore, it is very easy to program graphics and images using GDI +. However, it should be pointed out that, although it is complicated to use GDI programming, almost all of the complex functions can be completed. For example, you can easily create a screen capture program using GDI in an instance. So, can we achieve the same function in GDI +? The answer is yes. It is because GDI + evolved from GDI that we only need to call some features of GDI in GDI +.
C # has a feature that allows you to use any pre-completed dynamic connection library file (*. dll), including the gdi32.dll file. This file contains the basic class libraries, functions, and methods required for GDI programming. In this way, we can also enjoy the valuable resources that GDI leaves for us under the GDI + programming mechanism. The specific import method is as follows:
[System. Runtime. InteropServices. DllImportAttribute ("gdi32.dll")]
The context device (device context) is required for screen capture programs in GDI. In the program, we use it to obtain the screen image and write it into a single-digit graph in the memory. The implementation method is to call the BitBlt () function. Similarly, the principle implemented in GDI + is the same. The following describes external declaration functions in GDI + (described in C ):
Privatestaticexternbool BitBlt (
IntPtr hdcDest,File: // objectHandle of the context Device
Int nXDest,File: // objectX coordinate in the upper left corner of the graph
Int nYDest,File: // objectY coordinate in the upper left corner of the graph
Int nWidth,File: // objectThe width of the rectangle in the image.
Int nHeight,File: // objectThe rectangle height of the image.
IntPtr hdcSrc,File: // SourceContext device handle
Int nXSrc,File: // SourceX coordinate in the upper left corner of the graph
Int nYSrc,File: // SourceX coordinate in the upper left corner of the graph
System. Int32 dwdropFile: // opticalGrid Operation Code
);
In order to call GDI in GDI +, we also need to obtain the context device hook. A Method in GDI + provides the specific implementation of this operation:
IntPtr dc1 = aGraphicsObject. GetHdc ();
After obtaining the handles of the source and target context devices, we can use these BitBlt () functions as parameters to create images. After an image is created, the source and target context device resources must be released.