Silverlight for Windows Phone Programming Tip #1

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.

Jeff Prosise

View Comments

Recent Posts

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

5 days ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

3 weeks ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

1 month ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

1 month ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago

Mastering Compliance: The Definitive Guide to Managed Compliance Services

In the intricate landscape of modern business, compliance is both a cornerstone of operational integrity…

2 months ago