我如何在c#中调整图像大小?

Ani*_*pta 4 c# asp.net-mvc

我有一张图片

image = Image.FromStream(file.InputStream);
Run Code Online (Sandbox Code Playgroud)

我如何使用该属性System.Drawing.Size来调整它们或此属性用于何处?

我可以直接调整图像大小而不将其更改为位图或不丢失任何质量.我不希望公司只是调整它们的大小.

我怎么能在C#中做到这一点?

tho*_*vdb 6

这是我在当前项目中使用的函数:

    /// <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;
    }
Run Code Online (Sandbox Code Playgroud)