All about WP7 Isolated Storage - Read and Save Captured Image
published on: 4/11/2011 | Views: N/A | Tags: IsoStore Images
by WindowsPhoneGeek
This is the 8th article from the "All about WP7 Isolated Storage " series of short articles focused on real practical examples with source code rather than a plain theory. I am going to talk about how to read and save Captured Images into Isolated Storage.
- All about WP7 Isolated Storage - intro to Isolated Storage
- All about WP7 Isolated Storage - Creating Folders and files
- All about WP7 Isolated Storage - Store data in IsolatedStorageSettings
- All about WP7 Isolated Storage - Read and Save Text files
- All about WP7 Isolated Storage - Read and Save XML files using XmlSerializer
- All about WP7 Isolated Storage - Read and Save XML files using XmlWriter
- All about WP7 Isolated Storage - Read and Save Images
- All about WP7 Isolated Storage - Read and Save Captured Image
- All about WP7 Isolated Storage - Read and Save Binary files
- All about WP7 Isolated Storage - File manipulations
- All about WP7 Isolated Storage - Recommendations and Best Practices
- All about WP7 Isolated Storage - open source Databases and Helper libraries
In this post we will use the CameraCaptureTask .For more info about performing different tasks in WP7 take a look at our "Launchers and Choosers How to" series of articles.
NOTE: Use a real device to test CameraCaptureTask! It is not possible to simulate the full camera capture experience on Emulator! In order to be able to run CameraCaptureTask on Emulator your display driver needs to be directx10 minimum andWDDM1.1 Compliant. Otherwise you will see a black screen. Here are some screen shots:
To begin with lets first create a sample Windows Phone 7 application project and include the following namespaces in MainPage.xaml.cs (alternatively you can use the code in another page):
using Microsoft.Phone.Tasks; using System.Windows.Media.Imaging; using System.IO.IsolatedStorage; using System.IO; using Microsoft.Phone;
include the following namespaces in MainPage.xaml.cs (alternatively you can use the code in another page):
Save Captured Image to Isolatedstorage
This example demonstrates how to save a captured image to the Isolatedstorage. At first we use the CameraCaptureTask to capture the desired Image. After that we save the Image as Jpg file to Isolatedstorage using BitmapImage and WritableBitmap:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
ShowCameraCaptureTask();
}
private void ShowCameraCaptureTask()
{
CameraCaptureTask photoCameraCapture = new CameraCaptureTask();
photoCameraCapture = new CameraCaptureTask();
photoCameraCapture.Completed += new EventHandler<PhotoResult>(photoCameraCapture_Completed);
photoCameraCapture.Show();
}
void photoCameraCapture_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
SaveToIsolatedStorage(e.ChosenPhoto,"image1.jpg");
}
}
private void SaveToIsolatedStorage(Stream imageStream, string fileName)
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(fileName))
{
myIsolatedStorage.DeleteFile(fileName);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(imageStream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
}
NOTE: Use a real device to test CameraCaptureTask! It is not possible to simulate the full camera capture task in Emulator!
Read Captured Image from Isolatedstorage
In this example at first open the previously created image1.jpg Image file from the Isolated Storage and read its content. After that the content is visualized in an Image control:
private void ReadFromIsolatedStorage(string fileName)
{
WriteableBitmap bitmap = new WriteableBitmap(200,200);
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
// Decode the JPEG stream.
bitmap = PictureDecoder.DecodeJpeg(fileStream);
}
}
this.img.Source = bitmap;
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
this.ReadFromIsolatedStorage("image1.jpg");
}
NOTE: When open an existing file from the Isolated Storage we use FileMode.Open, when we want to read to a file we use FileAccess.Read! You can find a list of all FileAccess possibilities here.
NOTE: "img" is an ImageControl placed inside MainPage.xaml: <Image x:Name="img"/>
In this article I talked about reading and saving Images into Isolated Storage. Here is the full source code:
Stay tuned with the rest of the posts. I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Thanks to the author for information
posted by: viagra on 9/17/2011 8:56:07 AM
I liked the article, but some disagree
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
