Upload an image file instance in asp.net (add a watermark to the uploaded image)

Source: Internet
Author: User

This asp.net tutorial is an example of uploading an image file to asp.net (adding a watermark to the uploaded image). He first uploads the image to the server, adds an image watermark, and saves the image to the database.

This asp tutorial. the net tutorial is an example of uploading an image file (adding a watermark to the uploaded image) in the asp.net tutorial. He first uploads the image to the server, adds an image watermark, and then saves the image to the database.

// Namespace involved
Using system;
Using system. collections;
Using system. componentmodel;
Using system. data;
Using system. data. sqlclient;
Using system. drawing;
Using system. drawing. drawing2d;
Using system. drawing. imaging;
Using system. web;
Using system. configuration;

// Method
Public void adduser (string personname, string personemail, string personsex, string persondob, string personimage, string personimagetype)
{
String strimagetype = userimage. postedfile. contenttype;
Stream imagestream = userimage. postedfile. inputstream;

// Add a watermark --------------->
String wimagefile = server. mappath ("/bkwww/image/homesign.gif"); // watermark image to be added
Image simage = image. fromstream (imagestream); // create an image from the http input stream
Image wimage = image. fromfile (wimagefile );

// Drawing
Graphics g = graphics. fromimage (simage );
G. drawimage (wimage, new rectangle (0, 0, wimage. width, wimage. height), 0, 0, wimage. width, wimage. height, graphicsunit. pixel );

// Save and convert the image to byte []
Memorystream MS = new memorystream ();
Byte [] myimage = null;
Simage. save (MS, imageformat.gif );
Myimage = ms. getbuffer ();
// ------------------------>

 

// Write data to the database
String strconn = configurationsettings. apps tutorial ettings ["connectionstring"];
Sqlconnection myconnection = new sqlconnection (strconn );
Sqlcommand mycommand = new sqlcommand ("sp_person_isp", myconnection );
Mycommand. commandtype = commandtype. storedprocedure;

Mycommand. parameters. add ("@ personemail", sqldbtype. varchar, 255). value = personemail;
Mycommand. parameters. add ("@ personname", sqldbtype. varchar, 255). value = personname;
Mycommand. parameters. add ("@ personsex", sqldbtype. char, 1 );
If (sexmale. checked)
Mycommand. parameters ["@ personsex"]. value = "m ";
Else
Mycommand. parameters ["@ personsex"]. value = "f ";
Mycommand. parameters. add ("@ persondob", sqldbtype. datetime). value = persondob;
Mycommand. parameters. add ("@ personimage", sqldbtype. image). value = myimage;
Mycommand. parameters. add ("@ personimagetype", sqldbtype. varchar, 255). value = imagetype;

Try
{
Myconnection. open ();
Mycommand.exe cutenonquery ();
Myconnection. close ();
Response. write ("added successfully! ");
}
Catch (system. exception sqlex)
{
Response. write ("failed to add! "+ Sqlex. tostring ());
}
}

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.