一般常用寫Bimap保存會報錯GDI+會報錯的寫法 1 public void GetImageLocal() 2 { 3 int num = 9; 4 int initWidth = 256; 5 int initHeight = 256; 6 7 for (var c = '\uff41'; c ...
一般常用寫Bimap保存會報錯GDI+會報錯的寫法
1 public void GetImageLocal() 2 { 3 int num = 9; 4 int initWidth = 256; 5 int initHeight = 256; 6 7 for (var c = '\uff41'; c <= '\uff5a'; c++) 8 { 9 Bitmap image = new Bitmap(initWidth, initHeight);//初始化大小 10 Graphics g = Graphics.FromImage(image); 11 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//設置圖片質量 12 num = (int)c; 13 switch (num % 8)//設置圖片不同背景色 14 { 15 case 1: 16 g.Clear(Color.FromArgb(255, 139, 139)); 17 break; 18 case 2: 19 g.Clear(Color.FromArgb(97, 191, 173)); 20 break; 21 case 3: 22 g.Clear(Color.FromArgb(22, 124, 127)); 23 break; 24 case 4: 25 g.Clear(Color.FromArgb(50, 182, 122)); 26 break; 27 case 5: 28 g.Clear(Color.FromArgb(191, 181, 215)); 29 break; 30 case 6: 31 g.Clear(Color.FromArgb(240, 207, 97)); 32 break; 33 case 7: 34 g.Clear(Color.FromArgb(5, 90, 91)); 35 break; 36 default: 37 g.Clear(Color.FromArgb(5, 90, 91)); 38 break; 39 } 40 41 Font font = new Font("Arial ", 88);//, System.Drawing.FontStyle.Bold);//設置字體樣式,大小 42 Brush brushbackground = new SolidBrush(Color.White); 43 Brush brush = new SolidBrush(Color.FromArgb(166, 8, 8)); 44 g.DrawString(c.ToString(), font, brushbackground, 45, 60);//設置位置 45 image.Save(Application.StartupPath + "\\img\\" + (num).ToString() + ".jpg", ImageFormat.Jpeg);//自己創建一個文件夾,放入生成的圖片(根目錄下) 46 } 47 48 }
這種寫法一般都會報錯,具體怎麼找到問題點呢?一時半會也不好找
所以用個通用的寫法如下
public void AddTextToImgName(string text) { //判斷指定圖片是否存在 if (!File.Exists(AddNameFilePath)) { throw new FileNotFoundException("The file don't exist!"); } if (text == string.Empty) { return; } System.Drawing.Image image = System.Drawing.Image.FromFile(AddNameFilePath); Bitmap bitmap = new Bitmap(image, image.Width, image.Height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //字體大小 float fontSize = 136.0f; //文本的長度 float textWidth = text.Length * fontSize; //下麵定義一個矩形區域,以後在這個矩形里畫上白底黑字 float rectX = 120; float rectY = 400; float rectWidth = text.Length * (fontSize + 140); float rectHeight = fontSize + 70; //聲明矩形域 RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight); //定義字體 System.Drawing.Font font = new System.Drawing.Font("思源黑體", fontSize, System.Drawing.FontStyle.Bold); //font.Bold = true; //白筆刷,畫文字用 System.Drawing.Brush whiteBrush = new SolidBrush(System.Drawing.Color.Black); //黑筆刷,畫背景用 //Brush blackBrush = new SolidBrush(Color.Black); //g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight); g.DrawString(text, font, whiteBrush, textArea); g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; //輸出方法一:將文件生成並保存到根目錄 //Bitmap bmp = new Bitmap(image); //string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "AddName.jpg"; //bmp.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "AddName.jpg"; bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose(); bitmap.Dispose(); image.Dispose(); }
這個隨筆只是做個記錄,遇到的時候,可以翻翻看。謝謝觀看。
多看書,少裝逼!