Sample code to add text to a picture in C # share

Source: Internet
Author: User
Recent projects need to dynamically add text to the picture (book cover picture), modify the font size, font, color, control position, etc., below through the example code to share C # in the picture to add text implementation code, together to see it

Business needs to dynamically add text to the picture (the book's cover picture), modify the font size, font, color, control location

Test code:


string path = @ "E:\cover.png";      Bitmap bmp = new Bitmap (path);      Graphics g = graphics.fromimage (BMP);      String str = "Yin Folly"; Font font = new Font ("Imitation _gb2312", +, fontstyle.bold);//Set font, size, weight solidbrush Sbrush = new SolidBrush (color.white);//      Set color int base_left = 10;      int left_space = 30;      int base_top = 27;      int top_space = 27; for (int i = 0; i < str. Length;          i++) {if (i >) {continue;        g.DrawString (Str[i] + "", Font, Sbrush, new PointF (Base_left + (Left_space * 2), Base_top + (Top_space * (i-14))); } else if (i > 6) {g.drawstring (Str[i] + "", Font, Sbrush, new PointF (Base_left + (Left_spa        CE * 1), Base_top + (Top_space * (i-7))); } else {g.drawstring (Str[i] + "", Font, Sbrush, new PointF (Base_left, Base_top + (Top_space * i)))        ;      }}//memorystream ms = new MemoryStream (); Bmp. Save (MS, System.Drawing.Imaging.      Imageformat.bmp); Bmp. Save (@ "E:\cover1.png");

Font selection:

Blackbody: Simhei
Song Body: SimSun
New song body: Nsimsun
Imitation: Fangsong
Italics: Kaiti
Imitation _gb2312:fangsong_gb2312
Italic _gb2312:kaiti_gb2312
Microsoft Elegance Blackbody: Microsoft Yahei

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.