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?
LVL 3
apnamianAsked:
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.

valkyrie_ncCommented:
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:  http://articles.techrepublic.com.com/5100-3513_11-6039171.html


hth

valkyrie_nc
0
strickddCommented:
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 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.")
        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)
        graphicsImage.Dispose()
        Return bitmap
    End Function
0

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
strickddCommented:
0
jirofeingoldCommented:
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);
        context.Response.BinaryWrite(imageStream.ToArray());
        context.Response.Flush();

        imageStream.Dispose();
        g.Dispose();
        newImage.Dispose();

0
virafhCommented:
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

0
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
ASP.NET

From novice to tech pro — start learning today.