首頁  >  文章  >  後端開發  >  C#增加圖片文字實例程式碼

C#增加圖片文字實例程式碼

零下一度
零下一度原創
2017-06-23 15:19:441704瀏覽

業務需要動態增加圖片文字(書本的封面圖片),修改字體大小、字體、顏色、控制位置

測試程式碼:

 1             string path = @"E:\cover.png"; 2  3             Bitmap bmp = new Bitmap(path); 4             Graphics g = Graphics.FromImage(bmp); 5             String str = "贤愚经"; 6             Font font = new Font("仿宋_GB2312", 14, FontStyle.Bold);//设置字体,大小,粗细 7             SolidBrush sbrush = new SolidBrush(Color.White);//设置颜色 8             int base_left = 10; 9             int left_space = 30;10 11             int base_top = 27;12             int top_space = 27;13             for (int i = 0; i < str.Length; i++)14             {15                 if (i > 13)16                 {17                     continue;18                     g.DrawString(str[i] + "", font, sbrush, new PointF(base_left + (left_space * 2), base_top + (top_space * (i - 14))));19                 }20                 else if (i > 6)21                 {22                     g.DrawString(str[i] + "", font, sbrush, new PointF(base_left + (left_space * 1), base_top + (top_space * (i - 7))));23                 }24                 else25                 {26                     g.DrawString(str[i] + "", font, sbrush, new PointF(base_left, base_top + (top_space * i)));27                 }28 29             }30 31             //MemoryStream ms = new MemoryStream();32             //bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);33             bmp.Save(@"E:\cover1.png");

字體選擇:

黑體:SimHei
宋體:SimSun
新宋體:NSimSun
仿宋:FangSong
楷體:KaiTi
仿宋_GB2312:FangSong_GB2312
楷體_GB2312 :KaiTi_GB2312
微軟雅黑體:Microsoft YaHei

結束。

以上是C#增加圖片文字實例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn