如何在Windows 10 UWP中复制和调整图像大小 [英] How to Copy and Resize Image in Windows 10 UWP

查看:210
本文介绍了如何在Windows 10 UWP中复制和调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了

解决方案


我可能想要返回原始 BitmapImage 的副本,而不是修改原始文件。


没有好方法直接复制 BitmapImage ,但我们可以多次重复使用 StorageFile



如果您只想选择一张图片,并在同时显示原始图片的重新调整图片时,您可以传递 StorageFile 作为参数如下:

  public static async Tas K<&的BitmapImage GT; ResizedImage(StorageFile ImageFile,int maxWidth,int maxHeight)
{
IRandomAccessStream inputstream = await ImageFile.OpenReadAsync();
BitmapImage sourceImage = new BitmapImage();
sourceImage.SetSource(inputstream);
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;

返回sourceImage;
}

在这种情况下,您只需要调用此任务并显示重新调整大小像这样的图像:

  smallImage.Source = await ResizedImage(file,250,250); 

如果你想保留 BitmapImage 参数由于某些原因(如sourceImage可能是修改后的位图但未直接从文件加载),并且您想要将此新图片重新调整为另一个,您需要先将重新调整大小的图片保存为文件,然后打开此文件并重新调整大小。


I have used the code from http://www.codeproject.com/Tips/552141/Csharp-Image-resize-convert-and-save to resize images programmatically. However, that project uses the System.Drawing libraries, which are not available to Windows 10 applications.

I have tried using the BitmapImage class from Windows.UI.Xaml.Media.Imaging instead, but it does not seem to offer the functionality that was found in System.Drawing..

Has anyone had any luck being able to resize (scale down) images in Windows 10? My application will be processing images from multiple sources, in different formats/sizes, and I am trying to resize the actual images to save space, rather than just letting the application size it to fit the Image in which it is being displayed.

EDIT

I have modified the code from the above mentioned link, and have a hack which works for my specific need. Here it is:

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;
}

This way seems to work, but ideally rather than modifying the original BitmapImage, I would like to create a new/copy of it to modify and return instead.

Here is a shot of it in action:

解决方案

I may want to return a copy of the original BitmapImage rather than modifying the original.

There is no good method to directly copy a BitmapImage, but we can reuse StorageFile for several times.

If you just want to select a picture, show it and in the meanwhile show the re-sized picture of original one, you can pass the StorageFile as parameter like this:

public static async Task<BitmapImage> ResizedImage(StorageFile ImageFile, int maxWidth, int maxHeight)
{
    IRandomAccessStream inputstream = await ImageFile.OpenReadAsync();
    BitmapImage sourceImage = new BitmapImage();
    sourceImage.SetSource(inputstream);
    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;
}

In this scenario you just need to call this task and show the re-sized image like this:

smallImage.Source = await ResizedImage(file, 250, 250);

If you want to keep the BitmapImage parameter due to some reasons (like the sourceImage might be a modified bitmap but not directly loaded from file), and you want to re-size this new picture to another one, you will need to save the re-sized picture as a file at first, then open this file and re-size it again.

这篇关于如何在Windows 10 UWP中复制和调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆