Upload pictures in ASP.NET and generate a thumbnail C # source Using system; using system.collections; using system.data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; using System. Drawing.Image;
Namespace Emeng.exam {///
private void Page_Load (object sender, System.EventArgs e) {// Put user code to initialize the page here Label1.Text = "
}
#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.oninit (e);
///
} #endregion
Private void button1_click (object sender, system.eventargs e) {httpfilecollection myfilecoll = httpContext.current.request.files; httppostedfile mypostedfile = myfilecoll [0];
IF (MyPostedFile.conteType.toString (). TOLOWER (). IndexOf ("Image") <0) {response.write ("Invalid graphics format."); return;} getthumbnail (MyPostedFile.FileName, 100, 100, MyPostedFile.ContentType.ToString (), false, MyPostedFile.InputStream);} private System.Drawing.Imaging.ImageFormat GetImageType (object strContentType) {if ((strContentType.ToString () ToLower ().) == "image / pjpeg" ) {Return System.drawing.Imaging.ImageFormat.jpeg;} else IF (). TOLOWER ()) == "image / gif") {return system.drawing.image.imageformat.gif;} else IF ((StrContentType.toString (). TOLOWER ()) == "image / bmp") {return system.drawing.image.imageformat.bmp;} else f ((StrContentType.toString (). TOLOWER ()) == "image / TIFF") {return system.drawing.imaging.imageformat.tiff;} else f ((StrContentType.tostring (). TOLOWER ()) == "image / x-icon") {Return System.drawing.Image Else IF ((strcontenttype.tostring (). TOLOWER ()) == "Image / X-PNG") {Return System.drawing.Image.ImageFormat.png; Else IF ((strContentType.toString (). TOLOWER ()) == "Image / X-EMF") {Return System.drawing.Imaging.ImageFormat.emf;} else IF ((strContentType.toString (). TOLOWER )) == "image / x-exif") {return system.drawing.Imaging.imageformat.exif;} else f ((StrContentType.toString (). TOLOWER ()) == "image / x-wmf") { return System.Drawing.Imaging.ImageFormat.Wmf;} else {return System.Drawing.Imaging.ImageFormat.MemoryBmp;}} private void getThumbNail (string strFileName, int iWidth, int iheight, string strContentType, bool blnGetFromFile, System.IO. Stream Imgstream) {system.drawing.image oimg;
if (blnGetFromFile) {oImg = System.Drawing.Image.FromFile (strFileName);} else {oImg = System.Drawing.Image.FromStream (ImgStream);} oImg = oImg.GetThumbnailImage (iWidth, iheight, null, IntPtr.Zero ); string strGuid = System.Guid.NewGuid () ToString () ToUpper ();.. string strFileExt = strFileName.Substring (strFileName.LastIndexOf ( ".")); Response.ContentType = strContentType; MemoryStream MemStream = new MemoryStream ( ); object-save (Memstream, GetImageType (strContentType); Memstream.writEto (Response.outputStream);
}}} Font> font> font> Features: 1. Upload the picture file (JPG GIF PNG), 2. Save to the specified path (set the path in Web.config, save the original format of the file), 3. And automatically generate a specified width (set width in web.config) 4. And specify the format (specify the format of the thumbnail in Web.config) 5. The same thumbnail with the original map (the height of the width and high calculation of the original) 6. You can determine if files 7 already exist. If it is not overwritten, an error 8 is given. If you select "Overwrite the original picture" Checkbox, you can override the original image. 9. You can set more than 1 File INPUT and the corresponding Checkbox 10 on WebForm as required. After completion of the file, the file name, size, byte, and 11 of the original map are displayed. The file name size of the thumbnail. 12. Thumbnail file name format: original image "_ thumb." Specify format, such as test.jpg_thumb.gif for easy management.
-------------------- Public void UploadFile (Object Sender, System.EventArgs E) {
string imgNameOnly, imgNameNoExt, imgExt; string imgThumbnail; int erroNumber = 0; System.Drawing.Image oriImg, newImg; string strFePicSavePath = ConfigurationSettings.AppSettings [ "FePicSavePath"] ToString ();. string strFePicThumbFormat = ConfigurationSettings.AppSettings [ "FePicThumbFormat" ] .Tostring (). TOLOWER (); int inTfethumbWidth = int32.Pars (ConfigurationSettings.AppSettings ["fepiceumbwidth"]; string fileext; StringBuilder Picinfo = new stringbuilder ();
IF (page.isvalid) {
For (int i = 0; i IMGNAMEOMEONLY = system.io.path.GetFileName (PostedFile.FileName); if (fileext == ".jpg" || fileext == ".gif" || fileext == ".png") { IF (STRFEPICSAVEPATH IMGNAMEONLY && (CheckBoxListreWrite.Items [i] .selected == false)) {Erronumber = erronumber 1; picinfo.append (" error: b> Document (" (i 1) ") " IMGNAMEONLY " already exists, please modify the file name ");}} else {Erronumber = erronumber 1; picinfo.append (" errors : B> file (" (i 1) ") " IMGNAMEONLY " extension " FileExt " Not licensed ");} } IF (Erronumber> 0) {picinfo.append (" all the operations are not completed, please modify the error, then perform font>");} else {for (int i = 0; i HttpPostedFile PostedFile = Request.Files [i]; imgNameOnly = System.IO.Path.GetFileName (PostedFile.FileName); imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension (PostedFile.FileName); imgExt = System.IO.Path.GetExtension ( . PostedFile.FileName) .ToString () ToLower (); oriImg = System.Drawing.Image.FromStream (PostedFile.InputStream); newImg = oriImg.GetThumbnailImage (intFeThumbWidth, intFeThumbWidth * oriImg.Height / oriImg.Width, null, new System .Intptr (0)); Switch (imgext) {// case ".jpeg": case ".jpg": Oriimg.save (StrfePicsavePath ImgnameOnly, System.drawing.Image.ImageFormat.jpeg); Break; Case. gif ": oriImg.Save (strFePicSavePath imgNameOnly, System.Drawing.Imaging.ImageFormat.Gif); break; case" .png ": oriImg.Save (strFePicSavePath imgNameOnly, System.Drawing.Imaging.ImageFormat.Png); break } //oriimg.save (ConfigurationSettings.appsettings["fepicsavepath "] IMGNAMENOEXT " .jpg ", system.drawing.Image.imageformat.jpeg); switch (strFePicThumbFormat) {// jpeg format can get the smallest file size, and the png is the largest size // case "jpeg": case "jpg": newImg.Save (strFePicSavePath imgNameOnly "_thumb.jpg", System .Drawing.Imaging.ImageFormat.Jpeg); imgThumbnail = imgNameOnly "_thumb.jpg"; break; case "gif": newImg.Save (strFePicSavePath imgNameOnly "_thumb.gif", System.Drawing.Imaging.ImageFormat.Gif ); imgThumbnail = imgNameOnly "_thumb.gif"; break; case "png": newImg.Save (strFePicSavePath imgNameOnly "_thumb.png", System.Drawing.Imaging.ImageFormat.Png); imgThumbnail = imgNameOnly "_thumb .png "; break; default: newImg.Save (strFePicSavePath imgNameOnly " _thumb.jpg ", System.Drawing.Imaging.ImageFormat.Jpeg); imgThumbnail = imgNameOnly " _thumb.jpg "; break;} // switch Picinfo.append (" file name: b>" ivnameonly "(" Oriimg.Width ")" PostedFile.ContentLength / 1024 "KB Oriimg.dispose (); newimg.dispose (); } // for picinfo.append (" all operations" "" " } // if erronumber = 0 } else {picinfo.append ( has an error, please check. Operation is not successful font> "); } LBLPICINFO.TEXT = Picinfo.toString (); } Font>
"); Picinfo.append (" thumbnail name: b> " ivthumbnail " (" newimg.width ") ");