无法从带有索引像素格式的图像创建graphics对象
来源:互联网 发布:上海专业seo服务 编辑:程序博客网 时间:2024/06/05 09:34
http://www.cnblogs.com/qixuejia/archive/2010/09/03/1817248.html
大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be created from an image that has an indexed pixel format"
这个exception是出现在 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage("图片路径")
这个调用的语句上,通过查询 MSDN, 我们可以看到如下的提示信息:
/// <summary>/// 会产生graphics异常的PixelFormat/// </summary>private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,PixelFormat.Format8bppIndexed };/// <summary>/// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中/// </summary>/// <param name="imgPixelFormat">原图片的PixelFormat</param>/// <returns></returns>private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat){ foreach (PixelFormat pf in indexedPixelFormats) { if (pf.Equals(imgPixelFormat)) return true; } return false;}//.........使用using (Image img = Image.FromFile("原图片路径")){ //如果原图片是索引像素格式之列的,则需要转换 if (IsPixelFormatIndexed(img.PixelFormat)) { Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmp)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(img, 0, 0); } //下面的水印操作,就直接对 bmp 进行了 //...... } else //否则直接操作 { //直接对img进行水印操作 }}
// <summary> /// 在图片上生成图片水印 /// </summary> /// <param name="Path">原服务器图片路径</param> /// <param name="Path_syp">生成的带图片水印的图片路径</param> /// <param name="Path_sypf">水印图片路径</param> public static void AddWaterPic(string Path, string Path_syp, string Path_sypf, double xPer, double yPer) { using (Image img = Image.FromFile(Path)) { //如果原图片是索引像素格式之列的,则需要转换 if (IsPixelFormatIndexed(img.PixelFormat)) { Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmp)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(img, 0, 0); } System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf); System.Drawing.Graphics g2 = System.Drawing.Graphics.FromImage(bmp); int x = (int)Convert.ToInt32(bmp.Width * xPer); int y = (int)Convert.ToInt32(bmp.Height * yPer);
g2.DrawImage(copyImage, new System.Drawing.Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel); bmp.Save(Path_syp);
g2.Dispose(); bmp.Dispose();
} else { System.Drawing.Image image = System.Drawing.Image.FromFile(Path); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); int x = (int)Convert.ToInt32(image.Width * xPer); int y = (int)Convert.ToInt32(image.Height * yPer);
g.DrawImage(copyImage, new System.Drawing.Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel); image.Save(Path_syp);
g.Dispose(); image.Dispose(); } } } #endregion #region /// <summary> /// 会产生graphics异常的PixelFormat /// </summary> private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare, PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed, PixelFormat.Format8bppIndexed };
/// <summary> /// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中 /// 无法从带有索引像素格式的图像创建graphics对象 /// </summary> /// <param name="imgPixelFormat">原图片的PixelFormat</param> /// <returns></returns> private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat) { foreach (PixelFormat pf in indexedPixelFormats) { if (pf.Equals(imgPixelFormat)) return true; }
return false; }
#endregion
- 无法从带有索引像素格式的图像创建 Graphics 对象
- 无法从带有索引像素格式的图像创建 Graphics 对象。
- "无法从带有索引像素格式的图像创建graphics对象"
- 无法从带有索引像素格式的图像创建graphics对象
- 无法从带有索引像素格式的图像创建graphics对象
- 无法从带有索引像素格式的图像创建graphics对象
- Bitmap添加水印提示【无法从带有索引像素格式的图像创建Graphics对象】
- 无法从带有索引像素格式的图像创建graphics对象
- 无法从带有索引像素格式的图像创建graphics对象
- 绘图操作时报错“无法从带有索引像素格式的图像创建 Graphics 对象”
- .net下灰度模式图像在创建Graphics时出现:无法从带有索引像素格式的图像创建graphics对象 问题的解决方案。
- Graphics 不能建立有索引像素格式的对象
- 关于无法从带INDEX格式的GIF图片创建Graphics的解决方案
- c#_具有索引像素格式的图像不支持 SetPixel
- System.Drawing.Graphics读取带有索引的图片及思考
- 创建Graphics对象的方法及使用
- 创建Graphics对象的三种方法
- 将JSON对象带有格式的写出到文件中
- lua脚本之lua语言简介以及lua的安装
- JavaScript对select中option的隐藏和显示操作
- #pragma once与 #ifndef的区别
- 游戏服务器之锁
- rqnoj-2
- 无法从带有索引像素格式的图像创建graphics对象
- 原型模式(prototype)-通俗讲解
- PC机通过蓝牙SPP透明串口连接pn532成功
- OSPF路由选择
- Add third-party code into your project
- 7.用函数实现模块化程序设计
- 工程师素质拓展课程主页(2012级)
- ThinkPHP 的“自动填充”为什么失效?
- Tomcat假死报告