Asp.net (C #) upload an image to generate a thumbnail

Source: Internet
Author: User

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"]);
}

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.