如何在上传的图片上加上版权文字

很多时候需要在用户上传的图片上加上版权或者一些其他的附加文字信息,如何实现这样的功能,下面帖个简单实现的例子,起到抛砖引玉的作用。

1@ Page Language="c#" Debug="true" Trace="true"
1@ Import Namespace="System.IO" 
1@ Import Namespace="System.Drawing" 
1@ Import Namespace="System.Drawing.Imaging" 
 1<html>
 2<script runat="server">   
 3void UploadBtn_Click(Object sender, EventArgs e) {   
 4String filename;   
 5String filename1;   
 6String[] filename2;   
 7int q;   
 8filename=UploadFile.PostedFile.FileName ;   
 9filename2=filename.Split(new Char[] {'\'});   
10q=filename2.GetUpperBound(0);   
11filename1=filename2[q];   
12dis.Text="上传文件名:"+filename1+"<br>";   
13UploadFile.PostedFile.SaveAs(Server.MapPath(filename1));   
14ImageEditor.Visible = true;   
15dis.Text+="文件大小:"+UploadFile.PostedFile.ContentLength+"字节数";   
16Image1.Src=filename1;   
17}   
18void UpdateBtn_Click(Object sender, EventArgs e) {   
19String filename1;   
20filename1=Image1.Src;   
21System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filename1));   
22System.Drawing.Image newimage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRGB);   
23Graphics g = Graphics.FromImage(newimage);   
24g.DrawImage(image,0,0,image.Width,image.Height);   
25Font f = new Font(FontType.SelectedItem.Text, Int32.Parse(FontSize.SelectedItem.Text));   
26Brush b = new SolidBrush(Color.Red);   
27g.DrawString(Caption.Text, f, b, 10, 140);   
28g.Dispose();   
29System.Drawing.Image thumbImage = newimage.GetThumbnailImage(Int32.Parse(Width.Text),Int32.Parse   
30(Height.Text),null,0);   
31image.Dispose();   
32thumbImage.Save(Server.MapPath(filename1), ImageFormat.JPEG);   
33Image1.Src=filename1;   
34Caption.Text="";   
35  
36} 
37
38</script>
39<body>
40<asp:label id="dis" runat="server/">
41<form enctype="multipart/form-data" runat="server">   
42选择上传文件: <input id="UploadFile" runat="server" type="file"/>
43<asp:button onclick="UploadBtn_Click" runat="server/" text="Upload Me!">
44<hr/>
45<asp:panel id="ImageEditor" runat="server" visible="false">
46<img id="Image1" runat="server" src=""/>   
47图像宽度: <asp:textbox id="Width" runat="server/">   
48图像高度: <asp:textbox id="Height" runat="server/"> <br/>   
49文本标题: <asp:textbox id="Caption" runat="server/">   
50标题字号: <asp:dropdownlist id="FontSize" runat="server">
51<asp:listitem>14</asp:listitem>
52<asp:listitem>18</asp:listitem>
53<asp:listitem>26</asp:listitem>
54<asp:listitem>36</asp:listitem>
55<asp:listitem>48</asp:listitem>
56<asp:listitem>62</asp:listitem>
57</asp:dropdownlist>   
58标题字体: <asp:dropdownlist id="FontType" runat="server">
59<asp:listitem>黑体</asp:listitem>
60<asp:listitem>仿宋</asp:listitem>
61<asp:listitem>隶书</asp:listitem>
62<asp:listitem>楷书</asp:listitem>
63<asp:listitem>彩云</asp:listitem>
64<asp:listitem>新魏</asp:listitem>
65</asp:dropdownlist>
66<asp:button onclick="UpdateBtn_Click" runat="server/" text="Update Image">
67</asp:button></asp:textbox></asp:textbox></asp:textbox></asp:panel>
68</asp:button></form>
69</asp:label></body>
70</html>

很多时候需要在用户上传的图片上加上版权或者一些其他的附加文字信息,如何实现这样的功能,下面帖个简单实现的例子,起到抛砖引玉的作用。

1@ Page Language="c#" Debug="true" Trace="true"
1@ Import Namespace="System.IO" 
1@ Import Namespace="System.Drawing" 
1@ Import Namespace="System.Drawing.Imaging" 
 1<html>
 2<script runat="server">   
 3void UploadBtn_Click(Object sender, EventArgs e) {   
 4String filename;   
 5String filename1;   
 6String[] filename2;   
 7int q;   
 8filename=UploadFile.PostedFile.FileName ;   
 9filename2=filename.Split(new Char[] {'\'});   
10q=filename2.GetUpperBound(0);   
11filename1=filename2[q];   
12dis.Text="上传文件名:"+filename1+"<br>";   
13UploadFile.PostedFile.SaveAs(Server.MapPath(filename1));   
14ImageEditor.Visible = true;   
15dis.Text+="文件大小:"+UploadFile.PostedFile.ContentLength+"字节数";   
16Image1.Src=filename1;   
17}   
18void UpdateBtn_Click(Object sender, EventArgs e) {   
19String filename1;   
20filename1=Image1.Src;   
21System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filename1));   
22System.Drawing.Image newimage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRGB);   
23Graphics g = Graphics.FromImage(newimage);   
24g.DrawImage(image,0,0,image.Width,image.Height);   
25Font f = new Font(FontType.SelectedItem.Text, Int32.Parse(FontSize.SelectedItem.Text));   
26Brush b = new SolidBrush(Color.Red);   
27g.DrawString(Caption.Text, f, b, 10, 140);   
28g.Dispose();   
29System.Drawing.Image thumbImage = newimage.GetThumbnailImage(Int32.Parse(Width.Text),Int32.Parse   
30(Height.Text),null,0);   
31image.Dispose();   
32thumbImage.Save(Server.MapPath(filename1), ImageFormat.JPEG);   
33Image1.Src=filename1;   
34Caption.Text=""; 
35
36} 
37
38</script>
39<body>
40<asp:label id="dis" runat="server/">
41<form enctype="multipart/form-data" runat="server">   
42选择上传文件: <input id="UploadFile" runat="server" type="file"/>
43<asp:button onclick="UploadBtn_Click" runat="server/" text="Upload Me!">
44<hr/>
45<asp:panel id="ImageEditor" runat="server" visible="false">
46<img id="Image1" runat="server" src=""/>   
47图像宽度: <asp:textbox id="Width" runat="server/">   
48图像高度: <asp:textbox id="Height" runat="server/"> <br/>   
49文本标题: <asp:textbox id="Caption" runat="server/">   
50标题字号: <asp:dropdownlist id="FontSize" runat="server">
51<asp:listitem>14</asp:listitem>
52<asp:listitem>18</asp:listitem>
53<asp:listitem>26</asp:listitem>
54<asp:listitem>36</asp:listitem>
55<asp:listitem>48</asp:listitem>
56<asp:listitem>62</asp:listitem>
57</asp:dropdownlist>   
58标题字体: <asp:dropdownlist id="FontType" runat="server">
59<asp:listitem>黑体</asp:listitem>
60<asp:listitem>仿宋</asp:listitem>
61<asp:listitem>隶书</asp:listitem>
62<asp:listitem>楷书</asp:listitem>
63<asp:listitem>彩云</asp:listitem>
64<asp:listitem>新魏</asp:listitem>
65</asp:dropdownlist>
66<asp:button onclick="UpdateBtn_Click" runat="server/" text="Update Image">
67</asp:button></asp:textbox></asp:textbox></asp:textbox></asp:panel>
68</asp:button></form>
69</asp:label></body>
70</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus