23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

ImageProperties.savePropertiesAsync(properties).done(function () {<br />

// ...<br />

}, function (error) {<br />

// Some error handling as some properties may not be supported by all image formats.<br />

});<br />

A few noteworthy features of this code include the following:<br />

• It separates keywords in the UI control and separately appends each to the keywords property<br />

vector.<br />

• It creates a new collection of properties of type Windows.Foundation.Collections.PropertySet<br />

and uses its insert method to add properties to the list. This property set is what’s expected by<br />

the savePropertiesAsync method.<br />

• The Helpers.convertStartsToSystemRating method (see js/default.js) converts between 1–5<br />

stars, as used in the WinJS.UI.Rating control, to the System.Rating value that uses a 1–99 range.<br />

The documentation for System.Rating specifically indicates this mapping.<br />

In general, all the detailed information you want for any particular Windows property can be found<br />

on the reference page for that property. Again start at the Windows Properties and drill down from<br />

there.<br />

Image Manipulation and Encoding<br />

To do something more with an image than just loading and displaying it (where again you can apply<br />

various <strong>CSS</strong> transforms for effect), you need to get to the actual pixels by means of a decoder. This<br />

already happens under the covers when you assign a URI to an img.src., but to have direct access to<br />

pixels means decoding manually. On the flip side, saving pixels back out to an image file means using an<br />

encoder.<br />

WinRT provides APIs for both in the Windows.Graphics.Imaging namespace, namely in the<br />

BitmapDecoder, BitmapTransform, and BitmapEncoder classes. Loading, manipulating, and saving an<br />

image file often involves these three classes in turn, though the BitmapTransform object is focused on<br />

rotation and scaling so you won’t use it if you’re doing other manipulations.<br />

One demonstration of this API can be found in Scenario 2 of the Simple imaging sample. I’ll leave it<br />

to you to look at the code directly, however, because it gets fairly involved—up to 11 chained promises<br />

to save a file! It also does all decoding, manipulation, and encoding within a single function such as<br />

saveHandler (js/scenario2.js). Here’s the process it follows:<br />

• Open a file with StorageFile.openAsync, which provides a stream.<br />

• Pass that stream to the static method BitmapDecoder.createAsync which provides a specific<br />

instance of BitmapDecoder for the stream.<br />

• Pass that decoder to the static method BitmapEncoder.createForTranscodingAsync, which<br />

442

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!