我有一张图片
image = Image.FromStream(file.InputStream);
我如何使用属性System.Drawing.Size
来调整大小,或者这个属性用于哪里?
我可以直接调整图像大小而不将它们更改为位图或不损失任何质量吗?我不想裁剪,只想调整大小。
我该如何在C#中实现这个功能?
我有一张图片
image = Image.FromStream(file.InputStream);
我如何使用属性System.Drawing.Size
来调整大小,或者这个属性用于哪里?
我可以直接调整图像大小而不将它们更改为位图或不损失任何质量吗?我不想裁剪,只想调整大小。
我该如何在C#中实现这个功能?
/// <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;
}
var image_16 = new System.Drawing.Bitmap(image, new Size(16, 16));
参考 - http://msdn.microsoft.com/zh-cn/library/system.drawing.size.aspx
更先进的GDI+技术可在以下链接中找到: