简体   繁体   中英

How to edit and save photo in Windows Store App?

I make some application which edit photo and save it in other location. So I find a question which shows how to resize photos in Windows Store Apps. Then I implement it in my program:

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

Problem

Result of this code is the same photo saved in _targetFolder catalogue. So I have no idea how to fix it.

Any help would be appreciated.

Mateusz will something like this inside your foreach loop work I am not sure

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM