"无法从带有索引像素格式的图像创建graphics对象" 解决
大家在用 .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("图片路径")
原因是因为图片是索引像素格式的。为了避免此问题的发生,我们在做水印之前,可以先判断原图片是否是索引像素格式的,如果是,则可以采用将此图片先clone到一张BMP上的方法来解决:
1
/// <summary>
2
/// 会产生graphics异常的PixelFormat
3
/// </summary>
4
private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
5
PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
6
PixelFormat.Format8bppIndexed
7
};
8
9
/// <summary>
10
/// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
11
/// </summary>
12
/// <param name="imgPixelFormat">原图片的PixelFormat</param>
13
/// <returns></returns>
14
private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
15
{
16
foreach (PixelFormat pf in indexedPixelFormats)
17
{
18
if (pf.Equals(imgPixelFormat)) return true;
19
}
20
21
return false;
22
}
23
24
//

使用
25
using (Image img = Image.FromFile("原图片路径"))
26
{
27
//如果原图片是索引像素格式之列的,则需要转换
28
if (IsPixelFormatIndexed(img.PixelFormat))
29
{
30
Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
31
using (Graphics g = Graphics.FromImage(bmp))
32
{
33
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
34
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
35
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
36
g.DrawImage(img, 0, 0);
37
}
38
//下面的水印操作,就直接对 bmp 进行了
39
//

40
}
41
else //否则直接操作
42
{
43
//直接对img进行水印操作
44
}
45
}
/// <summary>2
/// 会产生graphics异常的PixelFormat3
/// </summary>4
private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,5
PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,6
PixelFormat.Format8bppIndexed7
};8

9
/// <summary>10
/// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中11
/// </summary>12
/// <param name="imgPixelFormat">原图片的PixelFormat</param>13
/// <returns></returns>14
private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)15
{16
foreach (PixelFormat pf in indexedPixelFormats)17
{18
if (pf.Equals(imgPixelFormat)) return true;19
}20

21
return false;22
}23

24
//

使用25
using (Image img = Image.FromFile("原图片路径"))26
{27
//如果原图片是索引像素格式之列的,则需要转换28
if (IsPixelFormatIndexed(img.PixelFormat))29
{30
Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);31
using (Graphics g = Graphics.FromImage(bmp))32
{33
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;34
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;35
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;36
g.DrawImage(img, 0, 0);37
}38
//下面的水印操作,就直接对 bmp 进行了39
//

40
}41
else //否则直接操作42
{43
//直接对img进行水印操作44
}45
}
经过上面的操作, 就可以避免因图片的索引信息而引发异常了。
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122
做人要厚道,转载请注明出处!

浙公网安备 33010602011771号