系统相关
首页 > 系统相关> > c# – 如何在Windows应用商店应用中编辑和保存照片?

c# – 如何在Windows应用商店应用中编辑和保存照片?

作者:互联网

我制作了一些编辑照片的应用程序并将其保存在其他位置.所以我找到一个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目录中保存的照片相同.所以我不知道如何解决它.

任何帮助,将不胜感激.

解决方法:

Mateusz在你的foreach循环工作中会有类似的东西我不确定

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

fileStream.Dispose();
ras.Dispose();

标签:c,windows-store-apps,windows-8
来源: https://codeday.me/bug/20190709/1411907.html