Upload images with ASP. NET and generate thumbnails with copyright information

Source: Internet
Author: User
<% @ Page Language = "C #" ResponseEncoding = "gb2312" %>
<% @ Import Namespace = "System" %>
<% @ Import Namespace = "System. IO" %>
<% @ Import Namespace = "System. Drawing" %>
<% @ Import Namespace = "System. Drawing. Imaging" %>
<Script runat = "server">

Void Page_Load (Object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
ImgPreview. Visible = false;
}
}
Void GetThumbnailImage (int width, int height, string strInfo, int left, int right)
{
String file = "Uploads/" + uploadFile. PostedFile. FileName. Substring (uploadFile. PostedFile. FileName. LastIndexOf (''\'') + 1 );
String newfile = "Uploads/" + uploadFile. PostedFile. FileName. Substring (uploadFile. PostedFile. FileName. LastIndexOf (''\'') + 1) + ". jpg ";
String stradd = strinfo;
System. Drawing. Image oldimage = system. Drawing. image. fromfile (server. mappath (File ));
System. Drawing. Image thumbnailimage =
Oldimage. getthumbnailimage (width, height, new system. Drawing. image. getthumbnailimageabort (thumbnailcallback), intptr. Zero );
Response. Clear ();
Bitmap output = new Bitmap (thumbnailimage );
Graphics G = graphics. fromimage (output );
G. drawstring (stradd, new font ("Courier New", 14), new solidbrush (color. Red), left, right );
Output. Save (server. mappath (newfile), system. Drawing. imaging. imageformat. JPEG );
Response. contenttype = "image/GIF ";
Imgpreview. Visible = true;
Imgpreview. imageurl = newfile;
}
Bool thumbnailcallback ()
{
Return true;
}

Void Button_Click (object sender, EventArgs e)
{
Int width, height, left, right;
String strAddInfo = txtAddInfo. Text;
Width = Int32.Parse (txtWidth. Text );
Height = Int32.Parse (txtHeight. Text );
Left = Int32.Parse (txtLeft. Text );
Right = Int32.Parse (txtRight. Text );
If (! (UploadFile. PostedFile. ContentLength> 0 ))
{
LblErrInfo. Text = "no file selected ";
}
Else
{

String path = Server. mapPath (". /Uploads/"+ uploadFile. postedFile. fileName. substring (uploadFile. postedFile. fileName. lastIndexOf (''\'') + 1 ));
If (File. Exists (path ))
{
LblErrInfo. Text = "a file with the same name already exists ";
}
Else
{
UploadFile. PostedFile. SaveAs (path );
GetThumbnailImage (width, height, strAddInfo, left, right );
}
}
}
</Script>
<Html>
<Head>
</Head>
<Body>
<Form method = "post" enctype = "multipart/form-data" runat = "server">
<P>
<Input id = "uploadFile" type = "file" runat = "server"/>
<Asp: Label id = "lblErrInfo" runat = "server" forecolor = "Red"> </asp: Label>
</P>
<P>
Width: <asp: TextBox id = "txtWidth" runat = "server" Width = "40px"> 100 </asp: TextBox>
Height: <asp: TextBox id = "txtHeight" runat = "server" Width = "40px"> 150 </asp: TextBox>
 
</P>
<P>
Add information: <asp: TextBox id = "txtAddInfo" runat = "server"> AspxBoy. Com </asp: TextBox>
</P>
<P>
Information location: left: <asp: TextBox id = "txtLeft" runat = "server" Width = "40px"> 10 </asp: TextBox>
Right: <asp: TextBox id = "txtRight" runat = "server" Width = "40px"> 135 </asp: TextBox>
</P>
<P>
 
<Input id = "button" type = "button" value = "upload to generate a thumbnail" onServerClick = "Button_Click" runat = "server"/>
</P>
<P> <asp: Image id = "ImgPreview" runat = "server"> </asp: Image>
</P>
<! -- Insert content here -->
</Form>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.