Simple C # image operations

Source: Internet
Author: User

Recently, You need to draw some labels (such as simple circular rectangular text) on an image that already exists on the server ). I have never done any graphic operations before, but I heard it is quite simple. Let's just ask, we will use the Graphics class.
Then, you can simply get the following code.

1 // get the image
2 System. Drawing. Image img = System. Drawing. Image. FromFile (this. TextBox1.Text );
3 Graphics g = Graphics. FromImage (img );
4 Pen p = new Pen (Color. White, 2 );
5g. DrawImage (img, 10, 10 );
6
7 // circle and text
8g. DrawEllipse (p, 10, 10, 5, 5 );
9g. DrawString ("here is the circle", new Font ("", 9), Brushes. White, new PointF (20, 20 ));
10
11 // fill in a circle
12g. FillEllipse (Brushes. White, 10, 10, 5, 5 );
13
14 // The file is not generated when it is displayed on the page
15 Response. ContentType = "image/gif ";
16 img. Save (Response. OutputStream, ImageFormat. Jpeg );

In this way, an image is displayed on the page. And draw a circle.

PS: Use a stupid method to merge Multiple Graphs into one graph: 1 // merge four images
2 // 96*96
3System. Drawing. Image imgBig = System. Drawing. Image. FromFile ("c: \ qcd_logo2.PNG ");
4 // 48*48
5System. Drawing. Image img = System. Drawing. Image. FromFile ("c: \ qcd_logo.bmp ");
6 Graphics g = Graphics. FromImage (imgBig );
7G. DrawImage (img, 0, 0 );
8g. DrawImage (img, 49, 0 );
9g. DrawImage (img, 0, 49 );
10g. DrawImage (img, 49, 49 );

PS2: extract the merged graph in one way. 1 // 144*144 Graph
2 System. Drawing. Image imgBig = System. Drawing. Image. FromFile ("c: \ qcd_logo2.PNG ");
3 // 48*48 Graph
4 System. Drawing. Image img = System. Drawing. Image. FromFile ("c: \ qcd_logo.bmp ");
5 Graphics g = Graphics. FromImage (imgBig );
6 Pen p = new Pen (Color. Red, 2 );
7 // merge all, that is, 3*3
8g. DrawImage (img, 0, 0 );
9g. DrawImage (img, 49, 0 );
10g. DrawImage (img, 0, 49 );
11g. DrawImage (img, 49, 49 );
12g. DrawImage (img, 97, 0 );
13g. DrawImage (img, 0, 97 );
14g. DrawImage (img, 97, 97 );
15g. DrawImage (img, 49, 97 );
16g. DrawImage (img, 97, 49 );
17g. DrawEllipse (p, 49, 49, 10, 10 );
18g. DrawString ("this is a circle", new Font ("", 9), Brushes. Red, new PointF (55, 55 ));
19
20 // switch to the thumbnail
21g = Graphics. FromImage (img );
22 // specifies the range of the captured image.
23 Rectangle rect = new Rectangle (49, 49, 48, 48 );
24 // capture a large image
25g. DrawImage (imgBig, 0, 0, rect, GraphicsUnit. Pixel );
26
27 // The file is not generated when it is displayed on the page
28 Response. ContentType = "image/gif ";
29 img. Save (Response. OutputStream, ImageFormat. Jpeg );

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.