// 创建一个新的图片对象
Bitmap image = new Bitmap(500, 500); // 宽度和高度为200像素
Pen pen = new Pen(Color.Black);
pen.Color = Color.Yellow;
pen.Width = 3;
// 获取图片的Graphics对象,用于绘制
Graphics graphics = Graphics.FromImage(image);
// 设置背景色为白色
graphics.Clear(Color.White);
// 绘制表格边框
graphics.DrawRectangle(Pens.Black, 50, 50, 400, 400);
//graphics.DrawLine(Pens.Black, 50, 50, 50, 200);
graphics.DrawLine(pen, 100, 50, 100, 200);
graphics.DrawLine(pen, 150, 50, 150, 200);
graphics.DrawLine(pen, 200, 50, 200, 200);
graphics.DrawLine(pen, 250, 50, 250, 200);
pen.Color = Color.Red;
graphics.DrawLine(pen, 50, 100, 250, 100);
graphics.DrawLine(pen, 50, 150, 250, 150);
graphics.DrawLine(pen, 50, 200, 250, 200);
graphics.DrawLine(pen, 50, 250, 250, 250);
//绘制表格标题行
graphics.DrawString("Column 1", new Font("Arial", 14), Brushes.Black, 60, 60);
graphics.DrawString("Column 2", new Font("Arial", 14), Brushes.Black, 70, 80);
graphics.DrawString("Column 3", new Font("Arial", 14), Brushes.Black, 80, 60);
graphics.DrawString("Column 4", new Font("Arial", 14), Brushes.Black, 90, 80);
graphics.DrawString("Column 5", new Font("Arial", 14), Brushes.Black, 100, 60);
graphics.DrawString("Column 6", new Font("Arial", 14), Brushes.Black, 110, 80);
// 绘制表格数据行
//graphics.DrawString("Data 1", new Font("Arial", 12), Brushes.Black, 70, 100);
//graphics.DrawString("Data 2", new Font("Arial", 12), Brushes.Black, 70, 120);
// 保存图片到文件
image.Save(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "MustFile\\" + "总结" + ".png"); //System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "MustFile\\"
using (FileStream stream = new FileStream(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "MustFile\\" + "总结" + ".png", FileMode.Open, FileAccess.Read))
{
pictureBox2.Image = Image.FromStream(stream);
}
//pictureBox2.Image = Image.FromFile(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "MustFile\\" + "总结" + ".png");
// 释放Graphics和Bitmap资源
graphics.Dispose();
image.Dispose();
C#:使用GDI+创建一张图片并保存到本地
最新推荐文章于 2024-08-08 11:24:16 发布