Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2007-08-17
5
Medium Priority
?
4,383 Views
Last Modified: 2013-11-05
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
Comment
Question by:apnamian
5 Comments
 
LVL 9

Assisted Solution

by:valkyrie_nc
valkyrie_nc earned 80 total points
ID: 19716701
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
 
LVL 28

Accepted Solution

by:
strickdd earned 1120 total points
ID: 19716832
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
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 1120 total points
ID: 19716834
0
 

Assisted Solution

by:jirofeingold
jirofeingold earned 800 total points
ID: 19720355
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
 

Expert Comment

by:virafh
ID: 24838732
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question