. NET upload image plus text and watermark image source code

Source: Internet
Author: User

Copy the following code and create an UPLOAD folder and an LZ. jpg image in the same directory.


. Aspx File

<% @ Page language = "c #" Codebehind = "35cnnet. aspx. cs" AutoEventWireup = "false" Inherits = "uploadfiles.35cnnet" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> MikeCat_WaterMark </title>
<Meta name = "GENERATOR" Content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "CODE_LANGUAGE" Content = "C #">
<Meta name = "vs_defaultClientScript" content = "JavaScript">
<Meta name = "vs_targetSchema" content = "http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" method = "post" runat = "server">
<INPUT id = "File1" style = "Z-INDEX: 101; LEFT: 144px; WIDTH: 400px; POSITION: absolute; TOP: 88px;

HEIGHT: 22px"
Type = "file" size = "47" name = "File1" runat = "server">
<Asp: Button id = "Button1" style = "Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 136px"

Runat = "server"
Text = "upload and add a Text watermark"> </asp: Button>
<Asp: Button id = "Button2" style = "Z-INDEX: 103; LEFT: 352px; POSITION: absolute; TOP: 136px"

Runat = "server"
Text = "upload and add an image watermark"> </asp: Button>
<Asp: RequiredFieldValidator id = "RequiredFieldValidator1" style = "Z-INDEX: 104; LEFT: 552px; POSITION:

Absolute; TOP: 88px"
Runat = "server" ErrorMessage = "*" ControlToValidate = "File1"> </asp: RequiredFieldValidator>
<DIV style = "Z-INDEX: 105; LEFT: 16px; WIDTH: 100%; POSITION: absolute; TOP: 168px; HEIGHT: 100px"
Ms_positioning = "GridLayout">
<Asp: Image id = "Image1" style = "Z-INDEX: 107; LEFT: 56px; POSITION: absolute; TOP: 16px"

Runat = "server"
ImageAlign = "Middle"> </asp: Image> </DIV>
<Asp: Label id = "Label1" style = "Z-INDEX: 107; LEFT: 176px; POSITION: absolute; TOP: 40px"

Runat = "server"> Design-upload an image and add a watermark </asp: Label>
</Form>
</Body>
</HTML>

. Cs File

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using System. IO;

Namespace uploadfiles
{

Public class 35 cnnet: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. Button Button1;
Protected System. Web. UI. HtmlControls. HtmlInputFile File1;
Protected System. Web. UI. WebControls. Image Image1;
Protected System. Web. UI. WebControls. RequiredFieldValidator RequiredFieldValidator1;
Protected System. Web. UI. WebControls. Label Label1;
Protected System. Web. UI. WebControls. Button Button2;

Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
If (! Page. IsPostBack)
{
Image1.ImageUrl = "aa.gif ";
}
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Button1.Click + = new System. EventHandler (this. button#click );
This. Button2.Click + = new System. EventHandler (this. Button2_Click );
This. Load + = new System. EventHandler (this. Page_Load );

}
# Endregion

Private void button#click (object sender, System. EventArgs e)
{
If (File1.PostedFile. FileName. Trim ()! = "")
{
// Upload a file
String extension = Path. GetExtension (File1.PostedFile. FileName). ToLower ();
String fileName = DateTime. Now. ToString ("yyyyMMddhhmmss ");
String path = Server. MapPath (".") + "/upload/" + fileName + extension;
File1.PostedFile. SaveAs (path );

// Add a text watermark. Note that the code here cannot coexist with the image watermark code below.
System. Drawing. Image image = System. Drawing. Image. FromFile (path );
Graphics g = Graphics. FromImage (image );
G. DrawImage (image, 0, 0, image. Width, image. Height );
Font f = new Font ("Verdana", 16 );
Brush B = new SolidBrush (Color. Blue );
String addText = "Hundred Percent design ";
G. DrawString (addText, f, B, 10, 10 );
G. Dispose ();

// Save the watermark image and delete the original image
String newPath = Server. MapPath (".") + "/upload/" + fileName + "_ new" + extension;
Image. Save (newPath );
Image. Dispose ();
If (File. Exists (path ))
{
File. Delete (path );
}

Image1.ImageUrl = newPath;
// Response. Redirect (newPath );
}

}

Private void Button2_Click (object sender, System. EventArgs e)
{
// Upload a file
String extension = Path. GetExtension (File1.PostedFile. FileName). ToUpper ();
String fileName = DateTime. Now. ToString ("yyyyMMddhhmmss ");
String path = Server. MapPath (".") + "/upload/" + fileName + extension;
File1.PostedFile. SaveAs (path );

// Add an image watermark
System. Drawing. Image image = System. Drawing. Image. FromFile (path );
System. Drawing. Image copyImage = System. Drawing. Image. FromFile (Server. MapPath (".") + "/lz.jpg ");
Graphics g = Graphics. FromImage (image );
G. DrawImage (copyImage, new Rectangle (image. Width-copyImage.Width, image. Height-copyImage.Height,

CopyImage. Width, copyImage. Height), 0, 0, copyImage. Width, copyImage. Height, GraphicsUnit. Pixel );
G. Dispose ();

// Save the watermark image and delete the original image
String newPath = Server. MapPath (".") + "/upload/" + fileName + "_ new" + extension;
Image. Save (newPath );
Image. Dispose ();
If (File. Exists (path ))
{
File. Delete (path );
}

Image1.ImageUrl = newPath;
}
}
}

 

From: http://www.studycs.com/html/523.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.