-
C# 中针对 System.Runtime.InteropServices.ExternalException:“GDI+ 中发生一般性错误。”另
一般常用写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
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数