Easily implement thumbnails in ASP. NET

Source: Internet
Author: User
Previously, third-party components must be used to implement thumbnails on the page. Now, with. net, you can easily implement thumbnails. The following is an example of how to implement a thumbnail.

View examples
Http://dotnet.aspx.cc/Exam/Thumbnail.aspx

CodeThumbnail. aspx

Copy Save
 <% @ Page Language =  "VB"   Autoeventwireup =  "False"   Codebehind =  "Thumbnail. aspx. VB"  Inherits =  "Aspxweb. thumbnail"   %>  <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">  < Html  >  <  Head  >  <  Title  > Easily implement thumbnails in ASP. NET </  Title  >  <  Meta   Content =  "Microsoft Visual Studio. net7.0"   Name =  "Generator"  >  <  Meta   Content =  "Visual Basic 7.0"   Name =  "Code_language"  > <  Meta   Content =  "JavaScript"   Name =  "Vs_defaultclientscript"  >  <  Meta   Content =  Http://schemas.microsoft.com/intellisense/ie5"   Name =  "Vs_targetschema"  >  </  Head  >  <  Body   Ms_positioning =  "Gridlayout"  >  <  ASP: Label  Id =  "Label1"   Runat =  "Server"  >  </  ASP: Label  >  <  Form   Id =  "Form1"   Method =  "Post"   Runat =  "Server"   Enctype =  "Multipart/form-Data"  >  <  Input   Type =  "File"   Name =  "File"   Width = "600"  >  <  BR  >  <  BR  >  <  ASP: button   Id =  "Button1"   Runat =  "Server"  >  </  ASP: button  >  </  Form  >  </  Body  >  </  Html  > 

Code: thumbnail. aspx. VB

Copy Save
 Imports System Imports System. Web Imports System. Drawing Imports System. Io Imports System. Drawing. Imaging Public   Class Thumbnail Inherits System. Web. UI. Page Protected   Withevents Label1 As System. Web. UI. webcontrols. Label Protected   Withevents Button1 As System. Web. UI. webcontrols. Button # Region"Web Form Designer generated code"  'This call is required by the web form designer. <System. Diagnostics. debuggerstepthrough ()> Private   Sub Initializecomponent () End   Sub  Private   Sub Page_init ( Byval Sender As System. Object , Byval E As System. eventargs) Handles   Mybase . Init 'Codegen: This method call is required by the web Form Designer      'Do not modify it using the code editor. Initializecomponent () End   Sub  # End Region  Private   Sub Page_load ( Byval Sender As System. Object , Byval E As System. eventargs) Handles   Mybase . Loadlabel1.text = "<H3> easily implement thumbnails in ASP. NET  Button1.text = "Upload and display thumbnails" End   Sub  Private   Sub Button#click ( Byval Sender As   Object , Byval E As System. eventargs) Handles Button1.click Dim Myfilecoll As Httpfilecollection = httpcontext. Current. Request. Files Dim Mypostedfile As Httppostedfile = myfilecoll. Item (0) If Lcase (mypostedfile. contenttype. tostring (). indexof ( "Image" ) <0Then Response. Write ( "Invalid image format. " ) Exit   Sub  End   If Getthumbnail (mypostedfile. filename, 100,100, mypostedfile. contenttype. tostring (),_ False , Mypostedfile. inputstream) End   Sub  Private   Function Getimagetype ( Byval Strcontenttype) As System. Drawing. imaging. imageformat Select   Case (Strcontenttype. tostring (). tolower ()) Case   "Image/pjpeg" Getimagetype = system. Drawing. imaging. imageformat. JPEG Case   "Image/GIF" Getimagetype = system. Drawing. imaging. imageformat. gif Case   "Image/BMP" Getimagetype = system. Drawing. imaging. imageformat. BMP Case   "Image/tiff" Getimagetype = system. Drawing. imaging. imageformat. Tiff Case   "Image/X-icon" Getimagetype = system. Drawing. imaging. imageformat. icon Case   "Image/X-PNG" Getimagetype = system. Drawing. imaging. imageformat. PNG Case   "Image/X-EMF" Getimagetype = system. Drawing. imaging. imageformat. EMF Case  "Image/X-EXIF" Getimagetype = system. Drawing. imaging. imageformat. EXIF Case   "Image/X-WMF" Getimagetype = system. Drawing. imaging. imageformat. WMF Case   Else Getimagetype = system. Drawing. imaging. imageformat. memorybmp End   Select  End   Function  Private   Sub Getthumbnail ( Byval Strfilename, Byval Iwidth, Byval Iheight, Byval Strcontenttype ,_ Byval Blngetfromfile,Byval Imgstream) Dim Oimg As Image If Blngetfromfile Then Oimg = oimg. fromfile (strfilename) Else Oimg = oimg. fromstream (imgstream) End   If Oimg = oimg. getthumbnailimage (iwidth, iheight, Nothing ,( New Intptr (). Zero) Dim Strguid As   String = ( New GUID (). newguid (). tostring (). toupper () Dim Strfileext As  String = Strfilename. substring (strfilename. lastindexof ( "." )) 'Save to local      'Oimg. Save (server. mappath ("Images") + "" + strguid + strfileext, getimagetype (strcontenttype ))      'Directly output the URL File      'Response. Redirect ("images/" + strguid + strfileext)      'The following is displayed on the screen Response. contenttype = strcontenttype Dim Memstream As   New Memorystream () 'Note: If you use oimg. Save (response. outputstream, getimagetype (strcontenttype ))      'Errors may occur for different formats, such as PNG. Oimg. Save (memstream, getimagetype (strcontenttype) memstream. writeto (response. outputstream) End  Sub  End   Class 

C #CopySave

 Using System; Using System. collections; Using System. componentmodel; 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. imaging; Namespace Emeng. Exam { /// <Summary>      /// Summary of thumbnail.      /// </Summary>      Public   Class Thumbnail: system. Web. UI. Page { Protected System. Web. UI. webcontrols. Label label1; Protected System. Web. UI. webcontrols. Button button1; Private   Void Page_load ( Object Sender, system. eventargs e ){ // Place user code here to initialize the page Label1.text = "<H3> easily implement thumbnails in ASP. NET  ; Button1.text = "Upload and display thumbnails" ;} # Code generated by region web Form Designer          Override   Protected   Void Oninit (eventargs e ){ //              // Codegen: This call is required by the ASP. NET web form designer.              // Initializecomponent (); Base . Oninit (E );} /// <Summary>          /// The designer supports the required methods-do not use the code editor to modify          /// Content of this method.          /// </Summary>          Private   Void Initializecomponent (){ This . Button1.click + = New System. eventhandler (This . Button#click ); This . Load + = New System. eventhandler ( This . Page_load );} # Endregion  Private   Void Button#click ( Object Sender, system. eventargs e) {httpfilecollection myfilecoll = httpcontext. Current. Request. Files; httppostedfile mypostedfile = myfilecoll [0]; If (Mypostedfile. contenttype. tostring (). tolower (). indexof ( "Image" ) <0) {response. Write ( "Invalid image 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 (Strcontenttype. tostring (). tolower () = "Image/GIF" ){ Return System. Drawing. imaging. imageformat. gif ;} Else   If (Strcontenttype. tostring (). tolower () = "Image/BMP" ){ Return System. Drawing. imaging. imageformat. BMP ;} Else   If (Strcontenttype. tostring (). tolower () = "Image/tiff" ){ Return System. Drawing. imaging. imageformat. Tiff ;} Else   If (Strcontenttype. tostring (). tolower () = "Image/X-icon" ){ Return System. Drawing. imaging. imageformat. Icon ;} Else   If (Strcontenttype. tostring (). tolower () = "Image/X-PNG" ){ Return System. Drawing. imaging. 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   If (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 (); oimg. Save (memstream, getimagetype (strcontenttype); memstream. writeto (response. outputstream );}}}
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.