Thumbnails in ASP.NET

xiaoxiao2021-03-06  41

Thumbnails in ASP.NET

Source: Unknown Author: Unknown Recommended rating: Visits: 19

Previously, the thumbnail on the page must be used to use a third party component. Now, with .NET, thumbnails can be easily implemented. Below is an example of achieving thumbnails.

View example code as follows: thumbnail.aspx <% @ page language = "vb" autoeventwireup = "false" codebehind = "thumbnail.aspx.vb" inherits = "aspxweb.thumbnail"%> Easily implement thumbnail in ASP.NET </ title> <meta content =" Microsoft Visual Studio.net 7.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"> <ask: label id = "label1" runat = "server"> </ ask: 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> after the code: Thumbnail.aspx.vb Imports System Imports System.Web Imports System.Drawing Imports System.IO Imports System.Drawing.Imaging Public Class thumbnail inherits system.Web.ui.page protected w ithEvents 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 ()></p> <p>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 (Byval e AS System.Object, byval e as system.eventargs) Handles mybase.load label1.text = "<h3> Easily implement thumbnails in ASP.NET < / h3> "Button1.Text =" upload and display thumbnail "End Sub Private Sub Button1_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" <0 Then Response.write ("invalid graphics format.</p> <p>") 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.Image.imageforma t.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") "/" </p> <p>strGuid strFileExt, GetImageType (strContentType)) 'direct output file url' Response.Redirect ( "images /" strGuid strFileExt) 'displayed on the screen less Response.ContentType = strContentType Dim MemStream As New MemoryStream ()' Note: This If you use Oimg.Save (response.outputstream, GetImageType (strContentType)) 'to different formats, such as PNG format. oImg.Save (MemStream, GetImageType (strContentType)) MemStream.WriteTo (Response.OutputStream) End Sub End Class C # 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> /// thumbnail's summary description. /// </ 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 page label1.text = "<h3> Easily implement thumbnail </ h3>"; button1.text = "in ASP.NET "} #Region Web Form Designer Generated Code Override Protected Void OnInit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} /// <summary> /// Designer Supports the required method - Do not use the code editor to modify the // / this method.</p> <p>/// </ summary> private void InitializeComponent () {this.Button1.Click = new System.EventHandler (this.Button1_Click); this.Load = new System.EventHandler (this.Page_Load);} #endregion private void Button1_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 graphics format.</p> <p>"); 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 f ((StrContentType.toString (). TOLOWER ()) == "image / gif ") {Return System.drawing.Imaging.ImageFormat.gif;} else f (). TOLOWER ()) ==" image / bmp ") {return system.drawing.image.imageformat.bmp;} ELSE IF ((StrContentType.toString (). TOLOWER ()) == "Image / Tiff") {Return System.drawing.Imaging.imageFormat.tiff;} else IF ((StrContentType.toString (). Tool! ()) = = "image / x-icon") {return system.drawing.imaging.imageformat.icon;} else f (). TOLOWER ()) == "image / x-png") {Return System. Drawing.Image.ImageFormat.png;} else IF ((strcontenttype.tostring (). TOLOWER ()) == "image / x-emf") {return system.drawing.Image .} Else IF ((strconteTtype.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 =</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-70434.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="70434" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.037</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'jrUZ77t_2F7uHohEoAOzNMM_2FZTzp3FmoS8EOzX_2FJOEu_2BYC47sFaWZYzyzsbJRJ6pRvwShGjkb8oxBp8bFH'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>