zoukankan      html  css  js  c++  java
  • C#生成透明背景图片的方法


        /// <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;

        }

  • 相关阅读:
    Laravel 出现 No application encryption key has been specified.
    windows下用composer局部安装laravel
    vue组件--通讯录
    vue组件--TagsInput
    axios封装(二)队列管理
    axios封装(一)基础配置
    [git hooks] pre-commit 配置
    qs.js
    flexbox的应用
    盒子模型详解
  • 原文地址:https://www.cnblogs.com/QGame/p/2344355.html
Copyright © 2011-2022 走看看