zoukankan      html  css  js  c++  java
  • GDI+ 实现 合成艺术字,并使艺术字透明

    1.Default.aspx 前台HTML源码,调用了3个js,实现自动选择颜色的功能

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFont.Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
        <style>
            body
            {
                font-size: 14px;
            }
        </style>

        <script type="text/javascript" src="veryide.core.js"></script>
    <script type="text/javascript" src="veryide.library.js"></script>
    <script type="text/javascript" src="veryide.color.js"></script>


    </head>
    <body>
        <form id="form1" runat="server">
        <div><div style="visibility:hidden;"><input name="sel1" visible="false" id="sel1"  type="text" onfocus="VeryIDE.Color(this)" style="vertical-align:middle;" />
        <input name="sel2" visible="false" id="sel2"  type="text" onfocus="VeryIDE.Color(this)" style="vertical-align:middle;" />
        <input name="sel3" visible="false" id="sel3"  type="text" onfocus="VeryIDE.Color(this)" style="vertical-align:middle;" />
       
        <input name="sel4" visible="false" id="sel4"  type="text" onfocus="VeryIDE.Color(this)" style="vertical-align:middle;" /></div>
           
            <table width="750" border="0" cellspacing="0" cellpadding="0" align="center" border="1">
                <tr>
                    <td width="150" height="25">
                        字体:
                    </td>
                    <td>
                        &nbsp;&nbsp;<asp:DropDownList ID="dl_fontStyle" runat="server">
                            <asp:ListItem>微软雅黑</asp:ListItem>
                            <asp:ListItem>方正舒体</asp:ListItem>
                            <asp:ListItem>方正琥珀简体</asp:ListItem>
                            <asp:ListItem>方正华隶简体</asp:ListItem>
                            <asp:ListItem>方正黄草简体</asp:ListItem>
                            <asp:ListItem Selected="True">方正超粗黑简体</asp:ListItem>
                            <asp:ListItem>汉仪醒示体简</asp:ListItem>
                            <asp:ListItem>汉仪雪峰体简</asp:ListItem>
                            <asp:ListItem>汉仪篆书繁</asp:ListItem>
                            <asp:ListItem>华文彩云</asp:ListItem>
                            <asp:ListItem>华文细黑</asp:ListItem>
                            <asp:ListItem>华文新魏</asp:ListItem>
                            <asp:ListItem>华文行楷</asp:ListItem>
                            <asp:ListItem>黑体</asp:ListItem>
                            <asp:ListItem>隶书</asp:ListItem>
                            <asp:ListItem>宋体</asp:ListItem>
                            <asp:ListItem>楷体</asp:ListItem>
                            <asp:ListItem>幼圆</asp:ListItem>
                            <asp:ListItem>仿宋</asp:ListItem>
                        </asp:DropDownList>
                        &nbsp;字体大小:<asp:DropDownList ID="dl_fontSize" runat="server">
                            <asp:ListItem>10</asp:ListItem>
                            <asp:ListItem>20</asp:ListItem>
                            <asp:ListItem Selected="True">30</asp:ListItem>
                            <asp:ListItem>40</asp:ListItem>
                            <asp:ListItem>50</asp:ListItem>
                            <asp:ListItem>60</asp:ListItem>
                            <asp:ListItem>70</asp:ListItem>
                            <asp:ListItem>80</asp:ListItem>
                            <asp:ListItem>90</asp:ListItem>
                        </asp:DropDownList>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td width="150" height="25">
                        输入文字:
                    </td>
                    <td>
                        &nbsp;&nbsp;<asp:TextBox ID="tbWord" runat="server" Width="235px" MaxLength="100">特效文字</asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="150" height="25">
                        文字样式:
                    </td>
                    <td>
                        &nbsp;<img src="spacer.gif" alt="选择颜色" id="test" runat="server" style="border:#ccc solid 1px;20px;height:20px;vertical-align:middle;" onclick="VeryIDE.Color(this,$('sel1'))" />
                       <%-- <asp:DropDownList ID="DropDownList2" runat="server">
                            <asp:ListItem>红色</asp:ListItem>
                            <asp:ListItem>黄色</asp:ListItem>
                            <asp:ListItem>蓝色</asp:ListItem>
                            <asp:ListItem>绿色</asp:ListItem>
                            <asp:ListItem>黑色</asp:ListItem>
                            <asp:ListItem>白色</asp:ListItem>
                        </asp:DropDownList>--%>
                    </td>
                </tr>
                <tr>
                    <td width="150" height="25">
                        特效:
                    </td>
                    <td>
                        &nbsp;
                        <asp:DropDownList ID="dl_tx" runat="server" AutoPostBack="True"
                            onselectedindexchanged="dl_tx_SelectedIndexChanged">
                            <asp:ListItem Value="0">无特效</asp:ListItem>
                            <asp:ListItem Value="1">投影</asp:ListItem>
                            <asp:ListItem Value="2">立体</asp:ListItem>
                            <asp:ListItem Value="3">浮雕</asp:ListItem>
                            <asp:ListItem Value="4">描边</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr id="mb" runat="server" visible="false"><td>描边颜色:</td><td><img src="spacer.gif" alt="选择颜色" id="Img3" runat="server" style="border:#ccc solid 1px;20px;height:20px;vertical-align:middle;" onclick="VeryIDE.Color(this,$('sel4'))" /></td></tr>
                <tr id="mytx" runat="server" visible="false">
                    <td width="150" height="25">
                        特效颜色:(阴影颜色)
                    </td>
                    <td>
                        &nbsp;<img src="spacer.gif" alt="选择颜色" id="Img2" runat="server" style="border:#ccc solid 1px;20px;height:20px;vertical-align:middle;" onclick="VeryIDE.Color(this,$('sel2'))" />
                       <%-- <asp:DropDownList ID="dl_txcolor" runat="server">
                            <asp:ListItem>红色</asp:ListItem>
                            <asp:ListItem>黄色</asp:ListItem>
                            <asp:ListItem>蓝色</asp:ListItem>
                            <asp:ListItem>绿色</asp:ListItem>
                            <asp:ListItem>黑色</asp:ListItem>
                            <asp:ListItem>白色</asp:ListItem>
                        </asp:DropDownList>--%>
                        厚度:<asp:DropDownList ID="dl_txhd" runat="server">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                            <asp:ListItem>4</asp:ListItem>
                            <asp:ListItem>5</asp:ListItem>
                            <asp:ListItem>6</asp:ListItem>
                            <asp:ListItem>7</asp:ListItem>
                            <asp:ListItem>8</asp:ListItem>
                            <asp:ListItem>9</asp:ListItem>
                            <asp:ListItem>10</asp:ListItem>
                            <asp:ListItem>11</asp:ListItem>
                            <asp:ListItem>12</asp:ListItem>
                            <asp:ListItem>13</asp:ListItem>
                            <asp:ListItem>14</asp:ListItem>
                            <asp:ListItem>15</asp:ListItem>
                            <asp:ListItem>16</asp:ListItem>
                            <asp:ListItem>17</asp:ListItem>
                            <asp:ListItem>18</asp:ListItem>
                            <asp:ListItem>19</asp:ListItem>
                            <asp:ListItem>20</asp:ListItem>                     
                        </asp:DropDownList>
                        &nbsp;角度:<asp:DropDownList ID="dl_txjd" runat="server">
                            <asp:ListItem Value="0">右下角</asp:ListItem>
                            <asp:ListItem Value="1">左上角</asp:ListItem>
                            <asp:ListItem Value="2">右上角</asp:ListItem>
                            <asp:ListItem Value="3">左下角</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td width="150" height="25">
                        背景样式:
                    </td>
                    <td>
                        &nbsp;
                        <asp:DropDownList ID="dl_bgStyle" runat="server" AutoPostBack="True"
                            onselectedindexchanged="dl_bgStyle_SelectedIndexChanged">
                            <asp:ListItem Value="1">透明</asp:ListItem>
                            <asp:ListItem Value="2">纯色</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr id="bgColor" runat="server" visible="false">
                    <td width="150" height="25">
                        背景颜色:
                    </td>
                    <td>
                        &nbsp;<img src="spacer.gif" alt="选择颜色" id="Img1" runat="server" style="border:#ccc solid 1px;20px;height:20px;vertical-align:middle;" onclick="VeryIDE.Color(this,$('sel3'))" />
                        <%--<asp:DropDownList ID="dl_bgColor" runat="server">
                            <asp:ListItem>红色</asp:ListItem>
                            <asp:ListItem>黄色</asp:ListItem>
                            <asp:ListItem>蓝色</asp:ListItem>
                            <asp:ListItem>绿色</asp:ListItem>
                            <asp:ListItem>黑色</asp:ListItem>
                            <asp:ListItem>白色</asp:ListItem>
                        </asp:DropDownList>--%>
                    </td>
                </tr>
                <tr>
                    <td width="150" height="25">
                        预览:
                    </td>
                    <td style="background-color:Gray;">
                        <asp:Image ID="Image1" runat="server" Visible="false" />
                    </td>
                </tr>
                <tr>
                    <td width="150" height="25">
                    </td>
                    <td>
                        <asp:Button ID="btn" runat="server" Text="提交" OnClick="btn_Click" Width="84px" />
                    </td>
                </tr>
            </table>
        </div>
        </form>
    </body>
    </html>
    2.Default.aspx 后台源码

     using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;

     protected void btn_Click(object sender, EventArgs e)
            {
                string fontcolor = Request["sel1"];
                string fontshadecolor = Request["sel2"];//阴影颜色
                string bgcolor = Request["sel3"];//背景颜色
                string mbcolor = Request["sel4"];//描边颜色
                Color fc=Color.Black;//字体颜色
                Color shadecolor=Color.Red;//阴影颜色
                Color bgColor=Color.White;//背景颜色
                Color mbColor = Color.YellowGreen;//描边颜色
                System.Drawing.ColorConverter c = new ColorConverter();
                if (fontcolor != "")
                {
                    fc = (System.Drawing.Color)c.ConvertFromString(fontcolor);//字体颜色
                }         
                if (fontshadecolor != "")
                {
                    shadecolor = (System.Drawing.Color)c.ConvertFromString(fontshadecolor);// 阴影颜色
                }
                if (bgcolor != "")
                {
                    bgColor = (System.Drawing.Color)c.ConvertFromString(bgcolor);//背景颜色
                }
                if (mbcolor != "")
                {
                    mbColor = (System.Drawing.Color)c.ConvertFromString(mbcolor);//描边颜色
                }
                string fontName = dl_fontStyle.SelectedValue.ToString();//字体名称
                int fontSize = Int32.Parse(dl_fontSize.SelectedItem.Text);//字体大小
                string fontText = tbWord.Text.Trim();//输入的文字
                int shadewidth = Int32.Parse(dl_txhd.SelectedItem.Text);//阴影厚度
                int shadejd = Int32.Parse(dl_txjd.SelectedValue.ToString());//阴影角度
                int tx = 0;//特效效果 默认无特效
                tx = Int32.Parse(dl_tx.SelectedValue.ToString());//特效效果
                int bgstyle = Int32.Parse(dl_bgStyle.SelectedValue.ToString());//背景样式
                float bmpWidth = 100, bmpHeight = 100;//生成图片的大小 根据字的宽度计算出来
                //**********************以下是计算字符串的宽度,高度,不是长度********************************************************//
                Bitmap bm = new Bitmap(250, 130, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                Font font = new Font(fontName, fontSize);
                Graphics g = Graphics.FromImage(bm);
                SizeF textSize;
                textSize = g.MeasureString(fontText, font);
                bmpWidth = textSize.Width+10;//字符串宽度
                bmpHeight = textSize.Height+10;//字符串高度
               // Response.Write("宽度:" + bmpWidth.ToString() + "<br/>高度是:" + bmpHeight.ToString() + "<br>长度是:" + fontText.Length.ToString());
                //***************************************************************************//
                string rev = "";
                switch (tx)
                {
                    case 0://无特效
                        rev=ImageArt.无特效(bmpWidth, bmpHeight, fontName, fontSize, fc, bgstyle, bgColor, fontText);
                        break;
                    case 1://投影
                        rev = ImageArt.投影(bmpWidth, bmpHeight, fontName, fontSize, fc, bgstyle, bgColor, fontText, shadecolor, shadewidth, shadejd);
                        break;
                    case 2://立体
                        rev = ImageArt.立体(bmpWidth, bmpHeight, fontName, fontSize, fc, bgstyle, bgColor, fontText, shadecolor, shadewidth, shadejd);
                        break;
                    case 3://浮雕
                        rev = ImageArt.浮雕(bmpWidth, bmpHeight, fontName, fontSize, fc, bgstyle, bgColor, fontText);
                        break;
                    case 4:
                        rev = ImageArt.描边(bmpWidth, bmpHeight, fontName, fontSize, fc, bgstyle, bgColor, fontText,mbColor);
                        break;
                }
                string path = Server.MapPath("upload/");
                if (rev != "")
                {
                    Image1.Visible = true;
                    Image1.ImageUrl = path + "\\" + rev;
                }
              
            }

     #region
            protected void dl_tx_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (dl_tx.SelectedValue.ToString() == "1" || dl_tx.SelectedValue.ToString() == "2")
                {
                    mytx.Visible = true;
                }
                else
                {
                    mytx.Visible = false;
                }
                if (dl_tx.SelectedValue.ToString() == "4")
                {
                    mb.Visible = true;
                }
                else
                {
                    mb.Visible = false;
                }
            }

            protected void dl_bgStyle_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (dl_bgStyle.SelectedValue == "2")
                {
                    bgColor.Visible = true;
                }
                else
                {
                    bgColor.Visible = false;
                }
            }
         #endregion

     3.合成艺术字类ImageArt.cs

    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Text;
    using System.Drawing.Text;

     /// <summary>
            /// 實現文字描邊
            /// </summary>
            /// <param name="words">文字</param>
            /// <param name="font">字體</param>
            /// <param name="wordColor">文字顏色</param>
            /// <param name="bordColor">邊框顏色</param>
            /// <returns>合成后圖片</returns>
            public static Bitmap DrawWords(String words, Font font, Color wordColor, Color bordColor, int bgStyle,Color bgColor)
            {
                Graphics g = Graphics.FromImage(new Bitmap(10, 10));
                SizeF size = g.MeasureString(words, font);
                Bitmap bmp = new Bitmap((int)size.Width + 2, (int)size.Height + 2);
                g = Graphics.FromImage(bmp);
                if (bgStyle == 1)//透明
                {
                    g.Clear(Color.White);        //赋予图像一个背景色 
                }
                else
                {
                    g.Clear(bgColor);//背景色
                }
                SolidBrush brush = new SolidBrush(wordColor);
                g.DrawString(words, font, brush, new PointF(1, 1));
                brush.Dispose();
                g.Dispose();
                GC.Collect();
                for (int i = 0; i < bmp.Width; i++)
                {
                    for (int j = 0; j < bmp.Height; j++)
                    {
                        if (!ColorEquals(bmp.GetPixel(i, j), wordColor))
                        {
                            if (i < bmp.Width - 1 && j < bmp.Height - 1)
                            {
                                if (ColorEquals(bmp.GetPixel(i + 1, j), wordColor) || ColorEquals(bmp.GetPixel(i, j + 1), wordColor) || ColorEquals(bmp.GetPixel(i + 1, j + 1), wordColor))
                                {
                                    bmp.SetPixel(i, j, bordColor);
                                }
                            }
                            if (i > 0 && j > 0)
                            {
                                if (ColorEquals(bmp.GetPixel(i - 1, j), wordColor) || ColorEquals(bmp.GetPixel(i, j - 1), wordColor) || ColorEquals(bmp.GetPixel(i - 1, j - 1), wordColor))
                                {
                                    bmp.SetPixel(i, j, bordColor);
                                }
                            }
                        }
                    }
                }
                return bmp;
            }
            private static bool ColorEquals(Color c1, Color c2)
            {
                if (c1.A != c2.A)
                    return false;
                if (c1.R != c2.R)
                    return false;
                if (c1.G != c2.G)
                    return false;
                if (c1.B != c2.B)
                    return false;
                return true;
            }

            #region 描边

     #region 描边
            public static string 描边(float bmpWidth, float bmpHeight, string fontName, int fontSize, Color fontColor, int bgStyle, Color bgColor, string fontText,Color mbColor)
            {         

                Font font = new Font(fontName, fontSize, FontStyle.Bold);     
              

                Bitmap bm = DrawWords(fontText, font, fontColor, mbColor, bgStyle,  bgColor);
             
                //********************************保存图片****************************//
                //设置保存路径
                string now = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string revImg = now + "1.GIF";
                string path = System.Web.HttpContext.Current.Server.MapPath("upload/");
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                bm.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + now + ".GIF"), System.Drawing.Imaging.ImageFormat.Gif);
                bm.Dispose();
                if (bgStyle == 1)
                {
                    //处理透明
                    //*************开始处理背景透明**********************//
                    Bitmap bm3 = (Bitmap)System.Drawing.Image.FromFile(path + "\\" + now + ".GIF");
                    Bitmap bm4 = ImageToTrans.MakeTransparentGif(bm3, Color.White);

                    if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                    bm4.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + revImg), System.Drawing.Imaging.ImageFormat.Gif);
                    bm4.Dispose();
                    bm3.Dispose();
                    if (File.Exists(path + "\\" + now + ".GIF"))
                    {
                        File.Delete(path + "\\" + now + ".GIF");
                    }
                }
                else
                {
                    //纯色
                    revImg = now + ".GIF";
                }
                //*************************************************//
                return revImg;
            }
            #endregion

      #region 浮雕效果
            /// <summary>
            /// 浮雕效果
            /// </summary>
            /// <param name="bmpWidth"></param>
            /// <param name="bmpHeiht"></param>
            /// <param name="fontName"></param>
            /// <param name="fontSize"></param>
            /// <param name="fontColor"></param>
            /// <param name="bgStyle"></param>
            /// <param name="bgColor"></param>
            /// <param name="fontText"></param>
            /// <returns></returns>
            public static string 浮雕(float bmpWidth, float bmpHeight, string fontName, int fontSize, Color fontColor, int bgStyle, Color bgColor, string fontText)
            {
                Bitmap bm = new Bitmap(Convert.ToInt32(bmpWidth), Convert.ToInt32(bmpHeight), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                //Color backcolor = bm.GetPixel(1, 1);
                //bm.MakeTransparent(backcolor);


                Brush backBrush = new SolidBrush(Color.Black);
                Brush foreBrush = new SolidBrush(fontColor);

                Font font = new Font(fontName, Convert.ToInt16(fontSize), FontStyle.Bold);
                Graphics g = Graphics.FromImage(bm);
                g.Clear(bgColor);

                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                string text = fontText.Trim();
                SizeF size = g.MeasureString(text, font);
                Single posX = (Convert.ToInt32(bmpWidth) - Convert.ToInt32(size.Width)) / 2;
                Single posY = (Convert.ToInt32(bmpHeight) - Convert.ToInt32(size.Height)) / 2;
                g.DrawString(text, font, backBrush, posX + 3, posY + 3);
                g.DrawString(text, font, foreBrush, posX, posY);

                //********************************保存图片****************************//
                //设置保存路径
                string now = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string revImg = now + "1.GIF";
                string path = System.Web.HttpContext.Current.Server.MapPath("upload/");
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                bm.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + now + ".GIF"), System.Drawing.Imaging.ImageFormat.Gif);
                bm.Dispose();
                if (bgStyle == 1)
                {
                    //处理透明
                    //*************开始处理背景透明**********************//
                    Bitmap bm3 = (Bitmap)System.Drawing.Image.FromFile(path + "\\" + now + ".GIF");
                    Bitmap bm4 = ImageToTrans.MakeTransparentGif(bm3, bgColor);

                    if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                    bm4.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + revImg), System.Drawing.Imaging.ImageFormat.Gif);
                    bm4.Dispose();
                    bm3.Dispose();
                    if (File.Exists(path + "\\" + now + ".GIF"))
                    {
                        File.Delete(path + "\\" + now + ".GIF");
                    }
                }
                else
                {
                    //纯色
                    revImg = now + ".GIF";
                }
                //*************************************************//
                return revImg;

            }
            #endregion

      #region 浮雕效果
            /// <summary>
            /// 浮雕效果
            /// </summary>
            /// <param name="bmpWidth"></param>
            /// <param name="bmpHeiht"></param>
            /// <param name="fontName"></param>
            /// <param name="fontSize"></param>
            /// <param name="fontColor"></param>
            /// <param name="bgStyle"></param>
            /// <param name="bgColor"></param>
            /// <param name="fontText"></param>
            /// <returns></returns>
            public static string 浮雕(float bmpWidth, float bmpHeight, string fontName, int fontSize, Color fontColor, int bgStyle, Color bgColor, string fontText)
            {
                Bitmap bm = new Bitmap(Convert.ToInt32(bmpWidth), Convert.ToInt32(bmpHeight), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                //Color backcolor = bm.GetPixel(1, 1);
                //bm.MakeTransparent(backcolor);


                Brush backBrush = new SolidBrush(Color.Black);
                Brush foreBrush = new SolidBrush(fontColor);

                Font font = new Font(fontName, Convert.ToInt16(fontSize), FontStyle.Bold);
                Graphics g = Graphics.FromImage(bm);
                g.Clear(bgColor);

                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                string text = fontText.Trim();
                SizeF size = g.MeasureString(text, font);
                Single posX = (Convert.ToInt32(bmpWidth) - Convert.ToInt32(size.Width)) / 2;
                Single posY = (Convert.ToInt32(bmpHeight) - Convert.ToInt32(size.Height)) / 2;
                g.DrawString(text, font, backBrush, posX + 3, posY + 3);
                g.DrawString(text, font, foreBrush, posX, posY);

                //********************************保存图片****************************//
                //设置保存路径
                string now = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string revImg = now + "1.GIF";
                string path = System.Web.HttpContext.Current.Server.MapPath("upload/");
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                bm.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + now + ".GIF"), System.Drawing.Imaging.ImageFormat.Gif);
                bm.Dispose();
                if (bgStyle == 1)
                {
                    //处理透明
                    //*************开始处理背景透明**********************//
                    Bitmap bm3 = (Bitmap)System.Drawing.Image.FromFile(path + "\\" + now + ".GIF");
                    Bitmap bm4 = ImageToTrans.MakeTransparentGif(bm3, bgColor);

                    if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                    bm4.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + revImg), System.Drawing.Imaging.ImageFormat.Gif);
                    bm4.Dispose();
                    bm3.Dispose();
                    if (File.Exists(path + "\\" + now + ".GIF"))
                    {
                        File.Delete(path + "\\" + now + ".GIF");
                    }
                }
                else
                {
                    //纯色
                    revImg = now + ".GIF";
                }
                //*************************************************//
                return revImg;

            }
            #endregion

    #region 立体效果
            /// <summary>
            /// 立体效果图
            /// </summary>
            /// <param name="bmpWidth">画布宽度</param>
            /// <param name="bmpHeight">画布高度</param>
            /// <param name="fontName">字体名称</param>
            /// <param name="fontSize">字体大小</param>
            /// <param name="fontColor">字体颜色</param>
            /// <param name="bgStyle">字体样式</param>
            /// <param name="bgColor">背景颜色</param>
            /// <param name="fontText">输入的字体</param>
            /// <param name="shadeColor">阴影颜色</param>
            /// <param name="shadeHd">阴影厚度</param>
            /// <param name="shadeJd">阴影角度</param>
            /// <returns></returns>
            public static string 立体(float bmpWidth, float bmpHeight, string fontName, int fontSize, Color fontColor, int bgStyle, Color bgColor, string fontText, Color shadeColor, int shadeHd, int shadeJd)
            {
                Bitmap bm = new Bitmap(Convert.ToInt32(bmpWidth), Convert.ToInt32(bmpHeight), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                int i = 0;
                Brush backBrush = new SolidBrush(shadeColor);//阴影画刷
                Brush foreBrush = new SolidBrush(fontColor);
                Font font = new Font(fontName, System.Convert.ToInt16(fontSize), FontStyle.Regular);
                Graphics g = Graphics.FromImage(bm);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                if (bgStyle == 1)//透明
                {
                    g.Clear(Color.White);        //赋予图像一个背景色 
                }
                else
                {
                    g.Clear(bgColor);//背景色
                }
               
                SizeF size = g.MeasureString(fontText, font);
                Single posX = (Convert.ToInt32(bmpWidth) - Convert.ToInt16(size.Width)) / 2;
                Single posY = (Convert.ToInt32(bmpHeight) - Convert.ToInt16(size.Height)) / 3;
                //左下角
                //********************************************//
                while (i < Convert.ToInt16(shadeHd))//20
                {
                    //g.DrawString(text, font, backBrush, posX - i, posY + i);//左下角
                    //g.DrawString(text, font, backBrush, posX - i, posY - i);//左上角
                    // g.DrawString(text, font, backBrush, posX + i, posY - i);//右上角
                   
                    if (shadeJd == 0)//右下角
                    {
                        g.DrawString(fontText, font, backBrush, posX + i, posY + i);//右下角
                    }
                    else if (shadeJd == 1)//左上角
                    {
                        g.DrawString(fontText, font, backBrush, posX - i, posY - i);//左上角
                    }
                    else if (shadeJd == 2)//右上角
                    {
                        g.DrawString(fontText, font, backBrush, posX + i, posY - i);//右上角
                    }
                    else if (shadeJd == 3)//左下角
                    {
                        g.DrawString(fontText, font, backBrush, posX - i, posY + i);//左下角
                    }
                        i = i + 1;
                }
                //******************************************//
                g.DrawString(fontText, font, foreBrush, posX, posY);
                g.Dispose();


                //********************************保存图片****************************//
                //设置保存路径
                string now = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string revImg = now + "1.GIF";
                string path = System.Web.HttpContext.Current.Server.MapPath("upload/");
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                bm.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + now + ".GIF"), System.Drawing.Imaging.ImageFormat.Gif);
                bm.Dispose();
                if (bgStyle == 1)
                {
                    //处理透明
                    //*************开始处理背景透明**********************//
                    Bitmap bm3 = (Bitmap)System.Drawing.Image.FromFile(path + "\\" + now + ".GIF");
                    Bitmap bm4 = ImageToTrans.MakeTransparentGif(bm3, Color.White);

                    if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                    bm4.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + revImg), System.Drawing.Imaging.ImageFormat.Gif);
                    bm4.Dispose();
                    bm3.Dispose();
                    if (File.Exists(path + "\\" + now + ".GIF"))
                    {
                        File.Delete(path + "\\" + now + ".GIF");
                    }
                }
                else
                {
                    //纯色
                    revImg = now + ".GIF";
                }
                //*************************************************//
                return revImg;
            }
            #endregion
            #region 投影各种效果
            /// <summary>
            /// 投影效果艺术字
            /// </summary>
            /// <param name="bmpWidth">图片宽度</param>
            /// <param name="bmpHeight">图片高度</param>
            /// <param name="fontName">字体名称</param>
            /// <param name="fontSize">字体大小</param>
            /// <param name="fontColor">字体颜色</param>
            /// <param name="bgStyle">背景样式</param>
            /// <param name="bgColor">背景颜色</param>
            /// <param name="fontText">输入的文字</param>
            /// <param name="shadeColor">阴影颜色</param>
            /// <param name="shadeHd">阴影厚度</param>
            /// <param name="shadeJd">阴影角度</param>
            /// <returns></returns>
            public static string 投影(float bmpWidth,float bmpHeight,string fontName,int fontSize,Color fontColor,int bgStyle,Color bgColor,string fontText,Color shadeColor,int shadeHd,int shadeJd)
            {
                //先画一个矩形画布
                RectangleF rectF = new RectangleF(10, 10, bmpWidth, bmpHeight);
                Bitmap bm = new Bitmap(Convert.ToInt32(bmpWidth), Convert.ToInt32(bmpHeight), PixelFormat.Format32bppArgb);
                Font font = new Font(fontName, fontSize, FontStyle.Bold);
                Graphics g = Graphics.FromImage(bm);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                if (bgStyle == 1)//透明
                {
                    g.Clear(Color.White);        //赋予图像一个背景色 
                }
                else
                {
                    g.Clear(bgColor);//背景色
                }
                SolidBrush fgBrush = new SolidBrush(fontColor);//字体画刷
                SolidBrush bgBrush = new SolidBrush(bgColor);//背景画刷

                 StringFormat format = new StringFormat();
                 format.Alignment=StringAlignment.Near;
                g.FillRectangle(bgBrush, rectF);
                int shadejd = shadeJd;//阴影角度
                int shadehd = shadeHd;//阴影厚度
                if (shadejd == 0)//右下角
                {
                    rectF.Offset(+shadehd, +shadehd);
                    g.DrawString(fontText, font, new SolidBrush(shadeColor), rectF, format);
                    rectF.Offset(-shadehd, -shadehd);
                }
                else if (shadejd == 1)//左上角
                {
                    rectF.Offset(-shadehd, -shadehd);
                    g.DrawString(fontText, font, new SolidBrush(shadeColor), rectF, format);
                    rectF.Offset(+shadehd, +shadehd);
                }
                else if (shadejd == 2)//右上角
                {
                    rectF.Offset(+shadehd, -shadehd);
                    g.DrawString(fontText, font, new SolidBrush(shadeColor), rectF, format);
                    rectF.Offset(-shadehd, +shadehd);
                }
                else if(shadejd==3)//左下角
                {             

                    rectF.Offset(-shadehd, +shadehd);
                    g.DrawString(fontText, font, new SolidBrush(shadeColor), rectF, format);
                    rectF.Offset(+shadehd, -shadehd);
                }
                g.DrawString(fontText, font, fgBrush, rectF, format);
                g.Dispose();
                //********************************保存图片****************************//
                //设置保存路径
                string now = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string revImg = now + "1.GIF";
                string path = System.Web.HttpContext.Current.Server.MapPath("upload/");
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                bm.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + now + ".GIF"), System.Drawing.Imaging.ImageFormat.Gif);
                bm.Dispose();
                if (bgStyle == 1)
                {
                    //处理透明
                    //*************开始处理背景透明**********************//
                    Bitmap bm3 = (Bitmap)System.Drawing.Image.FromFile(path + "\\" + now + ".GIF");
                    Bitmap bm4 = ImageToTrans.MakeTransparentGif(bm3, Color.White);

                    if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                    bm4.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + revImg), System.Drawing.Imaging.ImageFormat.Gif);
                    bm4.Dispose();
                    bm3.Dispose();
                    if (File.Exists(path + "\\" + now + ".GIF"))
                    {
                        File.Delete(path + "\\" + now + ".GIF");
                    }
                }
                else
                {
                    //纯色
                    revImg = now + ".GIF";
                }
                //*************************************************//
                return revImg;


            }
            #endregion

     #region 无特效艺术字
            /// <summary>
            /// 无特效生成艺术字
            /// </summary>
            /// <param name="bmpWidth">需要生成图片宽度</param>
            /// <param name="bmpHeight">需要生成图片高度</param>
            /// <param name="fontName">字体名称</param>
            /// <param name="fontSize">字体大小</param>
            /// <param name="fontColor">字体颜色</param>
            /// <param name="bgStyle">背景样式</param>
            /// <param name="bgColor">背景颜色</param>
            ///  <param name="fontText">需要操作的文字</param>
            /// <returns></returns>
            public static string 无特效(float bmpWidth,float bmpHeight,string fontName,int fontSize,Color fontColor,
                int bgStyle,Color bgColor,string fontText)
            {
                Bitmap bm = new Bitmap(Convert.ToInt32(bmpWidth), Convert.ToInt32(bmpHeight), System.Drawing.Imaging.PixelFormat.Format32bppArgb);//需要绘图的bmp

                Font font = new Font(fontName, fontSize, FontStyle.Bold);

                Graphics g = Graphics.FromImage(bm);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                //g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                if (bgStyle == 1)//透明
                {
                    g.Clear(Color.White);        //赋予图像一个背景色 
                }
                else
                {
                    g.Clear(bgColor);//背景色
                }

                SizeF textSize;
                textSize = g.MeasureString(fontText, font);

                g.DrawString(fontText, font, new SolidBrush(fontColor), (bmpWidth - textSize.Width) / 2, (bmpHeight - textSize.Height) / 2);
                g.Dispose();
                //设置保存路径
                string now = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string revImg = now + "1.GIF";
                string path =System.Web.HttpContext.Current.Server.MapPath("upload/");
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                bm.Save(System.Web.HttpContext.Current.Server.MapPath("upload/"+now+".GIF"), System.Drawing.Imaging.ImageFormat.Gif);
                bm.Dispose();
                if (bgStyle == 1)
                {
                    //处理透明
                    //*************开始处理背景透明**********************//
                    Bitmap bm3 = (Bitmap)System.Drawing.Image.FromFile(path + "\\" + now + ".GIF");
                    Bitmap bm4 = ImageToTrans.MakeTransparentGif(bm3, Color.White);

                    if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                    bm4.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + revImg), System.Drawing.Imaging.ImageFormat.Gif);
                    bm4.Dispose();
                    bm3.Dispose();
                    if (File.Exists(path + "\\" + now + ".GIF"))
                    {
                        File.Delete(path + "\\" + now + ".GIF");
                    }
                }
                else
                {
                    //纯色
                    revImg = now + ".GIF";
                }
                return revImg;
                //******************处理透明结束********************//
            }
            #endregion

     #region 无特效艺术字
            /// <summary>
            /// 无特效生成艺术字
            /// </summary>
            /// <param name="bmpWidth">需要生成图片宽度</param>
            /// <param name="bmpHeight">需要生成图片高度</param>
            /// <param name="fontName">字体名称</param>
            /// <param name="fontSize">字体大小</param>
            /// <param name="fontColor">字体颜色</param>
            /// <param name="bgStyle">背景样式</param>
            /// <param name="bgColor">背景颜色</param>
            ///  <param name="fontText">需要操作的文字</param>
            /// <returns></returns>
            public static string 无特效(float bmpWidth,float bmpHeight,string fontName,int fontSize,Color fontColor,
                int bgStyle,Color bgColor,string fontText)
            {
                Bitmap bm = new Bitmap(Convert.ToInt32(bmpWidth), Convert.ToInt32(bmpHeight), System.Drawing.Imaging.PixelFormat.Format32bppArgb);//需要绘图的bmp

                Font font = new Font(fontName, fontSize, FontStyle.Bold);

                Graphics g = Graphics.FromImage(bm);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                //g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                if (bgStyle == 1)//透明
                {
                    g.Clear(Color.White);        //赋予图像一个背景色 
                }
                else
                {
                    g.Clear(bgColor);//背景色
                }

                SizeF textSize;
                textSize = g.MeasureString(fontText, font);

                g.DrawString(fontText, font, new SolidBrush(fontColor), (bmpWidth - textSize.Width) / 2, (bmpHeight - textSize.Height) / 2);
                g.Dispose();
                //设置保存路径
                string now = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                string revImg = now + "1.GIF";
                string path =System.Web.HttpContext.Current.Server.MapPath("upload/");
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                bm.Save(System.Web.HttpContext.Current.Server.MapPath("upload/"+now+".GIF"), System.Drawing.Imaging.ImageFormat.Gif);
                bm.Dispose();
                if (bgStyle == 1)
                {
                    //处理透明
                    //*************开始处理背景透明**********************//
                    Bitmap bm3 = (Bitmap)System.Drawing.Image.FromFile(path + "\\" + now + ".GIF");
                    Bitmap bm4 = ImageToTrans.MakeTransparentGif(bm3, Color.White);

                    if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                    bm4.Save(System.Web.HttpContext.Current.Server.MapPath("upload/" + revImg), System.Drawing.Imaging.ImageFormat.Gif);
                    bm4.Dispose();
                    bm3.Dispose();
                    if (File.Exists(path + "\\" + now + ".GIF"))
                    {
                        File.Delete(path + "\\" + now + ".GIF");
                    }
                }
                else
                {
                    //纯色
                    revImg = now + ".GIF";
                }
                return revImg;
                //******************处理透明结束********************//
            }
            #endregion

    其中用到的一个透明类,下篇继续。。。并有完整事列下载,如下载失效请联系 ggbbeyou@126.com

  • 相关阅读:
    英语生活箴言
    Javascript中最常用的55个经典技巧
    深刻理解Java编程的7个例子
    定制Apache索引样式
    【Androidin全球首发】国产Android Broncho A1 评测,第一印象
    系统程序员成长计划写得又快又好的秘诀(五)
    让adb logcat打印内核调试信息
    系统程序员成长计划写得又快又好的秘诀(三)
    Projects owned by limodev.cn
    Apache Direcotry Indexes目录列表显示样式定制
  • 原文地址:https://www.cnblogs.com/ggbbeyou/p/1356866.html
Copyright © 2011-2022 走看看