我之前使用了http://www.codeproject.com/Tips/552141/Csharp-Image-resize-convert-and-save中的代码来以编程方式调整图像大小。然而,该项目使用的
我尝试使用
有人成功调整(缩小)Windows 10中的图像大小吗?我的应用程序将处理来自多个来源、不同格式/大小的图像,并且我正在尝试调整实际图像的大小以节省空间,而不是让应用程序将其大小调整以适合显示它的
编辑: 我修改了上述链接中的代码,并实现了一个适合我的特定需求的hack。以下是代码:
System.Drawing
库在Windows 10应用程序中不可用。我尝试使用
Windows.UI.Xaml.Media.Imaging
中的BitmapImage
类,但似乎不能提供System.Drawing
中的功能。有人成功调整(缩小)Windows 10中的图像大小吗?我的应用程序将处理来自多个来源、不同格式/大小的图像,并且我正在尝试调整实际图像的大小以节省空间,而不是让应用程序将其大小调整以适合显示它的
Image
。编辑: 我修改了上述链接中的代码,并实现了一个适合我的特定需求的hack。以下是代码:
public static BitmapImage ResizedImage(BitmapImage sourceImage, int maxWidth, int maxHeight)
{
var origHeight = sourceImage.PixelHeight;
var origWidth = sourceImage.PixelWidth;
var ratioX = maxWidth/(float) origWidth;
var ratioY = maxHeight/(float) origHeight;
var ratio = Math.Min(ratioX, ratioY);
var newHeight = (int) (origHeight * ratio);
var newWidth = (int) (origWidth * ratio);
sourceImage.DecodePixelWidth = newWidth;
sourceImage.DecodePixelHeight = newHeight;
return sourceImage;
}
这种方法似乎可行,但是理想情况下,我希望创建一个新的/拷贝的BitmapImage
进行修改并返回。
BitmapImage
的副本而不是修改原始数据。 - dub stylee