This chapter is taken from book "Programming Windows Phone 7" by Charles Petzold published by Microsoft press. http://www.charlespetzold.com/phone/index.html
In a Silverlight program, you've seen that a bitmap added to the project as a resource is bound into the executable. It's so customary to reference that local bitmap directly from XAML, SilverlightTapToLoad project shows you how.
The MainPage.xaml.cs file requires a
using directive for the
System.Windows.Media.Imaging
namespace for the BitmapImage
class. Another
using directive for
System.Windows.Resources
is required for the
StreamResourceInfo
class. When the screen is tapped, the event handler accesses the resource using
the static GetResourceStream
method defined by the
Application class:
protected
override void
OnManipulationStarted(ManipulationStartedEventArgs
args)
{
Uri uri = new
Uri("/SilverlightTapToLoad;component/Images/Hello.png",
UriKind.Relative);
StreamResourceInfo resourceInfo =
Application.GetResourceStream(uri);
BitmapImage bmp =
new BitmapImage();
bmp.SetSource(resourceInfo.Stream);
img.Source = bmp;
args.Complete();
args.Handled =
true;
base.OnManipulationStarted(args);
}
Notice how complicated that URL is! It begins with the name of the program followed by a semicolon, followed by the word "component" and then the folder and filename of the file. If you change the Build Action of the Hello.png file to Content rather than Resource, you can simplify the syntax considerably:
Uri uri = new Uri("Images/Hello.png", UriKind.Relative);
In a document entitled "Creating High Performance Silverlight Applications for Windows Phone," Microsoft has recommending using a Build Action of Content rather than Resource for assets included in your application to minimize the size of the binary and startup time. However, if these assets are in a Silverlight library that the program references, then it is better for them to be embedded in the binary with a Build Action of Resource.
If you have a number of images in your program, and you don't want to include them all in the XAP file, but you're nervous about downloading the images, why not do a little of both? Include low resolution (or highly compressed) images in the XAP file, but download better versions asynchronously while the application is running.
Capturing from the Camera
Besides embedding bitmaps in your application or accessing them from the web,
Windows Phone 7 also allows you to acquire images from the built-in camera. The classes you use for this job are in the
Microsoft.Phone.Tasks
namespace, which contains several classes referred to as
choosers and
launchers.
Conceptually, these are rather similar, except that choosers return data to your
program but launchers do not.
The CameraCaptureTask
is derived from the generic
ChooserBase
class which defines a
Completed event and a
Show method.
Your program attaches a handler for the
Completed event
and calls Show.
When the Completed
event handler is called, the
PhotoResult
event argument contains a
Stream object to the photo. From there, you already know what
to do. Here's the entire
code-behind file:
using
System.Windows.Input;
using
System.Windows.Media.Imaging;
using
Microsoft.Phone.Controls;
using
Microsoft.Phone.Tasks;
namespace
SilverlightTapToShoot
{
public
partial class
MainPage :
PhoneApplicationPage
{
CameraCaptureTask camera =
new
CameraCaptureTask();
public MainPage()
{
InitializeComponent();
camera.Completed += OnCameraCaptureTaskCompleted;
}
protected
override void
OnManipulationStarted(ManipulationStartedEventArgs
args)
{
camera.Show();
args.Complete();
args.Handled = true;
base.OnManipulationStarted(args);
}
void
OnCameraCaptureTaskCompleted(object
sender, PhotoResult args)
{
if (args.TaskResult ==
TaskResult.OK)
{
BitmapImage bmp =
new
BitmapImage();
bmp.SetSource(args.ChosenPhoto);
img.Source = bmp;
}
}
}
}
You can run this program on the phone emulator. When you tap the emulator screen, the call to Show causes the camera task to start up and you'll navigate to a page that resembles the actual camera. You can "shoot" a photo by tapping an icon in the upper-right corner of the screen. The simulated "photo" just looks like a large white square with a small black square inside one of the edges. Then you need to click the Accept button.
You can also run this program on the phone itself, of course, but not when the phone is tethered to the PC and the Zune software is running. After deploying the application to the phone using Visual Studio, you'll need to close the Zune software before testing the program.
When the SilverlightTapToShoot program calls the Show method on the CameraCaptureTask object, the SilverlightTapToShoot program is terminated. (Not immediately, though. The OnManipulationStarted method is allowed to return back to the program, and a couple other events are fired, but then the program is definitely terminated.)
The camera utility then runs. When the camera utility has done its job, the SilverlightTapToShoot program is re-executed. It's a new instance of the program. The program starts up from the beginning, the MainPage constructor is eventually called which sets the Completed event of the CameraCaptureTask to OnCameraCaptureTaskCompleted, and then that method is called.

Join the conversation! Your thoughts help the community grow.