Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Image uploading with Resizing

I have an aspx page which will upload images to server harddisk from client pc

But now i need to change my program in such a way that it would allow me to resize the image while uploading.

Does anyone has any idea on this ? I couldnt not find such properties/methods with Input file server control

Any one there to guide me ?

like image 945
user29982 Avatar asked Oct 31 '08 18:10

user29982


People also ask

How to resize image in Asp net Core?

Resizing an Image can be done in a range of ways. The easiest method is to create a new Bitmap object from the in-memory image. When creating the Bitmap object, you assign the new dimension in the Size parameter. Finally, the resized Bitmap is written to a byte array.


2 Answers

Once the file has been saved to the server you can use code like this to resize. This code will take care of length/width ratio on the resize.

public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight) {      System.Drawing.Bitmap bmpOut = null;      try     {         Bitmap loBMP = new Bitmap(lcFilename);         ImageFormat loFormat = loBMP.RawFormat;          decimal lnRatio;         int lnNewWidth = 0;         int lnNewHeight = 0;          if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)             return loBMP;          if (loBMP.Width > loBMP.Height)         {             lnRatio = (decimal)lnWidth / loBMP.Width;             lnNewWidth = lnWidth;             decimal lnTemp = loBMP.Height * lnRatio;             lnNewHeight = (int)lnTemp;         }         else         {             lnRatio = (decimal)lnHeight / loBMP.Height;             lnNewHeight = lnHeight;             decimal lnTemp = loBMP.Width * lnRatio;             lnNewWidth = (int)lnTemp;         }           bmpOut = new Bitmap(lnNewWidth, lnNewHeight);         Graphics g = Graphics.FromImage(bmpOut);         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;         g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;         g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;         g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);         g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);          loBMP.Dispose();     }     catch     {         return null;     }     return bmpOut; } 
like image 137
JPrescottSanders Avatar answered Sep 18 '22 11:09

JPrescottSanders


You will not be able to resize "on the fly" since you will need to have the full image before you perform any image transformations. However, after the upload is complete and before you display any results to your user, you can use this basic image resizing method that I've used in a couple of my apps now:

   ''' <summary>    '''    Resize image with GDI+ so that image is nice and clear with required size.    ''' </summary>    ''' <param name="SourceImage">Image to resize</param>    ''' <param name="NewHeight">New height to resize to.</param>    ''' <param name="NewWidth">New width to resize to.</param>    ''' <returns>Image object resized to new dimensions.</returns>    ''' <remarks></remarks>    Public Shared Function ImageResize(ByVal SourceImage As Image, ByVal NewHeight As Int32, ByVal NewWidth As Int32) As Image        Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(NewWidth, NewHeight, SourceImage.PixelFormat)        If bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format1bppIndexed Or _           bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format4bppIndexed Or _           bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format8bppIndexed Or _           bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Undefined Or _           bitmap.PixelFormat = Drawing.Imaging.PixelFormat.DontCare Or _           bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppArgb1555 Or _           bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppGrayScale Then          Throw New NotSupportedException("Pixel format of the image is not supported.")       End If        Dim graphicsImage As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)        graphicsImage.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality       graphicsImage.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic       graphicsImage.DrawImage(SourceImage, 0, 0, bitmap.Width, bitmap.Height)       graphicsImage.Dispose()       Return bitmap     End Function 
like image 21
Dillie-O Avatar answered Sep 19 '22 11:09

Dillie-O