I should have made some changes according to the online code.
# Region upload images to the database
Private void upimgbutton_click (Object sender, system. eventargs E)
{
String exname = upfile. value. substring (upfile. value. lastindexof (".") + 1). toupper (); // locate the image suffix
String imgname = datetime. Now. tostring ("yyyymmddhhmmssfff") + "." + exname;
If (upfile. postedfile. contentlength = 0)
{
Response. Write ("<SCRIPT> alert ('the image you uploaded cannot be blank! '); </SCRIPT> ");
}
Else
{
Try
{
Byte [] filebyte = new byte [upfile. postedfile. contentlength];
Stream objectstream = upfile. postedfile. inputstream;
Objectstream. beginread (filebyte, 0, upfile. postedfile. contentlength, null, null );
String imgtype = upfile. postedfile. contenttype;
Byte [] smallfilebyte = new byte [upfile. postedfile. contentlength];
Smallfilebyte = createthumnail (objectstream, 100,100 );
String constr = "uid = SA, Pwd = SA, Server = Local, database = mydb ";
Sqlconnection conn = new sqlconnection (constr );
Conn. open ();
Sqlcommand mycommand = new sqlcommand ();
Mycommand. Connection = conn;
Mycommand. commandtext = "insert into [upimage] (imagename, image, imgtype, smallimage) values (@ imgname, @ filebyte, @ imgtype, @ smallimage )";
Mycommand. Parameters. Add ("@ imgname", imgname );
Mycommand. Parameters. Add ("@ filebyte", filebyte );
Mycommand. Parameters. Add ("@ imgtype", imgtype );
Mycommand. Parameters. Add ("@ smallimage", smallfilebyte );
Mycommand. executenonquery ();
Response. Write ("<SCRIPT> alert ('the image is successfully saved to the database! '); </SCRIPT> ");
}
Catch (exception ex)
{
Response. Write (ex. Message );
}
}
}
# Endregion
# Region generating thumbnails
Private byte [] createthumnail (Stream imagestream, int twidth, int theight)
{
System. Drawing. Image G = system. Drawing. image. fromstream (imagestream );
Int [] thumbsize = new int [] {1, 1 };
Thumbsize = newthumbsize (G. Width, G. Height, twidth, theight );
Bitmap imgoutput = new Bitmap (G, thumbsize [0], thumbsize [0]);
Memorystream imgstream = new memorystream ();
System. Drawing. imaging. imageformat thisformat = G. rawformat;
Imgoutput. Save (imgstream, thisformat );
Byte [] imgbin = new byte [imgstream. Length];
Imgstream. Position = 0;
Int32 n = imgstream. Read (imgbin, 0, imgbin. Length );
G. Dispose ();
Imgoutput. Dispose ();
Return imgbin;
}
# Endregion
# Region adjust the thumbnail size based on the uploaded Image
Protected int [] newthumbsize (INT currentwidth, int currentheight, int newwidth, int newheight)
{
Int tempmultiplier;
If (currentheight> currentwidth)
{
Tempmultiplier = newheight/currentheight;
}
Else
{
Tempmultiplier = newwidth/currentwidth;
}
Int [] newsize = new int [] {(currentwidth * tempmultiplier), (currentheight * tempmultiplier )};
Return newsize;
}
# Endregion
//// Code of the Image Display page
Mycommand. commandtext = "select smallimage from [upimage]";
Sqldatareader DR = dB. mycommand. executereader ();
This. response. contenttype = "image/GIF ";
While (dr. Read ())
{
Response. binarywrite (byte []) Dr ["smallimage"]);
}