how to reduce the size of an image in

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 2.0?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You will need to test the ContentLength of the uploaded file, and reject anything larger than you want.  This page has some good info and code samples on uploading using ASP.Net:


This is in VB, but it can be converted to C# pretty easily i'm sure:

Public Shared Function ImageResize(ByVal image As System.Drawing.Image, _
    ByVal height As Int32, ByVal width As Int32) As System.Drawing.Image
        Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(width, height, image.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
            'More Info
            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(image, 0, 0, bitmap.Width, bitmap.Height)
        Return bitmap
    End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You can use GDI to render out a smaller version of the image, and then save it to disk.  The following code renders it out as output (so you can point an image tag to a page) but it wouldn't be hard to have it save the image).

        System.Web.HttpContext context = System.Web.HttpContext.Current;
        context.Response.ContentType = "image/png";

        Int32 nWidth = Convert.ToInt32(Request.QueryString["width"].ToString());
        Int32 nHeight = Convert.ToInt32(Request.QueryString["height"].ToString());
        String strImage = Request.QueryString["image"];
        System.Drawing.Image newImage = new Bitmap(nWidth, nHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(newImage);

        System.Drawing.Image oldImage = new Bitmap(Server.MapPath(strImage));

        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

        Rectangle destRect = new Rectangle(0, 0, nWidth, nHeight);
        Rectangle srcRect = new Rectangle(0, 0, oldImage.Width, oldImage.Height);

        g.DrawImage(oldImage, destRect, srcRect, GraphicsUnit.Pixel);

        if (Request.QueryString["overlay"] != null)
            oldImage = new Bitmap(Server.MapPath(Request.QueryString["overlay"].ToString()));
            srcRect = new Rectangle(0, 0, oldImage.Width, oldImage.Height);
            g.DrawImage(oldImage, destRect, srcRect, GraphicsUnit.Pixel);
        MemoryStream imageStream = new MemoryStream();

        newImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);


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.

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 
        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); 
    return bitmap; 
///To call this function 
System.Drawing.Image image = ImageResize(bigImage, smallHeight, smallWidth); 

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.