• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4425
  • Last Modified:

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?
0
apnamian
Asked:
apnamian
4 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now