C#实现自动生成图片并保存在本地

本文介绍了一种使用C#编程语言动态生成图片的方法,并详细展示了如何根据当前日期和时间创建目录路径来保存这些图片。通过具体代码示例,读者可以了解到如何利用Graphics对象绘制文本到图片上,并将图片保存为JPEG格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 private void GenerateImagePath()
 2         {
 3             string Opath =@"D:\Test";//System.Configuration.ConfigurationSettings.AppSettings["ImagePath"];
 4             if (Opath.Substring(Opath.Length-11!= @"\")
 5                 Opath = Opath + @"\";
 6             string path1 = Opath + DateTime.Now.ToShortDateString();
 7             string path2 = Opath + DateTime.Now.ToShortDateString() + "\\" + DateTime.Now.Hour.ToString();
 8             string path3 = Opath + DateTime.Now.ToShortDateString() + "\\" + DateTime.Now.Hour.ToString() + "\\" + DateTime.Now.Minute.ToString();
 9             if (!Directory.Exists(path1))
10             {
11                 Directory.CreateDirectory(path1);
12             }
13             if (!Directory.Exists(path2))
14             {
15                 Directory.CreateDirectory(path2);
16             }
17             if (!Directory.Exists(path3))
18             {
19                 Directory.CreateDirectory(path3);
20             }
21             string _FontName = "verdana";
22             int _FontSize = 25;
23             string _ShowName = DateTime.Now.ToString();
24             Bitmap objBitmap = null;
25             Graphics g = null;
26             Font stringFont = new Font(_FontName, _FontSize, FontStyle.Bold);
27             StringFormat stringFormat = new StringFormat();
28             stringFormat.FormatFlags = StringFormatFlags.NoWrap;
29             objBitmap = new Bitmap(11);
30             g = Graphics.FromImage(objBitmap);
31             SizeF stringSize = g.MeasureString(_ShowName, stringFont);
32             int nWidth = (int)stringSize.Width;
33             int nHeight = (int)stringSize.Height;
34             g.Dispose();
35             objBitmap.Dispose();
36             objBitmap = new Bitmap(nWidth, nHeight);
37             g = Graphics.FromImage(objBitmap);
38             //g.FillRectangle(new SolidBrush(Color.Yellow), new Rectangle(0, 0, nWidth, nHeight));
39             g.FillEllipse(new SolidBrush(Color.Yellow), new Rectangle(00, nWidth, nHeight));
40             g.DrawString(_ShowName, stringFont, new SolidBrush(Color.Black), new PointF(00), stringFormat);
41             objBitmap.Save(path3 + "\\" + DateTime.Now.Ticks.ToString() + ".jpg", ImageFormat.Jpeg);
42         }

参考文章:
http://battler.cnblogs.com/archive/2005/01/26/97880.html

转载于:https://www.cnblogs.com/AlphaWu/archive/2006/04/10/371572.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值