Thứ Bảy, 18 tháng 8, 2012

How to Resize image while uploading in asp.net using c#

Resize image while uploading in asp.net using c#?

This post will show you how you can resize images in ASP.Net C# during uploading image file.
For we Dotnet Developer in most of our ASP.Net application we have uploaded image facility where will face the performance issue while loading uploaded images to display for the user. In my project their is a similar requiremnt that the user uploaded images will be resized internally through server side while uploading.
I have an .aspx page which will upload images to server harddisk from client pc. And i need to write a program in such a way that it would allow me to resize the image while uploading.

Here's the code:
Firstly, we are going to need the System.Drawing and System.Drawing.Drawing2D namespaces

Design Uploadlogo.aspx:
Upload Logo :


<asp:fileupload id="File1" runat="server">
</asp:fileupload>
 <asp:button id="Button1" onclick="Button1_Click" runat="server" text="Save">
 <asp:label id="lblmsg" runat="server" text=""></asp:label>
</asp:button>



Code Behind: UploadLogo.aspx.cs
  public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight)
{
var ratio = (double)maxHeight / image.Height;
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
using (var g = Graphics.FromImage(newImage))
{
g.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public partial class UploadLogo : System.Web.UI.Page
{
public Size OriginalImageSize { get; set; } //Store original image size.
public Size NewImageSize { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
lblmsg.Text="";
if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
{
Guid uid = Guid.NewGuid();
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string SaveLocation = Server.MapPath("LogoImages") + "\\" + uid+fn;
try
{
string fileExtention = File1.PostedFile.ContentType;
int fileLenght = File1.PostedFile.ContentLength;
if (fileExtention == "image/png" || fileExtention == "image/jpeg" || fileExtention == "image/x-png")
{
if (fileLenght <= 1048576)
{
System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(File1.PostedFile.InputStream);
System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 81);
objImage.Save(SaveLocation,ImageFormat.Png);
lblmsg.Text = "The file has been uploaded.";
lblmsg.Style.Add("Color", "Green");
}
else{
lblmsg.Text = "Image size cannot be more then 1 MB.";
lblmsg.Style.Add("Color", "Red");
}
}
else {
lblmsg.Text = "Invaild Format!";
lblmsg.Style.Add("Color", "Red");
}
}
catch (Exception ex)
{
lblmsg.Text= "Error: " + ex.Message;
lblmsg.Style.Add("Color", "Red");
}
}
}

}



Không có nhận xét nào:

Đăng nhận xét