C # GDI + simple Drawing (i)

Source: Internet
Author: User
Tags prepare

Recently on GDI + this thing contact more, also did some simple examples, such as drawing board, imitation QQ screenshot.

The first contact with this class, because you want to do imitation QQ screenshot effect. Coincidentally very, learned how to do after the screenshot. NET classroom teacher also happened to talk about C # drawing knowledge, and I myself in the online study of teacher Kim's training course, but also to use this class. There are some experience in learning, so prepare to write down these experiences, because the content is more, I may be divided several times.

Needless to say, let's first get to know this GDI + and see what it looks like.

Gdi+:graphics Device Interface Plus is a graphics device interface that provides a variety of graphic image processing functions; NET, using GDI + to process two-dimensional (2D) graphics and images, using DirectX to process three-dimensional (3D) graphics images, the primary namespace used for graphic image processing is system. Drawing: Provide the basic graphics function of GDI + access, mainly graphics class, Bitmap class, from the brush class, font class, Icon class, Image class, Pen class, color class.

When you know what GDI + is, let's take a look at the main tools that the drawing uses, to paint, definitely to the drawing board bar, in C # in the artboard can be created by graphics this class, with the artboard, you have to get a pen what kind of bar, or how to draw ah, it is difficult to draw with our fingers. The pen can be divided into many different kinds , such as pencils, brushes and so on. The main difference is that the pencil can be used to draw lines, and the brush, hey, think about it yourself. In C # We can use the Pen,brush class to implement similar functions. Pigments are naturally used in color classes.

With the tools, we can start! (Required namespaces: using System.Drawing;)

Implementation effect: Draw basic graphics in a blank form

First prepare a artboard:

There are 3 main ways to create an artboard:

A: Directly refer to the Graphics object in the Paint event of a form or control

B: Using the CreateGraphics method of a form or a control

C: Create a Graphics object from any object that inherits from the image

This time we'll take a as an example to illustrate the problem:

private void Form1_Paint(object sender, PaintEventArgs e)
    {
      Graphics g = e.Graphics; //创建画板,这里的画板是由Form提供的.
    }

Then we want a pen:

private void Form1_Paint(object sender, PaintEventArgs e)
    {
      Graphics g = e.Graphics; //创建画板,这里的画板是由Form提供的.
      Pen p = new Pen(Color.Blue, 2);//定义了一个蓝色,宽度为的画笔
    }

Then we'll be able to draw.

private void Form1_Paint(object sender, PaintEventArgs e)
    {
      Graphics g = e.Graphics; //创建画板,这里的画板是由Form提供的.
      Pen p = new Pen(Color.Blue, 2);//定义了一个蓝色,宽度为的画笔
      g.DrawLine(p, 10, 10, 100, 100);//在画板上画直线,起始坐标为(10,10),终点坐标为(100,100)
      g.DrawRectangle(p, 10, 10, 100, 100);//在画板上画矩形,起始坐标为(10,10),宽为,高为
      g.DrawEllipse(p, 10, 10, 100, 100);//在画板上画椭圆,起始坐标为(10,10),外接矩形的宽为,高为
    }

The effect chart is as follows:

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.