4

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.

Community
  • 1
  • 1
Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62
  • 1
    Windows.Storage.FileAccessMode.Read can't you change that to ReadWrite it looks like you are trying to save the new file which is an exact copy of the original file.. I am taking a stab in the dark await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); – MethodMan Dec 29 '12 at 15:09
  • @DJKRAZE Thanks, I haven't noticed that, but this doesn't help. – Mateusz Rogulski Dec 29 '12 at 15:13
  • have you Detached the Stream InMemoryRandomAccessStream for example after the await enc.FlushAsync(); add this code `ras.DetachStream();` http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.inmemoryrandomaccessstream - this link can also give you an idea http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/cf8c17dc-68d4-4777-951f-bb7f0665bd06/ – MethodMan Dec 29 '12 at 15:21
  • But `InMemoryRandomAccessStream` hasn't `DetachStream()` method. As I see on MSDN this method is on `DataWriter` class. http://msdn.microsoft.com/pl-pl/library/windows/apps/windows.storage.streams.inmemoryrandomaccessstream So I shoudl use it? – Mateusz Rogulski Dec 29 '12 at 15:27
  • Mateusz Yak shu Mash.. moya Mosh Skont Polska.. look at this it appears to be similar to what you are doing http://stackoverflow.com/questions/12868303/how-to-resize-bitmap-image-to-be-200-kb-and-meet-tile-restrictions-winrt – MethodMan Dec 29 '12 at 15:30
  • yes use the DataWriter.. – MethodMan Dec 29 '12 at 15:39
  • can you show the full Method where you have the above code.. I would like to see how the full Event or Method that this resides in, looks like. – MethodMan Dec 29 '12 at 15:44
  • This is bigger part of my code. The rest which I cut out are not important. Also can you suggest me how should I use `DataWriter` in this case? – Mateusz Rogulski Dec 29 '12 at 15:58
  • you could do something like this `DataWriter writer = new DataWriter(fileStream.GetOutputStreamAt(0));` – MethodMan Dec 29 '12 at 16:01
  • Is there a way that you can Debug that code and after the first await enc.FlushAsync(); when stepping thru the foreach loop, can you check to see if the file actually changed.. are all this image heights and widths the same .. can you change the height and width of one of the files in the debugger using the quick watch.. set it to some odd size like 150 x 350 and see if that changes.. – MethodMan Dec 29 '12 at 16:09
  • One other this this code here `item.CopyAsync` is there a `item.CopyToAsync` – MethodMan Dec 29 '12 at 16:17
  • Code from link and solution with `DataWriter` doesn't work. It produce error file. And file save after `CopyAsync()` method but after `FlushAsync()` doesn't make anything. – Mateusz Rogulski Dec 29 '12 at 16:29
  • what error.. There is something that must be missing.. when you run the original code.. are you sure that it was getting any actual data in the stream...? I can't test on my end because I don't have Windows8 – MethodMan Dec 29 '12 at 16:37
  • look at the portion where the OP talks about writing to a file the same theory should apply when writing / saving to the Image http://stackoverflow.com/questions/10836367/download-an-image-to-local-storage-in-metro-style-apps – MethodMan Dec 29 '12 at 16:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21870/discussion-between-dj-kraze-and-mateusz-rogulski) – MethodMan Dec 29 '12 at 16:47

1 Answers1

1

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();
Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62
MethodMan
  • 18,625
  • 6
  • 34
  • 52