Upload image files in asp.net to implement anti-counterfeit image watermarks and write them into the database

Source: Internet
Author: User

Copy codeThe Code is as follows:
// 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. etettings ["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. ExecuteNonQuery ();
MyConnection. Close ();
Response. Write ("added successfully! ");
}
Catch (System. Exception SqlEx)
{
Response. Write ("failed to add! "+ SqlEx. ToString ());
}
}

Related Article

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.