有没有人知道在.net 2.0 Compact框架上调整图像大小的方法?
我希望能够从手机内存中获取图像,用手机上的摄像头拍摄,调整大小,然后上传到网络服务中,这样我就不需要将调整大小的图像存储在磁盘上了。
发布于 2009-05-19 13:21:40
这是我使用的C#代码,用于调整.NET CF2.0应用程序中图像的大小:
public static Image ResizePicture( Image image, Size maxSize )
{
if( image == null )
throw new ArgumentNullException( "image", "Null passed to ResizePictureToMaximum" );
if( ( image.Width > maxSize.Width ) || ( image.Height > maxSize.Height ) )
{
Image resizedImage = new Bitmap( maxSize.Width, maxSize.Height );
using( Graphics graphics = Graphics.FromImage( resizedImage ) )
{
graphics.Clear( Color.White );
float widthRatio = maxSize.Width / image.Width;
float heightRatio = maxSize.Height / image.Height;
int width = maxSize.Width;
int height = maxSize.Height;
if( widthRatio > heightRatio )
{
width = ( int )Math.Ceiling( maxSize.Width * heightRatio );
}
else if( heightRatio > widthRatio )
{
height = ( int )Math.Ceiling( maxSize.Height * widthRatio );
}
graphics.DrawImage(
image,
new Rectangle( 0, 0, width, height ),
new Rectangle( 0, 0, image.Width, image.Height ),
GraphicsUnit.Pixel );
}
return resizedImage;
}
return image;
}发布于 2009-03-23 07:29:44
Graphics.FromImage应该适用于jpg图像,但是您可能会遇到内存问题。
看看这个论坛帖子,了解一些想法。
发布于 2011-02-25 09:09:56
中缺少强制转换的c#代码
float widthRatio = (float) maxSize.Width / image.Width;
float heightRatio = (float) maxSize.Height / image.Height;并以图像为中心:
graphics.DrawImage(image, new Rectangle((maxSize.Width - width) / 2, (maxSize.Height -height ) / 2, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);https://stackoverflow.com/questions/669137
复制相似问题