How to use BitmapEncoder Class

How do you use the BitmapEncoder Class to resize an image.  For example; I want to take an image that is 96 x 96 and resize it to 48 x 48 or take an image that is 96 x 96 and reduce it by 20%

I want to use this class because it is ASyncable.
Ray TurnerSenior ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Here is an example:

https://social.msdn.microsoft.com/Forums/en-US/490b9c01-db4b-434f-8aff-d5c495e67e55/how-to-crop-an-image-using-bitmaptransform?forum=winappswithcsharp

 async private void BitmapTransformTest()
        {
            // hard coded image location
            string filePath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\fantasy-dragons-wallpaper.jpg";

            StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);
            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);

            // convert the entire bitmap to a 100px by 100px bitmap
            enc.BitmapTransform.ScaledHeight = 100;
            enc.BitmapTransform.ScaledWidth = 100;
            
            
            BitmapBounds bounds = new BitmapBounds();
            bounds.Height = 50;
            bounds.Width = 50;
            bounds.X = 50;
            bounds.Y = 50;
            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

        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray TurnerSenior ConsultantAuthor Commented:
Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.