Skip to main content

Resizing an image within a Windows 8 app in C#

I'm creating an application where a user can add their own images. Accepting a user's pictures is handy and all but when modern digital cameras regularly take pictures that are greater than 4MB that's not something I want stored in memory.

Here I have image data within a stream and want to resize it to a given width and height - while maintaining aspect ratio. This accomplishes the task nicely...

  1.         /// <summary> 
  2.         /// Resizes image data within a stream to a given width and height.
  3.         /// </summary>
  4.         /// <returns>
  5.         /// Returns an image stream with the resized image data.
  6.         /// </returns>
  7.         public async Task<IRandomAccessStream> ResizeImage(IRandomAccessStream imageStream, uint width, uint height)
  8.         {
  9.             IRandomAccessStream resizedStream = imageStream;
  10.             var decoder = await BitmapDecoder.CreateAsync(imageStream);
  11.             if (decoder.OrientedPixelHeight > height || decoder.OrientedPixelWidth > width)
  12.             {
  13.                 resizedStream = new InMemoryRandomAccessStream();
  14.                 BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder);
  15.                 double widthRatio = (double)width / decoder.OrientedPixelWidth;
  16.                 double heightRatio = (double)height / decoder.OrientedPixelHeight;
  18.                 // Use whichever ratio had to be sized down the most to make sure the image fits within our constraints.
  19.                 double scaleRatio = Math.Min(widthRatio, heightRatio);
  20.                 uint aspectHeight = (uint)Math.Floor((double)decoder.OrientedPixelHeight * scaleRatio);
  21.                 uint aspectWidth = (uint)Math.Floor((double)decoder.OrientedPixelWidth * scaleRatio);
  23.                 encoder.BitmapTransform.ScaledHeight = aspectHeight;
  24.                 encoder.BitmapTransform.ScaledWidth = aspectWidth;
  26.                 // write out to the stream
  27.                 await encoder.FlushAsync();
  29.                 // Reset the stream location.
  30.                 resizedStream.Seek(0);
  31.             }
  33.             return resizedStream;
  34.         }


That's great

That's great and helpful

Works great

It worked great for my Windows Phone 8.1 app. Thanks!

Thanks for this.

Thanks for the info. Works like a charm on for my phone.Cheers! Ronald



C # helps the developers to

C # helps the developers to resize the images those needs to be decorated.There are many options for the improving the quality of the image.This will let the people know about the quality of the services. IP

All routers have a default username and password set in. These default usernames and passwords are same for all the routers. Router

Once on the watercolor, users

Once on the watercolor, users can use stroke and stylus to influence the images and add observations to the canvas. The submission is written in C# and is planned for windows 8 devices.

I am very glad to learn a lot

I am very glad to learn a lot from you this meaningful knowledge. From an article describing your unique way , we can see that you are an approachable , humorous person. Not only that, your article is rich with a lot of useful knowledge and helpful information.
Best Engineering College in North India
Best MBA college in Punjab

quite eventful

Year 2013 has been quite eventful. SRSG also witnessed events and evolved in terms of business verticals, technologies, people and processes.
Apple authorized service center delhi
Apple Product Dealers in Kolkata
ipad dealers in Mumbai
Macbook pro dealers in kolkata
iMac service center in Delhi


I have been looking for similar information since a while. After installing the latest windows upgrade, I felt so much difficulty in dealing with the similar processes. Glad that you have provide with the details for the same. Thanks for the share. prefabricated houses

Solutions Middle East, works

Solutions Middle East, works to Envision, Execute and Empower our clients by offerings design, implementation and on-going support services.
Storage providers in dubai
SafeQ version
IT Business Tracking System
End point tracking
Staff Outsourcing Companies

VIGYAN DHARA is the first

VIGYAN DHARA is the first Indian IIT-JEE/ NEET/AIIMS coaching Institute which is purely based on Historical Indian Education System that is the Gurukul Tradition of Education where students leave their homes during their studies.
Best PMT coaching institute in hisar
IIT coaching institute in hisar

Moonlight Films and Theatre

Moonlight Films and Theatre Studio operates under the Moonlight Films and Theatre Society , a non-profit association whose goals are to spread knowledge of and interest in theatrical and media arts through education.
Filmmaking courses in Delhi
Acting classes in Delhi

The Fairdeal Group of

The Fairdeal Group of Companies is a fast-growing group with the ambition of helping people across Kenya build their dream homes and offices.
Furniture in Kenya

IT Software

SCS is focused on providing the most comprehensive, heterogeneous, integrated IT Software solutions to enterprises around the world.
Lakeside Assessment
Online Faxing solutions provider
Ysoft SafeQ
Messaging Solutions
Open text Faxing Solution

Thanks for posting this info.

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. Le Quest Condo


Glad to get the details regarding the resizing of the image in windows 8 from this post. This will be very helpful in dealing with the similar processes while using the windows 8 systems. I want to learn more helpful details like this. Please share more of these. re0f10a


Take One School of Mass Communication considered as a part of the Top Mass Communication Colleges in India. It was set up in the year 2003 by honing media experts who met with a mutual vision of giving a stage to the adolescent of the nation.
Mass communication colleges in Delhi


Your post was very nicely written. I’ll be back in the future for sure!

wow, great, I was

wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <asp>, <c>, <cpp>, <cs>, <css>, <drupal5>, <drupal6>, <html4strict>, <java>, <javascript>, <jquery>, <php>, <python>, <ruby>, <sql>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.