Thursday, 17 April 2014

Resize Image Using C#.Net


Resize Image Using C#.Net

In this Post I will show you how to resize a Uploaded Image and Save the same in a specific Directory Using C#.Net..

Design Part:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Resize Image</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Height"></asp:Label>
        <asp:TextBox ID="txtheight" runat="server"></asp:TextBox><br />
        <br />
        <asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Width"></asp:Label>
        <asp:TextBox ID="txtWidth" runat="server"></asp:TextBox><br />
        <br />
        <asp:Label ID="Label3" runat="server" Font-Bold="True" Text="Image Name"></asp:Label>
        <asp:TextBox ID="txtImage" runat="server"></asp:TextBox><br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" /><br />
        <br />
        <asp:Label ID="lblMsg" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></div>
    </form>
</body>
</html>

Code-Behind:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
//using ImageResizer;

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

    public static void SaveImage(int imgheight, int imgwidth, string imgfilename, string path, FileUpload upImage)
    {
        int width = imgwidth;
        int height = imgheight;
        string fileName = imgfilename;
        float redPercentage = 100;

        try
        {
            var bmpOrg = new Bitmap(upImage.PostedFile.InputStream);
            float wPercentage = (Convert.ToSingle(width) * 100) / Convert.ToSingle(bmpOrg.Width);
            float hPercentage = (Convert.ToSingle(height) * 100) / Convert.ToSingle(bmpOrg.Height);
            redPercentage = (wPercentage < hPercentage) ? wPercentage : hPercentage;
            redPercentage = (redPercentage > 100) ? 100 : redPercentage;
            bmpOrg.MakeTransparent();
            //to save image with new height and width without frame
            //var imgBanner = ImageResize.ScaleByPercent(bmpOrg, redPercentage);
            //to save image with new height and width with frame specified
            System.Drawing.Image imgBanner = ScaleByPercent(bmpOrg, redPercentage);
            System.Drawing.Bitmap bmpOrg1 = new System.Drawing.Bitmap(imgBanner);
            Graphics newGraphic = Graphics.FromImage(bmpOrg1);
            newGraphic.Clear(Color.Transparent);
            newGraphic.DrawImage(bmpOrg, 0, 0, bmpOrg1.Width, bmpOrg1.Height);
            bmpOrg1.MakeTransparent(bmpOrg1.GetPixel(0, 0));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            bmpOrg1.Save(path + fileName + ".jpeg");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public static System.Drawing.Image ScaleByPercent(System.Drawing.Image imgPhoto, float Percent)
    {
        float nPercent = ((float)Percent / 100);
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;
        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);
        Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
        grPhoto.Clear(Color.White);
        grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
        GraphicsUnit.Pixel);
        grPhoto.Dispose();
        return bmPhoto;

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Set the Path where output file should be saved
        string path=@"D:\Practice New\Transparent Image\Image\";
        //ImageResizer.Class1.SaveImage(Convert.ToInt32(txtheight.Text), Convert.ToInt32(txtWidth.Text), txtImage.Text, path, FileUpload1);
        SaveImage(Convert.ToInt32(txtheight.Text), Convert.ToInt32(txtWidth.Text), txtImage.Text, path, FileUpload1);
    }
}

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top