Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 9 February 2015

Introduction
In this article I’ll explain how to upload image using fileupload control and resize it in ASP.Net(C# & VB.NET).
Description: While working on asp.net application it is sometimes required to resize the image as per application requirement. Suppose there is image upload functionality in your website where user can upload their pictures. In this case you must resize the image before storing so that web space requirement for storing can be reduced.


Source Code:
  • In the design page (.aspx) place a FileUpload Control and a Button control as:
<table>
        <tr>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                      <asp:Button ID="btnUpload" runat="server" Text="Submit"
            onclick="btnUpload_Click" /></td>
         </tr>          
  </table>


C#.NET Code to  upload and resize the images
  • In the code behind file (.aspx.cs) write the code as:
First include following namespaces:


using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Design;
using System.IO;
  • Then write the code as:
protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string img=string.Empty;
            Bitmap bmpImg=null;
            try
            {
                bmpImg = Resize_Image(FileUpload1.PostedFile.InputStream, 210, 130);
                img = Server.MapPath("images/") + Guid.NewGuid().ToString() + “.png";
                bmpImg.Save(img, ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                Response.Write("Error occured: " + ex.Message.ToString());
            }
            finally
            {
                img = string.Empty;
                bmpImg.Dispose();
            }      
          }
    }
  

private Bitmap Resize_Image(Stream streamImage, int maxWidth, int maxHeight)
    {
        Bitmap originalImage = new Bitmap(streamImage);
        int newWidth = originalImage.Width;
        int newHeight = originalImage.Height;
        double aspectRatio = Convert.ToDouble(originalImage.Width) /Convert.ToDouble(originalImage.Height);

        if (aspectRatio <= 1 && originalImage.Width > maxWidth)
        {
            newWidth = maxWidth;
            newHeight = Convert.ToInt32(Math.Round(newWidth / aspectRatio));
        }
        else if (aspectRatio > 1 && originalImage.Height > maxHeight)
        {
            newHeight = maxHeight;
            newWidth = Convert.ToInt32(Math.Round(newHeight * aspectRatio));
        }
        return new Bitmap(originalImage, newWidth, newHeight);
    }


VB.NET Code to resize the images


  • In the code behind file (.aspx.vb) write the code as:
First Import following namespaces:

Imports
 System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Drawing.Design
Imports System.IO
  • Then write the code as:
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlesbtnUpload.Click
        If FileUpload1.HasFile Then
            Dim img As String = String.Empty
            Dim bmpImg As Bitmap = Nothing
            Try
                bmpImg = Resize_Image(FileUpload1.PostedFile.InputStream, 210, 130)
                img = Server.MapPath("images/") + Guid.NewGuid().ToString() + ".png"
                bmpImg.Save(img, ImageFormat.Jpeg)
            Catch ex As Exception
                Response.Write("Error occured: " & ex.Message.ToString())
            Finally
                img = String.Empty
                bmpImg.Dispose()
            End Try
        End If
    End Sub

    Private Function Resize_Image(ByVal streamImage As Stream, ByVal maxWidth As Integer, ByValmaxHeight As Integer) As Bitmap
        Dim originalImage As New Bitmap(streamImage)
        Dim newWidth As Integer = originalImage.Width
        Dim newHeight As Integer = originalImage.Height
        Dim aspectRatio As Double = CDbl(originalImage.Width) / CDbl(originalImage.Height)

        If aspectRatio <= 1 AndAlso originalImage.Width > maxWidth Then
            newWidth = maxWidth
            newHeight = CInt(Math.Round(newWidth / aspectRatio))
        ElseIf aspectRatio > 1 AndAlso originalImage.Height > maxHeight Then
            newHeight = maxHeight
            newWidth = CInt(Math.Round(newHeight * aspectRatio))
        End If

        Return New Bitmap(originalImage, newWidth, newHeight)
    End Function


Note: Uploaded image will be stored in the “images” folder of the root directory in our case. 
Categories: , ,

0 comments:

Post a Comment