一般常用写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();
        }

这个随笔只是做个记录,遇到的时候,可以翻翻看。谢谢观看。

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/R00R/p/16667598.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!