I’m spending the bulk of my time these days writing applications for Windows phones using Silverlight for Windows Phone (and having a blast doing it, I might add). As I write, I think of lots of little tips that can save time, reduce aggravation, and help all of us build better applications. So I thought I’d begin a series of tips that I can add to over time. This is the first in that series.
The Silverlight for Windows Phone documentation contains a helpful article demonstrating how to encode a WriteableBitmap as a JPEG and use the XNA framework’s MediaLibrary class to save it in the phone’s pictures library. The code works, but it’s way more work than you have to do.
Rather than create a temporary file in isolated storage, write the JPEG into the temporary file, transfer the JPEG from isolated storage into the pictures library, and then delete the file per the example in the documentation, you can accomplish the same thing with four lines of code:
using (MemoryStream stream = new MemoryStream(bitmap.PixelWidth * bitmap.PixelHeight * 4))
{
Extensions.SaveJpeg(bitmap, stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
MediaLibrary library = new MediaLibrary();
library.SavePicture(“SavedPicture.jpg”, stream.ToArray());
}
In this example, bitmap is a variable containing a reference to the WriteableBitmap you wish to encode and save. The MediaLibrary class is found in the Microsoft.Xna.Framework assembly, so you’ll need to add a reference to that assembly to your project in order to use it.
Cloud management is difficult to do manually, especially if you work with multiple cloud…
Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…
https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…
FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…
Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…
In the intricate landscape of modern business, compliance is both a cornerstone of operational integrity…
View Comments
Top Tip! Thanks Jeff.
Nice.
I've written an extension method back in July 2010 that uses the same technique. With the help of this extension method, it becomes an one liner. ;)
http://kodierer.blogspot.com/2010/07/photos-photos-photos-how-to-save-load.html
Scope Your SIP - sounds painful, doesn't it? Adding on to Jeff 's series concerning WP7 Silverlight development
Great! It's helpful! Thanks!
Thanks for sharing this, and thanks for providing the source code, very helpful...thanks again! :-)
Thanks for sharing information