Link to home
Start Free TrialLog in
Avatar of apnamian
apnamian

asked on

how to reduce the size of an image in asp.net?

users want to upload their picture but i want to restrict them to upload an image of not more than 0.5MB , is there any method i can reduce the size of an image programmtically using asp.net 2.0?
SOLUTION
Avatar of valkyrie_nc
valkyrie_nc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of virafh
virafh

Dear Strickdd,

i converted your codes to c#,

can you explain it more how to display the image by using your given codes.

the function return type is bitmap, i am confuse how to use this.

thanx
public static System.Drawing.Image ImageResize(System.Drawing.Image image, Int32 height, Int32 width) 
{ 
    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, image.PixelFormat); 
    if (bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format1bppIndexed | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format4bppIndexed | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format8bppIndexed | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Undefined | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.DontCare | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format16bppArgb1555 | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format16bppGrayScale) { 
        //More Info http://msdn.microsoft.com/library/default.asp?_ 
        //url=/library/en-us/cpref/html/frlrfSystemDrawingGraphicsClassFromImageTopic.asp 
        throw new NotSupportedException("Pixel format of the image is not supported."); 
    } 
    System.Drawing.Graphics graphicsImage = System.Drawing.Graphics.FromImage(bitmap); 
    graphicsImage.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality; 
    graphicsImage.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
    graphicsImage.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height); 
    graphicsImage.Dispose(); 
    return bitmap; 
} 
///To call this function 
System.Drawing.Image image = ImageResize(bigImage, smallHeight, smallWidth); 

Open in new window