Asp.net Image Upload Method

Source: Internet
Author: User

Upload the relative path of the image to the corresponding field in the database tutorial. when reading the display, point the imageurl attribute of the control (assuming the image control is used) to the relative path.

Code:

Protected void button#click (object sender, eventargs e)

{

String name = fileupload1.filename; // get the file name

String type = name. substring (name. lastindexof (".") + 1 );

// Obtain the file type

String ipath = server. mappath ("image") + "" + name;

// Obtain the file path

String wpath = "image" + name;

// [Color = red] sets the file storage relative path

(The path starts with the name of the folder where the image is stored.) [/color]

 

String query1 = "insert into images values

('"+ Wpath + "')";

 

If (type = "jpg" | type = "gif" |

Type = "bmp" | type = "png ")

{

Fileupload1.saveas (ipath); // server storage path

Sqlhelper.exe cternonquery (query1 );

}

}

Show button events:

Code:

Protected void button2_click (object sender, eventargs e)

{

String query2 = "select * from images where

Image_id = "+ convert. toint32 (textbox1.text );

Sqldatareader sdr = sqlhelper. getreader (query2 );

String wpath2 = "";

While (sdr. read ())

{

Wpath2 = sdr [1]. tostring ();

// Obtain the relative path

}

Sdr. close ();

Image1.imageurl = wpath2;

// The image display path is the relative path.

Label1.text = wpath2; // displays the relative path.

}

Next let's look at another instance.

Background:

 

Protected void button#click (object sender, eventargs e)
{
Upimagefile (fileupload1 );
}

Protected void upimagefile (fileupload fileload)
{
If (fileload. hasfile)
{
String filetype = fileload. postedfile. contenttype;
If (filetype = "image/bmp" | filetype = "image/pjpeg" | filetype = "image/gif" | filetype = "image/png")
{
String loadpath = fileload. postedfile. filename; // The local path of the file to be uploaded
System. drawing. image img = system. drawing. image. fromfile (loadpath );
If (img. height> 100 | img. width> 100)
{
Fileinfo info = new fileinfo (loadpath );
String fname = info. name; // obtain the original file name
String filename = datetime. now. tostring ("yymmddhhmmss") + fname; // Add the time to the file name
String imgpath = server. mappath ("/upfile/orimages/") + filename; // original file path
String thpath = server. mappath ("/upfile/thimages/") + filename; // thumbnail path
Fileload. saveas (imgpath); // Save the original image
Makethumnail (imgpath, thpath); // generate a thumbnail
}
Else
{
// The image size is too small
}
}
Else
{
// The file format is incorrect.
}

}
}

Protected void makethumnail (string orpath, string thpath)
{
System. drawing. image img = system. drawing. image. fromfile (orpath );
Int width = 100; // set the width of the thumbnail to 100.
Int height = img. height * width/img. width; // proportional reduction of the thumbnail height
System. drawing. image bitmap = new system. drawing. bitmap (width, height); // create a vacant Graph
System. drawing. graphics g = system. drawing. graphics. fromimage (bitmap); // create a drawing board
G. interpolationmode = system. drawing. drawing2d. interpolationmode. high; // set the value to high quality interpolation.
G. smoothingmode = system. drawing. drawing2d. smoothingmode. highquality; // specify high quality and low speed rendering.
G. clear (color. transparent );
G. drawimage (img, new rectangle (0, 0, width, height ));
Try
{
Bitmap. save (thpath, system.drawing.imaging.imageformat.jpeg); // save the image in jpg format
}
Catch (system. exception e)
{
Throw e;
}
Finally
{
Img. dispose ();
Bitmap. dispose ();
G. dispose ();
}
}

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.