• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#生成透明背景图片的方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请


    /// <summary>
    /// Returns a transparent background GIF image from the specified Bitmap.
    /// </summary>
    /// <param name="bitmap">The Bitmap to make transparent.</param>
    /// <param name="color">The Color to make transparent.</param>
    /// <returns>New Bitmap containing a transparent background gif.</returns>
    public Bitmap MakeTransparentGif(Bitmap bitmap, Color color)
    {
        byte R = color.R;
        byte G = color.G;
        byte B = color.B;
        MemoryStream fin = new MemoryStream();
        bitmap.Save(fin, System.Drawing.Imaging.ImageFormat.Gif);
        MemoryStream fout = new MemoryStream((int)fin.Length);
        int count = 0;
        byte[] buf = new byte[256];
        byte transparentIdx = 0;
        fin.Seek(0, SeekOrigin.Begin);
        //header
        count = fin.Read(buf, 0, 13);
        if ((buf[0] != 71) || (buf[1] != 73) || (buf[2] != 70)) return null; //GIF
        fout.Write(buf, 0, 13);
        int i = 0;
        if ((buf[10] & 0x80) > 0)
        {
            i = 1 << ((buf[10] & 7) + 1) == 256 ? 256 : 0;
        }
        for (; i != 0; i--)
        {
            fin.Read(buf, 0, 3);
            if ((buf[0] == R) && (buf[1] == G) && (buf[2] == B))
            {
                transparentIdx = (byte)(256 - i);
            }
            fout.Write(buf, 0, 3);
        }
        bool gcePresent = false;
        while (true)
        {
            fin.Read(buf, 0, 1);
            fout.Write(buf, 0, 1);
            if (buf[0] != 0x21) break;
            fin.Read(buf, 0, 1);
            fout.Write(buf, 0, 1);
            gcePresent = (buf[0] == 0xf9);
            while (true)
            {
                fin.Read(buf, 0, 1);
                fout.Write(buf, 0, 1);
                if (buf[0] == 0) break;
                count = buf[0];
                if (fin.Read(buf, 0, count) != count) return null;
                if (gcePresent)
                {
                    if (count == 4)
                    {
                        buf[0] |= 0x01;
                        buf[3] = transparentIdx;
                    }
                }
                fout.Write(buf, 0, count);
            }
        }
        while (count > 0)
        {
            count = fin.Read(buf, 0, 1);
            fout.Write(buf, 0, 1);
        }
        fin.Close();
        fout.Flush();
        return new Bitmap(fout);
    }
    public void doTextImage(string FamilyName, string fontStyle, string FontWeight, string Text, int intFontSize, string FontColor, int Width, int Height, string TextAlign)
    {

        Bitmap bitmap = null;

        bitmap = new Bitmap(Width, Height);

        Graphics g = Graphics.FromImage(bitmap);

        //设置画笔

        Color color = ColorTranslator.FromHtml(FontColor);

        Brush brush = new SolidBrush(color);

        //设置字体

        FontStyle ftStyle = new FontStyle();

        if (fontStyle.ToLower() == "italic" && FontWeight.ToLower() == "bold")
        {

            ftStyle = FontStyle.Italic | FontStyle.Bold;

        }

        else if (fontStyle.ToLower() == "italic" && FontWeight.ToLower() != "bold")
        {

            ftStyle = FontStyle.Italic;

        }

        else if (fontStyle.ToLower() != "italic" && FontWeight.ToLower() == "bold")
        {

            ftStyle = FontStyle.Bold;

        }

        else
        {

            ftStyle = FontStyle.Regular;

        }

        Font font;

        font = new Font(FamilyName, intFontSize, ftStyle, GraphicsUnit.Pixel);

        //设置绘图区域

        RectangleF rect = new RectangleF(0, 0, Width, Height);

        //设置文本绘制格式

        StringFormat stringFormat = new StringFormat();

        if (TextAlign.ToLower() == "center")
        {

            stringFormat.LineAlignment = StringAlignment.Center;

        }

        else if (TextAlign.ToLower() == "right")
        {

            stringFormat.LineAlignment = StringAlignment.Far;

        }

        else
        {

            stringFormat.LineAlignment = StringAlignment.Near;

        }

        //设置绘图质量

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.SmoothingMode = SmoothingMode.HighQuality;

        g.PixelOffsetMode = PixelOffsetMode.HighQuality;

        g.CompositingMode = CompositingMode.SourceOver;

        g.CompositingQuality = CompositingQuality.HighQuality;

        g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

        g.PageUnit = GraphicsUnit.Pixel;

        //设置透明背景

        g.Clear(Color.Transparent);

        //绘制字符串

        g.DrawString(Text, font, brush, rect, stringFormat);

        //取浏览器版本,IE6显示透明gif,其它则显示透明png

        if (GetBrowsetype() == "IE" && GetBrowseVersion() == "6.0")
        {

            bitmap = MakeTransparentGif(bitmap, Color.Black);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

            byte[] bytes = ms.GetBuffer();

            g.Dispose();

            bitmap.Dispose();

            Response.Clear();

            Response.ContentType = "image/Gif";

            Response.BinaryWrite(bytes);

        }

        else
        {

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

            byte[] bytes = ms.GetBuffer();

            g.Dispose();

            bitmap.Dispose();

            Response.Clear();

            Response.ContentType = "image/Png";

            Response.BinaryWrite(bytes);

        }

    }
    public static string GetBrowseVersion()
    {

        System.Web.HttpBrowserCapabilities browser = System.Web.HttpContext.Current.Request.Browser;

        return browser.Version;

    }

    public static string GetBrowsetype()
    {

        System.Web.HttpBrowserCapabilities browser = System.Web.HttpContext.Current.Request.Browser;

        return browser.Browser;

    }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
AX2009C#客户端通过Webservice批量审核工作流(一)发布时间:2022-07-13
下一篇:
C#数据库连接字符串集合发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap