C # GDI + drawing Advanced programming

Source: Internet
Author: User
Tags object model old windows

In general, one of the benefits of Windows (actually the advantage of modern operating systems) is that it allows developers to leave out the specifics of a particular device. For example: You do not need to understand the hard drive device driver, just in the relevant. NET class, you can programmatically read and write files on the disk. This rule also applies to drawings. When the computer is drawing on the screen, the instructions are sent to the video card. The problem is that there are hundreds of different video cards on the market, most of which have different instruction set functions. If you take this I into account, it is impossible to write a specific code for each video card driver to draw on the screen in your application. This is why Windows graphical Device Interface (GDI) is available in the oldest version of Windows.

GDI + provides an abstraction layer that hides the differences between different video cards so that the Windows API function can be invoked to complete the specified task, and GDI also internally points out that when running a particular code, the client's video card completes the drawing. GDI can also accomplish other tasks. Most computers have multiple display devices---monitors, printers. GDI successfully makes the printer that the application uses look the same as the screen. If you want to print something instead of showing them, simply tell the system that the output device is a printer, and then call the same Windows API function in the same way.

You can see that the DC (device environment) is a very powerful object, under GDI, all drawing work must be done through the device environment. DCs can even be used for other operations that do not involve drawing on a screen or other hardware device, such as modifying an image in memory.

The GDI developer provides a fairly advanced API, but it is still an API based on the old Windows API and has a C language style function, so it's not easy to use. GDI + is to a large extent a layer between GDI and applications, providing a more intuitive, inheritance-based object model. Although GDI + is basically a wrapper for GDI, Microsoft has been able to provide new functionality through GDI + and has claimed some performance improvements.

1.gdi+ namespaces

(Don't say, see for yourself!!!) )

2. Equipment Environment and graphics objects

GDI uses the device environment (DC) to identify the output device as it is. The DC object stores information for a specific device and converts GDI API function calls to commands to be sent to the device. You can also determine what functionality the corresponding device has, such as color or black-and-white, through the DC object. If the device is required to complete a task that it cannot complete, the device object detects and takes action.

DC objects can be used not only as hardware but also as a bridge to Windows. For example, if Windows knows that only a small portion of the application window needs to be redrawn, the DC can capture and undo drawing work outside the area. Because DCs are very close to Windows, working through a DC can simplify code in other ways.

Drawing graphics

The following example illustrates how to draw in the main window of an application. Displayatstartup

Create a C # application and draw it in the constructor when you start the form. This is not the best way to draw on the screen, and this example does not redraw the form as needed after startup. It's just that you don't have to do too much work to explain some of the problems.

First, set the background color of the form to white. If you use Design view to set the background color, the system automatically adds code:

    private void InitializeComponent()
        {
               this.AutoScaleBaseSize = new System.Drawing.Size(5,13);
               this.BackColor = System.Drawing.Color.White;
               this.ClientSize = new System.Drawing.Size(292,266);
               this.Name = "Form1";
               this.Text = "Form1";
        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.