我制作了一些编辑照片的应用程序并将其保存在其他位置。所以我找到了question,其中显示了如何在Windows应用商店应用中调整照片大小。然后我在我的程序中实现它:
private async void ResizeButton_Click(object sender, RoutedEventArgs e)
{
uint width, height;
if (uint.TryParse(WidthTextBox.Text, out width) && uint.TryParse(HeightTextBox.Text, out height)
&& _folderWithPhoto != null && _targetFolder != null)
//_folderWithPhoto and _targetFolder are StorageFolder values get from FolderPicker
{
var files = await _folderWithPhoto.GetFilesAsync();
foreach (StorageFile item in files)
{
if (item.ContentType.Contains("image"))
{
StorageFile targetFile = await item.CopyAsync(_targetFolder, item.Name, NameCollisionOption.GenerateUniqueName);
var fileStream = await targetFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);
enc.BitmapTransform.ScaledHeight = height;
enc.BitmapTransform.ScaledWidth = width;
await enc.FlushAsync();
}
}
}
}
问题
此代码的结果与_targetFolder
目录中保存的照片相同。所以我不知道如何解决它。
任何帮助都将不胜感激。
答案 0 :(得分:1)
Mateusz在你的foreach循环工作中会有这样的东西我不确定
ras.Seek(0);
fileStream.Seek(0);
fileStream.Size = 0;
await RandomAccessStream.CopyAsync(ras, fileStream);
fileStream.Dispose();
ras.Dispose();