如何编辑和保存照片在Windows商店应用程序

本文关键字:照片 Windows 应用程序 保存 何编辑 编辑 | 更新日期: 2023-09-27 17:50:03

我制作了一些编辑照片并保存在其他位置的应用程序。所以我找到了一个问题,展示了如何在Windows Store应用程序中调整照片的大小。然后我在我的程序中实现它:

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目录中的同一张照片。所以我不知道如何修复它。

如何编辑和保存照片在Windows商店应用程序

Mateusz会在你的foreach循环中工作,我不确定

ras.Seek(0);
fileStream.Seek(0);
fileStream.Size = 0;
await RandomAccessStream.CopyAsync(ras, fileStream);
fileStream.Dispose();
ras.Dispose();