首页 » .Net, Asp.Net, C# » 阅读文章

无法从带有索引像素格式的图像创建graphics对象|A Graphics object cannot be created from an image that has an indexed pixel format

2008年12月19日

接受 无名 网友的建议,对原来的解决方法进行了改进,采用了判断图片 PixelFormat 的方式,来解决此问题

 

大家在用 .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, 我们可以看到如下的提示信息:


通过上面的错误解释,我们可以看到,原因是因为图片是索引像素格式的。为了避免此问题的发生,我们在做水印之前,可以先判断原图片是否是索引像素格式的,如果是,则可以采用将此图片先clone到一张BMP上的方法来解决:

/// <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进行水印操作
    }
}

经过上面的操作, 就可以避免因图片的索引信息而引发异常了。


和别人一起分享吧:
  • Google Bookmarks
  • QQ书签
  • 饭否
  • 校内网
  • 豆瓣九点
  • 嘀咕
  • 365Key网摘
  • POCO网摘
  • 豆瓣
  • 开心网
  • 新浪ViVi
  • 百度搜藏
  • email
  • Add to favorites
  • Facebook
  • Live
  • 收客
  • Twitter
  • del.icio.us
  • PDF
  • Yigg
  • Print
  • Digg
  • Haohao
  • RSS
  • Yahoo! Bookmarks

本文作者:三角猫 DeltaCat
文章出处:真有意思网(http://www.zu14.cn)
引用地址:点击复制本文的 Trackback Url
文章链接:http://www.zu14.cn/2008/12/19/net_gif_index_error/ [复制] (转载请注明出处及链接)

.Net, Asp.Net, C# , , , ,

上一篇 »:

下一篇 »:
  1. | #1

    @shen :
    水印没有加上,应该是你添加水印的代码有问题,和上面的代码应该无关

  2. shen
    | #2

    我试了,不行啊,虽然不报异常了,不过水印没有添加上啊

  3. | #3

    @无名 :
    呵呵,感谢支持
    春节快乐!

  4. 无名
    | #4

    很好 这样就完善了 具有了实用性。 加油。祝新年快乐!

  5. | #5

    @无名 :
    检测方法已经更正,希望多交流,祝新年快乐!

  6. | #6

    @无名
    呵呵,谢谢,方法我已经更正了, 采用了检测 图片的 PixelFormat 来解决此问题

  7. 无名
    | #7

    不敢说指点 只是我的一点小建议 希望你完善。

    我也早有此想法只是一直没有做过,等你做好了我用(*^__^*) 嘻嘻……

  8. 无名
    | #8

    判断应该是排除性的。
    gif动画加水印是可以实现的。
    cmyk的图片水印也会出现这个问题。
    并且 图片的格式被改掉的话 也会出现。

    最好是try

    • | #9

      谢谢指点。
      我写这篇blog,并不是说GIF不能加水印,也不是说所有的gif都有这个问题,而其他的格式没有。
      只是说,使用gif的时候,可能会遇到此问题(我就遇到了,所以共享出来了,呵呵)
      其实,根据 exception 的描述,解决的比较完美的方法,应该是先分析图片的 pixelformat,看是否有 indexed.

  1. 目前还没有任何 trackbacks 和 pingbacks.

 

Related Posts with Thumbnails