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

时间:2024-02-22 22:29:03
 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