| Page Item | Value |
| Title | How to Determine the Dimensions (Width and Height) of Uploaded Images in ASP.NET |
| Description | Tutorial describes how to determine the dimensions (i.e. width and height) of uploaded images in ASP.NET |
| Keywords | asp.net, vb.net, c#, csharp, upload, uploading, image, images, dimensions, size, sizing, width, height, length, tall |
| Robots Meta Tag | |
| Page Content | HOME | ABOUT ME | BIOTECHNOLOGY | ARTICLES | TOOLS | GALLERY | CONTACT Search: Go DEVELOPER TOOLS ASP Doc Tool ASP.NET Doc Tool SQL Doc Tool Index Server Companion The Website Utility TECHNICAL ARTICLES ASP ASP.NET JavaScript Transact SQL PHOTO GALLERIES Canon EOS 300D Samples Red Arrows 2004 Living Coasts Web Page Backgrounds More Galleries... NEW STUFF SQL Color Coder Canon EOS 300D Samples The Website Utility Search Engine Optimisation Build an ASP Search Engine My Tropical Fishtank Savings Other New Stuff... POPULAR STUFF Regular Expressions ASP Documentation Tool Index Server ASP JavaScript Ad Rotator LINKS Business Website ASPAlliance Articles
Home ASP.NET Articles Determining the Dimensions of Uploaded Images in ASP.NET This article how to describes how to determine the width and height of an image after it has been uploaded to a web server using the file upload facilities within the .NET Framework of ASP.NET. Determining image dimensions can be useful in a number of situations. One of the most useful is to ensure that users of a web application such as a content management system do not upload massive 4000 x 3000 pixel images for display on their pages! If large images are uploaded, then the user could be informed that they should resize the image. Alternatively using some of the other image handling facilities in the .NET Framework it would be possible to build in some sort of automatic image resizing functionality. Building the Upload Form The upload form is a standard ASP.NET form - the HTML is shown below. Note that for a file uploading form, the Form tag must have the encType= multipart/form-data attribute. There should also be an Input web control with its Type attribute set to file . Finally the form will also need a submit button, which in this example has the ID of ButtonUpload . form id= UploadImage method= post encType= multipart/form-data runat= server p Choose an image file from your machine: br INPUT id= UploadedPicture type= file name= UploadedPicture runat= server br asp:button id= ButtonUpload runat= server Text= Upload This Image /asp:button /p /form Creating the Upload Event Handler The click event of the ButtonUpload button will handle the uploading of the image and the subsequent operations to determine the size of the uploaded picture. The code for this is show in both C# and VB.NET . C# Code for Image Uploading The code for the event handler is shown below. The UploadedPicture.PostedFile object can be used to interrogated to find out about the uploaded image. It contains a number of properties. For example, the string UploadedPicture.PostedFile.FileName contains the filename of the uploaded file. The UploadedPicture.PostedFile.ContentType property is also worth interrogating as it contains information about the type of file uploaded. For security and data integrity reasons it is a good idea to only specifically allow the uploading of specific file types. Uploaded JPEGs will have the ContentType property of image/gif , JPEGs will either have image/jpeg or image/pjpeg . To determine the dimensions of the uploaded image, a System.Drawing.Image object is created from the uploaded image's input stream, i.e. UploadedPicture.PostedFile.InputStream. The image's properties can then be used to determine its dimensions - PhysicalDimension.Width and PhysicalDimension.Height in the case of image width and height. Note that determining the dimensions of an image works for most JPEGs and GIFs, but there is no guarantee that it will work with other image formats. As always, production code should have plenty of exception handling added! private void ButtonUpload_Click(object sender, System.EventArgs e) { //Determine type and filename of uploaded image string UploadedImageType = UploadedPicture.PostedFile.ContentType.ToString().ToLower(); string UploadedImageFileName = UploadedPicture.PostedFile.FileName; //Create an image object from the uploaded file System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(UploadedPicture.PostedFile.InputStream); //Determine width and height of uploaded image float UploadedImageWidth = UploadedImage.PhysicalDimension.Width; float UploadedImageHeight = UploadedImage.PhysicalDimension.Height; //Check that image does not exceed maximum dimension settings if (UploadedImageWidth 600 || UploadedImageHeight 400) { Response.Write( This image is too big - please resize it! ); } } VB.NET Code for Image Uploading Private Sub ButtonUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Determine type and filename of uploaded image Dim UploadedImageType As String = UploadedPicture.PostedFile.ContentType.ToString().ToLower() Dim UploadedImageFileName As String = UploadedPicture.PostedFile.FileName 'Create an image object from the uploaded file Dim UploadedImage As System.Drawing.Image = System.Drawing.Image.FromStream(UploadedPicture.PostedFile.InputStream) 'Determine width and height of uploaded image Dim UploadedImageWidth As single = UploadedImage.PhysicalDimension.Width Dim UploadedImageHeight As single = UploadedImage.PhysicalDimension.Height 'Check that image does not exceed maximum dimension settings If UploadedImageWidth 600 Or UploadedImageHeight 400 Then Response.Write( This image is too big - please resize it! ) End If End Sub Useful Development Tools ASP Documentation Tool Automatically creates developer documentation for ASP 2.0 and 3.0 web applications written in VBScript and JScript. Documentation for Microsoft Access, SQL Server 7/2000 databases and Visual Basic 6.0 components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats. View Sample Output (HTML Help format). View Sample Output (HTML Format). Download Trial Version (5.2Mb ZIP file). ASP.NET Documentation Tool Automatically creates developer documentation for ASP.NET web applications written in C# or VB.NET. Documentation for SQL Server 7/2000 databases and C#/VB.NET components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats. View Sample Output (HTML Help format). View Sample Output (HTML Format). Download Trial Version (2.9Mb ZIP file). SQL Documentation Tool The SQL Documentation Tool creates technical documentation for Microsoft SQL Server 7.0 and 2000 databases. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced. The SQL Documentation Tool documents SQL Server Tables, Views, Stored Procedures, Triggers and Table Relationships. View Sample Output (HTML Help format). View Sample Output (HTML Format). Download Trial Version (10.3Mb ZIP file). Index Server Companion The Index Server Companion is a Windows application that extends the functionality of Microsoft Index Server so that it is able to index content from remote websites and also from ODBC databases. As such it can be used as a low cost alternative to Site Server 3.0 Search. View Product Documentation (119K ZIP file). Try Sample Search Facility . Download Trial Version (1.7Mb ZIP file). The Website Utility The Website Utility examines websites for errors and areas that need to be optimised for search engines by using a built in web crawling engine. Errors checked for include broken or moved hyperlinks, missing page titles and missing meta tags. It also generates HTML for use in creating website site maps (table of contents pages - like this one ), and is able to create both client-side JavaScript Search Engines and server-side ASP Search Engines for a website. View Sample Output (HTML Help format). View Sample Output (HTML Format). Download Trial Version (3Mb ZIP file). Site Map All content is © 1995 - 2006 Brett Burridge |
| Image Alt Tags | Brettb.Com Microsoft Certified Professional View Sample Output (HTML Help format) View Sample Output (HTML Format) Download Trial Version View Sample Output (HTML Help format) View Sample Output (HTML Format) Download Trial Version View Sample Output (HTML Help format) View Sample Output (HTML Format) Download Trial Version View Product Documentation Try Sample Search Facility Download Trial Version View Sample Output (HTML Help format) View Sample Output (HTML Format) Download Trial Version ASP Documentation Tool - Free Trial Available! 1000MB and 40GB for $7.95 a month! |
| Internal Links | http://www.brettb.com/redirector.asp (12 links in this page) [ Robot View of this URL ] http://www.brettb.com/Default.asp (3 links in this page) [ Robot View of this URL ] http://www.brettb.com/ASP.NETArticles.asp (2 links in this page) [ Robot View of this URL ] http://www.brettb.com/DeveloperTools.asp (2 links in this page) [ Robot View of this URL ] http://www.brettb.com/TheWebsiteUtility.asp (2 links in this page) [ Robot View of this URL ] http://www.brettb.com/ASPNETUploadImageSize.asp (2 links in this page) [ Robot View of this URL ] http://www.brettb.com/ASPDocumentationTool.asp (2 links in this page) [ Robot View of this URL ] http://www.brettb.com/JavaScriptArticles.asp [ Robot View of this URL ] http://www.brettb.com/Website_Search_Engine_Optimisation.asp [ Robot View of this URL ] http://www.brettb.com/SearchResults.asp [ Robot View of this URL ] http://www.brettb.com/js_banner_ad_rotator.asp [ Robot View of this URL ] http://www.brettb.com/web.asp [ Robot View of this URL ] http://www.brettb.com/CanonEOS300D_Gallery1.asp [ Robot View of this URL ] http://www.brettb.com/BuildingAnASPSearchEngine.asp [ Robot View of this URL ] http://www.brettb.com/backgrounds.asp [ Robot View of this URL ] http://www.brettb.com/VBScriptRegularExpressions.asp [ Robot View of this URL ] http://www.brettb.com/toc.asp [ Robot View of this URL ] http://www.brettb.com/Investments_ISAs.asp [ Robot View of this URL ] http://www.brettb.com/TransactSQLColorCoder.asp [ Robot View of this URL ] http://www.brettb.com/MyTropicalFishtank.asp [ Robot View of this URL ] http://www.brettb.com/CanonEOS300D_Gallery3.asp [ Robot View of this URL ] http://www.brettb.com/ASPWatchArticles.asp [ Robot View of this URL ] http://www.brettb.com/Red_Arrows_2004.asp [ Robot View of this URL ] http://www.brettb.com/Living_Coasts_Photos.asp [ Robot View of this URL ] http://www.brettb.com/SQL_Help.asp [ Robot View of this URL ] http://www.brettb.com/contact.asp [ Robot View of this URL ] http://www.brettb.com/Biotechnology.asp [ Robot View of this URL ] http://www.brettb.com/Gallery.asp [ Robot View of this URL ] http://www.brettb.com/ASPNetDocumentationTool.asp [ Robot View of this URL ] http://www.brettb.com/gallery.asp [ Robot View of this URL ] http://www.brettb.com/SearchingIndexServerWithASP.asp [ Robot View of this URL ] http://www.brettb.com/what's_new.asp [ Robot View of this URL ] http://www.brettb.com/IndexServerCompanion.asp [ Robot View of this URL ] http://www.brettb.com/technicalwriting.asp [ Robot View of this URL ] |
Report generated by The Website Utility 2.8