刚刚接触C#,网上找了好久都没有找到想要的代码实例,现在需要将从数据库中读到的数据通过Bitmap这个类来生成*.bmp文件;如何实现呢?
使用GDI+楼主参考下面的:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; using System.IO; using System.Threading; using System.Drawing.Drawing2D; namespace drawgdi { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int length = 500; int height = 500; Bitmap bmp = new Bitmap(length, height);//新建一个图片对象 Graphics g = Graphics.FromImage(bmp);//利用该图片对象生成“画板” Font font = new Font("黑体", 12);//设置字体颜色 SolidBrush brush = new SolidBrush(Color.Red);//新建一个画刷,到这里为止,我们已经准备好了画板、画刷、和数据 Pen p = new Pen(Color.Red, 1);//定义了一个红色,宽度为的画笔 g.Clear(Color.Black); //设置黑色背景 //一排数据 g.DrawRectangle(p, 10, 10, 80, 20);//在画板上画矩形,起始坐标为(10,10),宽为80,高为20 g.DrawRectangle(p, 90, 10, 80, 20);//在画板上画矩形,起始坐标为(90,10),宽为80,高为20 g.DrawRectangle(p, 170, 10, 80, 20);// g.DrawRectangle(p, 250, 10, 80, 20);// g.DrawString("目标", font, brush, 12, 12);// g.DrawString("完成数", font, brush, 92, 12); g.DrawString("完成率", font, brush, 172, 12);//进行绘制文字。起始坐标为(172, 12) g.DrawString("效率", font, brush, 252, 12);//关键的一步,进行绘制文字。 g.DrawRectangle(p, 10, 30, 80, 20); g.DrawRectangle(p, 90, 30, 80, 20); g.DrawRectangle(p, 170, 30, 80, 20); g.DrawRectangle(p, 250, 30, 80, 20); g.DrawString("800", font, brush, 12, 32); g.DrawString("500", font, brush, 92, 32);//关键的一步,进行绘制文字。 g.DrawString("60%", font, brush, 172, 32);//关键的一步,进行绘制文字。 g.DrawString("50%", font, brush, 252, 32);//关键的一步,进行绘制文字。 g.DrawRectangle(p, 10, 50, 80, 20); g.DrawRectangle(p, 90, 50, 80, 20); g.DrawRectangle(p, 170, 50, 160, 20);//在画板上画矩形,起始坐标为(170,10),宽为160,高为20 g.DrawString("总查数", font, brush, 12, 52); g.DrawString("不良数", font, brush, 92, 52); g.DrawString("合格率", font, brush, 222, 52); bmp.Save("E:/test.bmp");//保存为输出流,否则页面上显示不出来 g.Dispose();//释放掉该资源 } } }
谢谢!我试试,貌似可以
使用GDI+绘制
先做一个模板图片:11.jpg
模板图片质量搞高点,不然生成会模糊。
然后再在这张图上把数字画上去。
代码:
System.Drawing.Image image = System.Drawing.Image.FromFile("D://11.jpg"); Bitmap bitWaterMark = new Bitmap(image.Width, image.Height); Graphics g = Graphics.FromImage(bitWaterMark); g.DrawImage(image, 0, 0, image.Width, image.Height); Font f = new Font("Arial", 9); Brush c = new SolidBrush(Color.FromName("red")); g.DrawString("800", f, c, new PointF(20, 20)); g.DrawString("500", f, c, new PointF(100, 20)); g.DrawString("60%", f, c, new PointF(180, 20)); g.DrawString("40%", f, c, new PointF(260, 20)); g.DrawString("100", f, c, new PointF(20, 60)); g.DrawString("5", f, c, new PointF(100, 60)); g.DrawString("95%", f, c, new PointF(200, 60)); g.Dispose(); image.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bitWaterMark.Save(ms, ImageFormat.Jpeg); System.Drawing.Image img2 = System.Drawing.Image.FromStream(ms); img2.Save("D://22.jpg"); bitWaterMark.Dispose();