System.Drawing.Graphics读取带有索引的图片及思考

用.NET的System.Drawing.Graphics来加载带有索引的图片,比如带有透明色的GIF图片,会出现“A Graphics object cannot be created from an image that has an indexed pixel format”的错误。上网搜索了一番,找到了变通解决的方法

代码如下:

using (Bitmap image = new Bitmap(Server.MapPath("1.gif")))
{
    // Size the new bitmap to source image's dimension.
    using (Bitmap frame = new Bitmap(image.Width, image.Height))
    {
        // Uses temp frame to write on g.
        using (Graphics g = Graphics.FromImage(frame))
        {
            g.Clear(Color.Red);
            //g.Clear(Color.Transparent);

            // Draw the original indexed image's content to the temp frame.
            // Paint the entire region of original image to the temp frame.
            // Use the rectangle type to select area of source image.
            g.DrawImage(image, new Rectangle(0, 0, frame.Width, frame.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

            Font f = new Font("Verdana", 30);
            Brush b = new SolidBrush(Color.Black);
            string addText = "ˮӡ";
            g.DrawString(addText, f, b, 400, 100);

            Response.Clear();
            Response.ContentType = "image/bmp";

            frame.Save(Response.OutputStream, ImageFormat.Jpeg);
        }
    }
}

就是不直接读取这个GIF,新建一个和它同样大小的Bitmap,用Graphics把图像画上去。但在输出图像的发现透明的地方变成了黑色,当我把Graphics清成红色时,输出的图像在透明处变成了红色,说明GIF的透明起作用了。由此引出了我的考虑:难道空的Graphics是黑色的,为什么不是透明色呢?

经过我的大量尝试和搜索,大概只用FCL是不能生成透明的GIF了,不知哪位大大有其他方法或是类库,还望赐教。

转载于:https://www.cnblogs.com/superstone/archive/2008/02/15/1070105.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值