In a previous post I wrote about some tips for creating WP7 apps which included using the CameraCaptureTask. A reader responded, asking how to save the picture. So I figured I’d write a more detailed response with some samples.
In this post I’ll show 3 easy sample:
All of these are pretty simple. I’m sure once you get started you can easily modify this code to do more creative things.
First, you’ll need to define a CameraCaptureTask. You should always do so as a class level variable. You should also wire up the Completed handler in the constructor These steps are important for dealing with application tombstoning. For more on this, please read this article.
private CameraCaptureTask _cameraCaptureTask; public MainPage() { InitializeComponent(); _cameraCaptureTask = new CameraCaptureTask(); _cameraCaptureTask.Completed += CameraCaptureTaskCompleted; }
You’ll need to kick off the camera task, I’m using a button.
private void SimpleTest_Click(object sender, EventArgs e) { try { _cameraCaptureTask.Show(); } catch (InvalidOperationException ex) { // Catch the exception, but no handling is necessary. } } In my XAML, I’ve defined an Image named MainImage
<Image x:Name="MainImage" />
Now when the CameraCaptureTask is complete, just bind the results of the task to the Image as I’m doing here:
void CameraCaptureTaskCompleted(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { //simply use the picture. BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(e.ChosenPhoto); MainImage.Source = bitmapImage; } } Easy, right?
In this case I want to save the image to IsolatedStorage. In addition, the app I was creating didn’t need full size images. So I figured, why waste space in the user’s IsolatedStorage? So I use a WritableBitmap and change the size of the image. You’ll notice that after I save the image, I read it back and bind the results to another image named SmallerImage. That’s just to prove that saving it really worked!
void CameraCaptureTaskCompleted(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { //here I save the image to Isolated Storage. Also I am changing the size of it to not waste space! WriteableBitmap writeableBitmap = new WriteableBitmap(200, 200); writeableBitmap.LoadJpeg(e.ChosenPhoto); string imageFolder = "Images"; string imageFileName = "TestImage.jpg"; using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { if (!isoFile.DirectoryExists(imageFolder)) { isoFile.CreateDirectory(imageFolder); } string filePath = Path.Combine(imageFolder, imageFileName); using (var stream = isoFile.CreateFile(filePath)) { writeableBitmap.SaveJpeg(stream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100); } } //now read the image back from storage to show it worked... BitmapImage imageFromStorage = new BitmapImage(); using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { string filePath = Path.Combine(imageFolder, imageFileName); using (var imageStream = isoFile.OpenFile( filePath, FileMode.Open, FileAccess.Read)) { imageFromStorage.SetSource(imageStream); } } SmallerImage.Source = imageFromStorage; } }
This one is pretty easy too. Just remember to add a reference to Microsoft.Xna.Framework or you can’t access the Media Library. Also, you’ll need this using statement:
using Microsoft.Xna.Framework.Media;
void CameraCaptureTaskForSavingToLibraryCompleted(object sender, PhotoResult e) { byte[] imageBits = new byte[(int)e.ChosenPhoto.Length]; e.ChosenPhoto.Read(imageBits, 0, imageBits.Length); e.ChosenPhoto.Seek(0, SeekOrigin.Begin); MediaLibrary library = new MediaLibrary(); library.SavePicture("TestPhoto", imageBits); }
Hopefully you’ll see that using this feature is pretty easy.
Remember Me
.Net (56) asp.net (8) asp.net mvc (2) Ben (19) Blog (6) C# (8) Code Camp (28) Entertainment (2) Family (21) Fun Stuff (4) General (19) Hiking (1) jquery (1) LINQ (7) Microsoft (15) MOQ (2) Movies (2) MSBuild (2) MVVM (2) NoDeNUG.org (4) NUnit (3) Orbius (2) Philly.Net (50) Sarah (1) Silverlight (14) SQL Server (3) Subversion (2) Tech-Ed 2007 (3) Technology (24) Travel (2) Unit Testing (2) Vista (7) Visual Studio (3) Web (9) Windows 7 (2) Windows Phone 7 (1)
Powered by: newtelligence dasBlog 1.9.6264.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012, Andrew Schwam
E-mail