C # Image Processing (1): Add text to the image and change the direction of the text

Source: Internet
Author: User

C # add text to the image. The Code is as follows:

1 /// <summary> 2 // Add text above the image, the text will be reversed by 180 degrees. 3 // </Summary> 4 // <Param name = "IMG"> image to be processed </param> 5 /// <Param name = "writestring"> written string </param> 6 // <Param name = "upmargin"> 180 degrees reverse, the distance from the top of the text to the top of the text </param> 7/ // <Param name = "rightmargin"> the leftmost text distance from the right edge </param> 8 /// <returns> bitmap </returns> 9 Public bitmap writeup (image img, string writestring, int upmargin, int rightmargin) 10 {11 return writeup (IMG, writestring, upmargin, rightmargin, "", 20 ); 12} 13 14 // <summary> 15 // Add text above the picture, the text will be reversed by 180 degrees. 16 /// </Summary> 17 /// <Param name = "IMG"> image to be processed </param> 18 /// <Param name = "writestring"> written string </param> 19 // <Param name = "upmargin"> the distance between the top of the text and the top edge of the text after the 180 degree reversal </param> 20/ // <Param name = "rightmargin"> the leftmost text distance from the right edge </param> 21 // <Param name = "fonttype"> font type </param> 22 /// <Param name = "fontsize"> font size </param> 23 // <returns> </returns> 24 public bitmap writeup (image IMG, string writestring, int upmargin, int rightmargin, string fonttype, int fontsize) 25 {26 // get the image width and height 27 int width = IMG. width; 28 int Height = IMG. height; 29 // obtain the horizontal and vertical resolutions of the image. 30 float dpix = IMG. horizontalresolution; 31 float dpiy = IMG. verticalresolution; 32 // create a bitmap file 33 bitmap bitmapresult = new Bitmap (width, height, pixelformat. format24bpprgb); 34 // sets the horizontal and vertical resolutions of Bitmap files to be consistent with that of IMG 35 bitmapresult. setresolution (dpix, dpiy); 36 // fill in a Rectangular Box 37 graphics GRP = graphics in the bitmap file. fromimage (bitmapresult); 38 system. drawing. rectangle rec = new system. drawing. rectangle (0, 0, width, height); 39 // fill img40 GRP in the rectangle. drawimage (IMG, 0, 0, REC, graphicsunit. pixel); 41 42 43 // Pan graphics object 44 GRP. translatetransform (width-rightmargin, upmargin); 45 // set the output angle of the graphics object to change the text direction. 46 GRP. rotatetransform (180); 47 // set the text fill color 48 brush = brushes. black; 49 // rotate the display text 50 GRP. drawstring (writestring, new font (fonttype, fontsize, graphicsunit. pixel), brush, 0, 0); 51 // restore the global transformation matrix 52 GRP. resettransform (); 53 GRP. dispose (); 54 GC. collect (); 55 return bitmapresult; 56}

 

Related Article

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.