C#在图片增加文字的实现代码
程序员文章站
2023-12-10 23:40:16
业务需要动态给图片增加文字(书本的封面图片),修改字体大小、字体、颜色、控制位置
测试代码:
string path = @"e:\cover.png";...
业务需要动态给图片增加文字(书本的封面图片),修改字体大小、字体、颜色、控制位置
测试代码:
string path = @"e:\cover.png"; bitmap bmp = new bitmap(path); graphics g = graphics.fromimage(bmp); string str = "贤愚经"; font font = new font("仿宋_gb2312", 14, fontstyle.bold);//设置字体,大小,粗细 solidbrush sbrush = new solidbrush(color.white);//设置颜色 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 > 13) { 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_space * 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");
字体选择:
黑体:simhei
宋体:simsun
新宋体:nsimsun
仿宋:fangsong
楷体:kaiti
仿宋_gb2312:fangsong_gb2312
楷体_gb2312:kaiti_gb2312
微软雅黑体:microsoft yahei
以上所述是小编给大家介绍的c#在图片增加文字的实现代码,希望对大家有所帮助