How I can resize the image in c#?
I have an image
image = Image.From开发者_开发技巧Stream(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;
}
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
精彩评论