大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > ASP.NET技巧 > 无法从带有索引像素格式的图像创建graphics对象解决方法

无法从带有索引像素格式的图像创建graphics对象解决方法


[摘要]本文以代码的方式解释了“无法从带有索引像素格式的图像创建graphics对象”的解决方法,供大家参考!

本文以代码的方式解释了“无法从带有索引像素格式的图像创建graphics对象”的解决方法,下面是源码:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;

using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

#region 在图片上生成图片水印
        /// <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



相关评论