C#給圖片增加文字執行個體代碼

來源:互聯網
上載者:User
業務需要動態給圖片增加文字(書本的封面圖片),修改字型大小、字型、顏色、控制位置

測試代碼:

 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

完結。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.