C#實現自動產生圖片並儲存在本地

來源:互聯網
上載者:User
 1 private void GenerateImagePath()
 2         {
 3             string Opath =@"D:\Test";//System.Configuration.ConfigurationSettings.AppSettings["ImagePath"];
 4             if (Opath.Substring(Opath.Length-1, 1) != @"\")
 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(1, 1);
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(0, 0, nWidth, nHeight));
40             g.DrawString(_ShowName, stringFont, new SolidBrush(Color.Black), new PointF(0, 0), stringFormat);
41             objBitmap.Save(path3 + "\\" + DateTime.Now.Ticks.ToString() + ".jpg", ImageFormat.Jpeg);
42         }

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.