4

I have an image

image = Image.FromStream(file.InputStream);

How I can use the property System.Drawing.Size for resize them or where this property used for?

Can I direct resize the image without change them to bitmap or without loss any quality. I not want corp just resize them.

How I can do this in C#?

DanielB
  • 19,910
  • 2
  • 44
  • 50
Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79

2 Answers2

6

This is a function I use in my current project:

    /// <summary>
    /// Resize the image.
    /// </summary>
    /// <param name="image">
    /// A System.IO.Stream object that points to an uploaded file.
    /// </param>
    /// <param name="width">
    /// The new width for the image.
    /// Height of the image is calculated based on the width parameter.
    /// </param>
    /// <returns>The resized image.</returns>
    public Image ResizeImage( Stream image, int width ) {
        try {
            using ( Image fromStream = Image.FromStream( image ) ) {
                // calculate height based on the width parameter
                int newHeight = ( int )(fromStream.Height / (( double )fromStream.Width / width));

                using ( Bitmap resizedImg = new Bitmap( fromStream, width, newHeight ) ) {
                    using ( MemoryStream stream = new MemoryStream() ) {
                        resizedImg.Save( stream, fromStream.RawFormat );
                        return Image.FromStream( stream );
                    }
                }
            }
        } catch ( Exception exp ) {
            // log error
        }

        return null;
    }
thomasvdb
  • 739
  • 1
  • 11
  • 32
3

You can use Bitmap class's constructor to help, and actually Bitmap is a subclass of Image.

var image_16 = new System.Drawing.Bitmap(image, new Size(16, 16));

reference at - http://msdn.microsoft.com/en-us/library/system.drawing.size.aspx

more advance by GDI+ technique able to find at

http://www.codeproject.com/KB/GDI-plus/imageresize.aspx

Jirapong
  • 24,074
  • 10
  • 54
  • 72