dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersShine S
Dotnet_CHD
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET WebForms

How to resize uploaded image in ASP.NET?


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Diamond    Points: 60


In this article I am going to explain about how to resize image in ASP.NET. In many cases user upload large images in the website using this code we can resize that each image and save in the server folder



 


Description :


If user upload large size of images in the server it occupy more spaces in the server and difficult to view all images in the website. So we need to resize that image during upload and then save it.

Client side


I placed file upload control to get file from user

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Resize Image</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="600" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" height="40">
<b>Resize image</b>
</td>
</tr>
<tr>
<td height="40">
Select File to be uplaod
</td>
<td height="40">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" height="40" align="center">
<asp:Button ID="btnUpload" runat="server" Text="Resize and Upload"
onclick="btnUpload_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Server side


In the server side I get that image input stream and resize it like below code

using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//create instance for image class
System.Drawing.Image myimg = default(System.Drawing.Image);
//get uploaded image input stream
myimg = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
//resize it using thumbnailimage method
myimg = myimg.GetThumbnailImage(50, 100, null, IntPtr.Zero);
MemoryStream str = new MemoryStream();
//Save it in the server images folder
myimg.Save(Server.MapPath("~\\images\\" + FileUpload1.FileName), myimg.RawFormat);
Response.Write("Resized image is successfully uploaded in to server images folder");
}
}
}

Output :


image

Source code:


Client Side: ASP.NET
Code Behind: C#

Conclusion

I hope this code snippet is help you to know about resize image in the ASP.NET.

Attachments
  • Resize_Image_Source (43964-73537-Resize-Image-Source.rar)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to resize uploaded image in ASP.NET?"
    Guest Author: Parul     08 Aug 2012
    Its working very nice.


    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to integrate PayPal in ASP.NET?
    Previous Resource: How to UnZip files in ASP.NET?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Resize image size  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.