This blog post walks you through an app which captures image from the device camera using the CameraCapture Chooser, saves it on the phone storage, retrieve it back and update the app live tile with this image
1. Lets get started by creating a new Windows Phone Application from the Visual Studio File Menu. We are using Visual C# - Silverlight for Windows Phone Template for this project.
2. Now drag two Image controls and three buttons from Windows Phone Control Toolbox on the page. Name the first image as myImage, second image as smallerImage, buttons as btnClick (set content property as Capture), btnRead (content as Retrieve), btnTile (content as Set Tile). Your page should look like the one shown below:
3. Add the following namespaces which we will be requiring in our code
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Shell;
private byte[] _imageBytes;
CameraCaptureTask CCT = new CameraCaptureTask();
string imageFolder = @"\Shared\ShellContent";
string imageFileName = "DemoImage.jpg";
public MainPage()
{
InitializeComponent();
CCT.Completed += new EventHandler<PhotoResult>(capture_completed);
}
private void btnClick_Click(object sender, RoutedEventArgs e)
{
try
{
CCT.Show();
}
catch (System.InvalidOperationException ex)
{
MessageBox.Show("An error occurred");
}
}
void capture_completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//Display the photo in an image control named myImage and save the image with dimensions of 150x150 pixels
WriteableBitmap WriteableBMP = new WriteableBitmap(150, 150);
WriteableBMP.LoadJpeg(e.ChosenPhoto);
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source = bmp;
//Using Isolated storage for storage
using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isoFile.DirectoryExists(imageFolder))
{
isoFile.CreateDirectory(imageFolder);
}
string filePath = System.IO.Path.Combine(imageFolder, imageFileName);
using (var stream = isoFile.CreateFile(filePath))
{
WriteableBMP.SaveJpeg(stream, WriteableBMP.PixelWidth, WriteableBMP.PixelHeight, 0, 100);
}
}
}
}
private void btnRead_Click(object sender, RoutedEventArgs e)
{
//Reading the image back from storage
BitmapImage imgFromStorage = new BitmapImage();
using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
string filePath = System.IO.Path.Combine(imageFolder, imageFileName);
using (var imageStream = isoFile.OpenFile(
filePath, FileMode.Open, FileAccess.Read))
{
imgFromStorage.SetSource(imageStream);
}
}
smallerImage.Source = imgFromStorage;
}
public void CreateApplicationTile()
{
var appTile = ShellTile.ActiveTiles.First();
//Retrieve the seconds part of current timestamp
int Sec=System.DateTime.Now.Second;
var store = IsolatedStorageFile.GetUserStoreForApplication();
if (appTile != null)
{
var standardTile = new StandardTileData
{
Title = "Live Tile Demo",
BackgroundImage = new Uri(@"isostore:/Shared/ShellContent/DemoImage.jpg", UriKind.Absolute),
//Set the tile count to current seconds value
Count=Sec,
BackTitle = "Tile Back Title",
BackBackgroundImage = new Uri(@"isostore:/Shared/ShellContent/DemoImage.jpg", UriKind.Absolute),
BackContent = "Live Tile Demo Back Title"
};
appTile.Update(standardTile);
}
}
private void btnTile_Click(object sender, RoutedEventArgs e)
{
CreateApplicationTile();
}
Related Resources
Isolated Storage for Windows Phone
Choosers for Windows Phone
Tiles overview for Windows Phone
Comments
Post a Comment