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 ());
}
}