indexed pixel format 图片格式显示出错

本文介绍了一种解决在.NET中创建Graphics对象时遇到的indexedpixel格式问题的方法。通过创建新的Bitmap对象,并将原图像内容复制到新对象上,可以转换图像格式,避免异常发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误描述:A Graphics object cannot be created from an image that has an indexed pixel

 

解决思路:做一个同样尺寸的bitmap,将“indexed pixel format”格式的图片复制过来,变为一个非“indexed pixel format”的图片

 

实现代码:

protected void Page_Load(object sender, EventArgs e)
    {
            if(Page.IsPostBack ==false)
            {
            string file = "mailbanner.gif";
            string addText = "上海星移企业邮局";

            try
            {
                addText = Request.QueryString["mark"].ToString();   
            }
            catch(Exception)
            {
            }

            //载入原始图片
            Bitmap bmp = new Bitmap(Server.MapPath(file));
            Graphics graphics;
            try
            {
                //试图取得Graphics对象,用于添加文字

                //对于indexed pixel format,此处会抛出异常
                graphics = System.Drawing.Graphics.FromImage(bmp);
            }
            catch (Exception)
            {

                //对于indexed pixel format,抛出异常的特殊处理
                Bitmap newBmp = new Bitmap(bmp.Width, bmp.Height);
                graphics = System.Drawing.Graphics.FromImage(newBmp); 
                graphics.DrawImage(bmp,
                                   new Rectangle(0, 0, newBmp.Width, newBmp.Height),
                                   new Rectangle(0, 0, bmp.Width, bmp.Height),
                                   GraphicsUnit.Pixel);
                bmp = newBmp;

            }

            graphics.DrawString(addText,
                new Font("Verdana", 14, FontStyle.Regular ),
                new SolidBrush(Color.Gray), 181, 9);
           
            graphics.DrawString(addText,
                new Font("Verdana", 14, FontStyle.Regular),
                new SolidBrush(Color.Goldenrod  ), 180, 8);

            graphics.Dispose();

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            //网上的例程,通常是 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)

            //色彩失真明显

            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            bmp.Dispose();

            Response.ClearContent();
            Response.ContentType = "image/bmp";
            Response.BinaryWrite(ms.ToArray());
        }

    }

 

 

转载于:https://www.cnblogs.com/chcnblogs/archive/2008/07/21/1247510.html

### C# 中 PixelFormat 的使用与定义 在 C# 和 GDI+ 图像处理中,`PixelFormat` 是一个枚举类型,用于描述图像像素的数据格式。它决定了如何存储颜色信息以及每个像素占用的位数。以下是关于 `PixelFormat` 使用和定义的一些重要细节: #### 定义 `PixelFormat` 枚举位于命名空间 `System.Drawing.Imaging` 下,主要用于指定图像数据的颜色深度和通道布局。常见的值包括但不限于以下几种[^1]: - **Indexed Formats**: 表示索引颜色模式,例如 `Format8bppIndexed`。 - **Grayscale Formats**: 如 `Format8bppGrayScale`,表示灰度图。 - **RGB Formats**: 像素由红、绿、蓝三个分量组成,例如 `Format24bppRgb` 或 `Format32bppArgb`。 这些格式直接影响内存中的图像数据结构及其性能表现。 #### 使用场景 当创建或操作 Bitmap 对象时,可以设置其 `PixelFormat` 属性来控制图像的质量和大小。例如,在加载图片时可以通过以下方式获取当前格式: ```csharp using System.Drawing; using System.Drawing.Imaging; Bitmap bitmap = new Bitmap("example.png"); PixelFormat pixelFormat = bitmap.PixelFormat; // 获取现有图像的 PixelFormat Console.WriteLine(pixelFormat); ``` 如果需要转换图像到特定的 `PixelFormat`,则可通过克隆实现: ```csharp Bitmap convertedBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(convertedBitmap); g.DrawImage(bitmap, Point.Empty); // 绘制原图至新格式 g.Dispose(); ``` 注意,某些格式可能不支持透明度(Alpha 通道),因此在选择合适的 `PixelFormat` 时需考虑目标用途[^2]。 #### 性能考量 不同的 `PixelFormat` 可显著影响渲染速度和文件尺寸。对于实时图形应用而言,通常推荐采用更高效率但较低精度的格式;而对于高质量打印,则应优先选用高分辨率且带 Alpha 通道的支持选项。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值