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#?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With