asp+文件上传增强实例

按实际上传时的文件名称保存(不在是test.jpg了)图片可以改大小,图上写字(可选字体,我设的全是中文,^_^。大小),没有解决字体颜色问题。

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(Height.Text),null,0);   
30image.Dispose();   
31thumbImage.Save(Server.MapPath(filename1), ImageFormat.JPEG);   
32Image1.Src=filename1;   
33Caption.Text="";   
34  
35}   
36  
37</script>
38<body>
39<asp:label id="dis" runat="server/">
40<form enctype="multipart/form-data" runat="server">
41<h3>   
42  
43Select File To Upload: <input id="UploadFile" runat="server" type="file"/>
44<asp:button onclick="UploadBtn_Click" runat="server/" text="Upload Me!">
45<hr/>
46<asp:panel id="ImageEditor" runat="server" visible="false">
47<img id="Image1" runat="server" src=""/>
48<h3>   
49Image Width: <asp:textbox id="Width" runat="server/">   
50  
51Image Height: <asp:textbox id="Height" runat="server/"> <br/>   
52  
53Text Caption: <asp:textbox id="Caption" runat="server/">   
54  
55Caption Size: <asp:dropdownlist id="FontSize" runat="server">
56<asp:listitem>14</asp:listitem>
57<asp:listitem>18</asp:listitem>
58<asp:listitem>26</asp:listitem>
59<asp:listitem>36</asp:listitem>
60<asp:listitem>48</asp:listitem>
61<asp:listitem>62</asp:listitem>
62</asp:dropdownlist>   
63Caption Font: <asp:dropdownlist id="FontType" runat="server">
64<asp:listitem>黑体</asp:listitem>
65<asp:listitem>仿宋</asp:listitem>
66<asp:listitem>隶书</asp:listitem>
67<asp:listitem>楷书</asp:listitem>
68<asp:listitem>方正姚体</asp:listitem>
69<asp:listitem>华文彩云</asp:listitem>
70</asp:dropdownlist>
71<asp:button onclick="UpdateBtn_Click" runat="server/" text="Update Image">
72</asp:button></asp:textbox></asp:textbox></asp:textbox></h3>
73</asp:panel>
74</asp:button></h3></form>
75</asp:label></body>
76</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus