none
Change resolution of image in WIndows 8 Store App

    Question

  • Code below is to save the image:

    private async void saveImage(StorageFile destinationFile)
    {
                if (destinationFile != null)
                {
                    using (var readStream = await sourceFile.OpenReadAsync())
                    {
                        var decoder = await BitmapDecoder.CreateAsync(readStream);
    
                        using (InMemoryRandomAccessStream writeStream = new InMemoryRandomAccessStream())
                        {
                            BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(writeStream, decoder);
    
                            encoder.BitmapTransform.Bounds = new BitmapBounds()
                            {
                                X = (uint)Math.Round(selectedLeft, 0),
                                Y = (uint)Math.Round(selectedTop, 0),
                                Width = (uint)Math.Round(selectedWidth, 0),
                                Height = (uint)Math.Round(selectedHeight, 0),
                            };
    
                            await encoder.FlushAsync();
    
                            using (var stream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
                            {
                                await RandomAccessStream.CopyAndCloseAsync(writeStream.GetInputStreamAt(0), stream.GetOutputStreamAt(0));
                            }
                        }
                    }
    }
    The output image size is 320x480. How to I resize to 960x1240? Thanks

    Tuesday, October 08, 2013 10:00 AM

Answers

  • Hi Howard,

    I tried your code but I don't know what selectedLeft means, so I create a sample myself, it works fine. I think the problem of your code might be encoder parameter.

    I scaled a 220*220 jpg from my picture library to a 440*440 image, the code I paste below:

                var folder = KnownFolders.PicturesLibrary;
    
                StorageFile file = await folder.GetFileAsync("avatar.jpg");
                if (file == null)
                    return;
    
                // create a stream from the file and decode the image
                var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
    
    
                // create a new stream and encoder for the new image
                InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
                BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);
    
               
                enc.BitmapTransform.ScaledHeight = 440;
                enc.BitmapTransform.ScaledWidth = 440;
    
    
                BitmapBounds bounds = new BitmapBounds();
                bounds.Height = 0;
                bounds.Width = 0;
                bounds.X = 0;
                bounds.Y = 0;
                enc.BitmapTransform.Bounds = bounds;
    
                // write out to the stream
                try
                {
                    await enc.FlushAsync();
                }
                catch (Exception ex)
                {
                    string s = ex.ToString();
                }
    
                // render the stream to the screen
                BitmapImage bImg = new BitmapImage();
                bImg.SetSource(ras);
                img.Source = bImg; // image element in xaml

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, October 10, 2013 7:21 AM
    Moderator

All replies

  • Hi Howard,

    Are you trying to fine this BitmapTransform ? By using ScaledHeight and ScaledWidth you could resize your image from 320*480 to 960*1240. There is a thread which discussed the same thing, and from which you could get some hint: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/490b9c01-db4b-434f-8aff-d5c495e67e55/how-to-crop-an-image-using-bitmaptransform?forum=winappswithcsharp

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, October 09, 2013 8:48 AM
    Moderator
  • Hi, I have try edit the code as follow, but the saved image resolution also 320x480.

    using (InMemoryRandomAccessStream writeStream = new InMemoryRandomAccessStream())
    {
          BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(writeStream, decoder);
    
          encoder.BitmapTransform.ScaledHeight = 960;
          encoder.BitmapTransform.ScaledWidth = 1240;
    
          encoder.BitmapTransform.Bounds = new BitmapBounds()
          {
              X = (uint)Math.Round(selectedLeft, 0),
              Y = (uint)Math.Round(selectedTop, 0),
              Width = (uint)Math.Round(selectedWidth, 0),
              Height = (uint)Math.Round(selectedHeight, 0),
          };
    
          await encoder.FlushAsync();
    
          using (var stream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
          {
               await RandomAccessStream.CopyAndCloseAsync(writeStream.GetInputStreamAt(0), stream.GetOutputStreamAt(0));
          }
    }

    But before click save image, I have the following code:

    _rect.Rect = new Rect(screenWidth/2, screenVertical/2,480,320);
    photo.Clip = _rect;
    is it the issue is here? The code is for clip the image size by 320x480 before save.


    • Edited by howard hee Thursday, October 10, 2013 6:58 AM
    Thursday, October 10, 2013 6:47 AM
  • Hi Howard,

    I tried your code but I don't know what selectedLeft means, so I create a sample myself, it works fine. I think the problem of your code might be encoder parameter.

    I scaled a 220*220 jpg from my picture library to a 440*440 image, the code I paste below:

                var folder = KnownFolders.PicturesLibrary;
    
                StorageFile file = await folder.GetFileAsync("avatar.jpg");
                if (file == null)
                    return;
    
                // create a stream from the file and decode the image
                var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
    
    
                // create a new stream and encoder for the new image
                InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
                BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);
    
               
                enc.BitmapTransform.ScaledHeight = 440;
                enc.BitmapTransform.ScaledWidth = 440;
    
    
                BitmapBounds bounds = new BitmapBounds();
                bounds.Height = 0;
                bounds.Width = 0;
                bounds.X = 0;
                bounds.Y = 0;
                enc.BitmapTransform.Bounds = bounds;
    
                // write out to the stream
                try
                {
                    await enc.FlushAsync();
                }
                catch (Exception ex)
                {
                    string s = ex.ToString();
                }
    
                // render the stream to the screen
                BitmapImage bImg = new BitmapImage();
                bImg.SetSource(ras);
                img.Source = bImg; // image element in xaml

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, October 10, 2013 7:21 AM
    Moderator
  • Oh, that is just a rectangle corner point, the value can be as follow:

    selectedLeft = 100;

    selectedTop =100;

    selectedWidth=480;

    selectedHeight=340;

     
    Thursday, October 10, 2013 8:10 AM
  • Hi Howard,

    When I posting my code, I did not see the update from your previous post.

    OK, could you do a validation? After you scaled the image, render the image and display on the screen like I did, you will see if your scale is work or not, then you could find if the problem is on your Scale code or Clip code.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, October 10, 2013 8:32 AM
    Moderator