先上个效果图看看:
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %> <!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 id="Head1" runat="server"> <title>ASP.NET 图片生成器</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btn_upload" runat="server" Text="upload" OnClick="btn_upload_Click" /> <br /> <br /> <strong>File list:</strong><br /> <asp:ListBox ID="lb_ImageList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="lb_ImageList_SelectedIndexChanged" Width="566px" Height="237px"> </asp:ListBox> <br /> <br /> <asp:Image ID="img_Src" runat="server" /> <asp:Button ID="btn_Delete1" runat="server" Text="Delete" OnClick="btn_Delete1_Click" /><br /> <br /> <strong> <asp:RadioButton GroupName="func" ID="rb_Text" runat="server" Text="Enable Adding Text:" Checked="True" /> <br /> </strong>Text <asp:TextBox ID="tb_Text" runat="server"></asp:TextBox> Color <asp:TextBox ID="tb_Color" runat="server" Width="80px">#2BC4F6</asp:TextBox> Size <asp:TextBox ID="tb_Size" runat="server" Width="60px">18</asp:TextBox> Font <asp:TextBox ID="tb_Font" runat="server" Width="100px">ubisofttitletwo</asp:TextBox><br /> <br /> <strong> <asp:RadioButton GroupName="func" ID="rb_Watermark" runat="server" Text="Enable Watermark:" /><br /> </strong>Background Color <asp:TextBox ID="tb_bgcolor" runat="server" Width="80px">#000000</asp:TextBox> Transparence <asp:TextBox ID="tb_transparence" runat="server" Width="50px">0.4</asp:TextBox><br /> Line1 Text <asp:TextBox ID="tb_Text1" runat="server">音乐让我说 - 博客园</asp:TextBox> Color <asp:TextBox ID="tb_Color1" runat="server" Width="80px">#ffffff</asp:TextBox> Size <asp:TextBox ID="tb_Size1" runat="server" Width="60px">14</asp:TextBox> Font<asp:TextBox ID="tb_Font1" runat="server" Width="100px">Arial</asp:TextBox><br /> Line2 Text <asp:TextBox ID="tb_Text2" runat="server">Http://music.cnblogs.com</asp:TextBox> Color <asp:TextBox ID="tb_Color2" runat="server" Width="80px">#ffffff</asp:TextBox> Size <asp:TextBox ID="tb_Size2" runat="server" Width="60px">14</asp:TextBox> Font<asp:TextBox ID="tb_Font2" runat="server" Width="100px">Arial</asp:TextBox><br /> <br /> <strong><asp:RadioButton GroupName="func" ID="rb_Corner" runat="server" Text="Enable Rounded corner:" /><br /> </strong>location <asp:DropDownList ID="ddl_CornerLocation" runat="server"> <asp:ListItem>TopLeft</asp:ListItem> <asp:ListItem>TopRight</asp:ListItem> <asp:ListItem>BottomLeft</asp:ListItem> <asp:ListItem>BottomRight</asp:ListItem> </asp:DropDownList> <br /> <br /> <asp:Button ID="btn_Generate" runat="server" Text="generate" OnClick="btn_Generate_Click" /> </div> </form> </body> </html>
后台代码:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; public partial class Index : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DirectoryInfo di = new DirectoryInfo(Server.MapPath(ConfigurationManager.AppSettings["uploadpath"])); this.lb_ImageList.DataSource = di.GetFiles("*.jpg"); this.lb_ImageList.DataBind(); } } protected void btn_upload_Click(object sender, EventArgs e) { string sFileName = this.FileUpload1.FileName; if (Path.GetExtension(sFileName).ToLower() != ".jpg") { ClientScriptManager csm = Page.ClientScript; csm.RegisterStartupScript(this.GetType(), "", "<script type=\"text/javascript\">alert('System can only handle jpg file!')</script>"); } else { string sFileFullPath = Server.MapPath(ConfigurationManager.AppSettings["uploadpath"] + sFileName); this.FileUpload1.SaveAs(sFileFullPath); this.lb_ImageList.Items.Add(sFileName); this.lb_ImageList.SelectedIndex = -1; this.lb_ImageList.Items.FindByValue(sFileName).Selected = true; this.img_Src.ImageUrl = ConfigurationManager.AppSettings["uploadpath"] + sFileName; } } protected void lb_ImageList_SelectedIndexChanged(object sender, EventArgs e) { this.img_Src.ImageUrl = ConfigurationManager.AppSettings["uploadpath"] + this.lb_ImageList.SelectedValue; ; } protected void btn_Generate_Click(object sender, EventArgs e) { string sFileName = this.lb_ImageList.SelectedValue; string prefix = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); string sSrcFilePath = Server.MapPath(ConfigurationManager.AppSettings["uploadpath"] + sFileName); string sDstFilePath = Server.MapPath(ConfigurationManager.AppSettings["uploadpath"] + prefix + sFileName); ; if (this.rb_Text.Checked) { try { MyGDI.CreatePlainText(sSrcFilePath, sDstFilePath,this.tb_Text.Text,tb_Color.Text,tb_Size.Text,this.tb_Font.Text); this.lb_ImageList.Items.Add(prefix + sFileName); this.lb_ImageList.SelectedIndex = -1; this.lb_ImageList.Items.FindByValue(prefix + sFileName).Selected = true; this.img_Src.ImageUrl = ConfigurationManager.AppSettings["uploadpath"] + prefix+ sFileName; } catch (Exception ex) { Response.Write(ex.Message); } } else if (this.rb_Watermark.Checked) { try { MyGDI.CreateWatermark(sSrcFilePath, sDstFilePath, this.tb_Text1.Text, this.tb_Color1.Text, this.tb_Size1.Text, this.tb_Font1.Text, this.tb_Text2.Text, this.tb_Color2.Text, this.tb_Size2.Text, this.tb_Font2.Text, this.tb_bgcolor.Text, this.tb_transparence.Text); this.lb_ImageList.Items.Add(prefix + sFileName); this.lb_ImageList.SelectedIndex = -1; this.lb_ImageList.Items.FindByValue(prefix + sFileName).Selected = true; this.img_Src.ImageUrl = ConfigurationManager.AppSettings["uploadpath"] + prefix + sFileName; } catch (Exception ex) { Response.Write(ex.Message); } } else if (this.rb_Corner.Checked) { try { MyGDI.CreateRoundedCorner(sSrcFilePath, sDstFilePath, this.ddl_CornerLocation.SelectedValue); this.lb_ImageList.Items.Add(prefix + sFileName); this.lb_ImageList.SelectedIndex = -1; this.lb_ImageList.Items.FindByValue(prefix + sFileName).Selected = true; this.img_Src.ImageUrl = ConfigurationManager.AppSettings["uploadpath"] + prefix + sFileName; } catch(Exception ex) { Response.Write(ex.Message); } } } protected void btn_Delete1_Click(object sender, EventArgs e) { string sFileFullPath = Server.MapPath(ConfigurationManager.AppSettings["uploadpath"] + this.lb_ImageList.SelectedValue); FileInfo fi = new FileInfo(sFileFullPath); fi.Delete(); this.lb_ImageList.Items.Remove(this.lb_ImageList.SelectedItem); } } public class MyGDI { public static void CreateWatermark(string sSrcFilePath, string sDstFilePath, string sText1, string sColor1, string sSize1, string sFont1, string sText2, string sColor2, string sSize2, string sFont2, string sBgColor, string sTransparence) { System.Drawing.Image image = System.Drawing.Image.FromFile(sSrcFilePath); Graphics g = Graphics.FromImage(image); g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //文字抗锯齿 g.DrawImage(image, 0, 0, image.Width, image.Height); Font f1 = new Font(sFont1, float.Parse(sSize1)); Font f2 = new Font(sFont2, float.Parse(sSize2)); Brush brushfortext1 = new SolidBrush(ColorTranslator.FromHtml(sColor1)); Brush brushfortext2 = new SolidBrush(ColorTranslator.FromHtml(sColor2)); Brush brushforbg = new SolidBrush(Color.FromArgb(Convert.ToInt16(255 * float.Parse(sTransparence)), ColorTranslator.FromHtml(sBgColor))); g.RotateTransform(-20); Rectangle rect = new Rectangle(-image.Width/2-50, image.Height - 50, image.Width * 2, 40); g.DrawRectangle(new Pen(brushforbg), rect); g.FillRectangle(brushforbg, rect); Rectangle rectfortext1 = new Rectangle(-image.Width/2 + image.Width / 5, image.Height - 45, image.Width * 2, 60); for (int i = 0; i < 10; i++) g.DrawString(sText1, f1, brushfortext1, rectfortext1); Rectangle rectfortext2 = new Rectangle(-image.Width / 2 + image.Width / 5, image.Height -25, image.Width * 2, 60); for (int i = 0; i < 10; i++) g.DrawString(sText2, f2, brushfortext2, rectfortext2); image.Save(sDstFilePath, ImageFormat.Jpeg); image.Dispose(); } public static void CreateRoundedCorner(string sSrcFilePath, string sDstFilePath, string sCornerLocation) { System.Drawing.Image image = System.Drawing.Image.FromFile(sSrcFilePath); Graphics g = Graphics.FromImage(image); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); GraphicsPath rectPath = CreateRoundRectanglePath(rect, image.Width / 10, sCornerLocation); //构建圆角外部路径 Brush b = new SolidBrush(Color.White);//圆角背景白色 g.DrawPath(new Pen(b), rectPath); g.FillPath(b, rectPath); g.Dispose(); image.Save(sDstFilePath, ImageFormat.Jpeg); image.Dispose(); } public static void CreatePlainText(string sSrcFilePath, string sDstFilePath,string sText, string sColor, string sSize, string sFont) { System.Drawing.Image image = System.Drawing.Image.FromFile(sSrcFilePath); Graphics g = Graphics.FromImage(image); g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(image, 0, 0, image.Width, image.Height); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //文字抗锯齿 Font f = new Font(sFont,float.Parse(sSize)); Brush b = new SolidBrush(ColorTranslator.FromHtml(sColor)); Rectangle rect = new Rectangle(10, 5, image.Width, image.Height); //适当空开一段距离 for (int i = 0; i < 30; i++) //加强亮度 g.DrawString(sText, f, b, rect); image.Save(sDstFilePath, ImageFormat.Jpeg); image.Dispose(); } private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, string sPosition) { GraphicsPath rectPath = new GraphicsPath(); switch (sPosition) { case "TopLeft": { rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90); rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius); break; } case "TopRight": { rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90); rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top); break; } case "BottomLeft": { rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90); rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom); break; } case "BottomRight": { rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90); rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom); break; } } return rectPath; } }
在 web.config 中配置一下:
<appSettings> <add key="uploadpath" value="upload/"/> </appSettings>
转载自:http://www.cnblogs.com/lovecherry/archive/2006/05/17/402541.html
谢谢浏览!