Introduction
A camera is one of the needed features of any phone. In our daily life, we use the camera so many times. So the Windows Phone 8 API has rovided us a flexible way to capture and save it your isolated storage.
Well, in this article we will capture our image and then save it to the Media folder.
Agenda
- First, create a layout
- Capture the image using the Camera Task
- Save it to The Isolated Storage of the Windows Phone
Procedures
Step 1
Create a Blank Windows Phone Silverlight App Project and try to make a UI like this.
Here, we have an Image Control and two buttons named btnSave and btnCaptute.
And add the following namespaces to your project:
- using Microsoft.Phone.Tasks;
- using System.Windows.Media.Imaging;
- using System.IO;
- using Microsoft.Xna.Framework.Media;
And we need a few global instances further. So, add them as well as in the following:
-
- BitmapImage bitMap;
- Random rand;
- WriteableBitmap saveBitMap;
And, we need Few Capabilities rights.
So, open WMAppManifest.xml and check few rights
Step 2
First, we will code the Capture button whose task is to call the Capture Image task and capture the image.
- private void btnCapture_Click(object sender, RoutedEventArgs e)
- {
- CameraCaptureTask camera = new CameraCaptureTask();
- camera.Completed += camera_Completed;
- camera.Show();
- }
-
- void camera_Completed(object sender, PhotoResult e)
- {
- if (e.TaskResult == TaskResult.OK)
- {
- bitMap = new BitmapImage();
- bitMap.SetSource(e.ChosenPhoto);
-
- caputredImage.Stretch = System.Windows.Media.Stretch.UniformToFill;
- caputredImage.Source = bitMap;
-
-
- btnSave.IsEnabled = true;
- }
- }
Explanation
First, we are creating an instance of the CameraCaptureTask that activates the physical camera of the phone to capture the image.
Then, we linked an event handler to the Completed event.
Within that event handler, we first check whether the task has completed or not. And saved the image to Bitmap instance.
Since every image is stored in a BitmapImage object, to display the captured image in our Image control, we need a BitmapImage instance where we have called the SetSource() method as the camera instance as the parameter. Finally, we have assigned a bitmap instance to the Source property of our Image control.
Note: capturedImage is the name of my Image control.
Until now, we have our Image on the Image control. Now, we want to save the captured image in Isolated Storage.
Step 3
- private void btnSave_Click(object sender, RoutedEventArgs e)
- {
-
- rand = new Random();
- saveBitMap = new WriteableBitmap(bitMap);
-
-
- using(MemoryStream stream=new MemoryStream())
- {
- saveBitMap.SaveJpeg(stream, saveBitMap.PixelWidth, saveBitMap.PixelHeight, 0,100);
- stream.Seek(0, SeekOrigin.Begin);
-
- using (MediaLibrary media = new MediaLibrary())
- {
- media.SavePicture("MyDemo_"+rand.Next(0,1000)+".jpg",stream);
-
-
- MessageBox.Show("Saved Succcessfully","Saved !!",MessageBoxButton.OK);
- }
- }
- }
Explanation
We have a randon() instance to create a random-unique name while saving. A SaveBitmapImage() instance is needed to save a Bitmap Image to isolated storage. Since file manipulation needs a stream, here we need a MemoryStream(). The SaveImage() method needs a stream instance along with pixel height and width, position and quality as parameters.
And then, we need a MediaLibrary() instance that saves in Isolated Storage and it is under a Microsoft.Xna.Framework.Media namespace.
Conclusion
We have designed a prototype that saves the captured image to a Media Folder of Windows Phone. And you can put this in your something as you need to in your App.
If you encounter any issue in the code, then you can refer to the Solution File that has been enclosed in this article.