IT tutorials
 
Mobile
 

Windows Phone 8 : Creating a Photos Extras Application (part 3) - Saving the Image

2/22/2014 2:29:22 AM
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

3. Saving the Image

After processing is complete, the user is provided with the means to save the image via an application bar icon button. The page contains an ICommand called SaveCommand, as shown:

readonly ICommand saveCommand;

public ICommand SaveCommand
{
    get
    {
        return saveCommand;
    }
}

The command is instantiated in the constructor and when executed calls the page’s SaveImage method, which is shown in the following excerpt:

public MainPage()
{
    InitializeComponent();

    saveCommand = new DelegateCommand(obj => SaveImage());

    DataContext = this;

    photoChooserTask.Completed += HandlePhotoChooserTaskCompleted;
}

The SaveImage method retrieves the WriteableBitmap from the Image control and then uses the WriteableBitmap extension method SaveJpeg to write the image bytes to a MemoryStream. The MemoryStream is then passed to the MediaLibrary.SavePicture method, as shown:

void SaveImage()
{
    var writeableBitmap = (WriteableBitmap)image.Source;
    if (writeableBitmap == null)
    {
        return;
    }

    const int jpegQuality = 90;

    using (MemoryStream memoryStream = new MemoryStream())
    {
        writeableBitmap.SaveJpeg(
            memoryStream,
            writeableBitmap.PixelWidth, // target width
            writeableBitmap.PixelHeight, // target height
            0, // orientation.
            jpegQuality);
        memoryStream.Seek(0, SeekOrigin.Begin);
        using (MediaLibrary mediaLibrary = new MediaLibrary())
        {
            Picture savedPicture
               = mediaLibrary.SavePicture(imageName, memoryStream);
        }
    }

    MessageBox.Show("Image saved to Saved Pictures.");
}

The MediaLibrary.SavePicture method requires that the specified image name is not null or an empty string. The SavePicture method has two overloads; one accepts a Stream of image bytes, the other a byte array representing the image. Both save the image to the Saved Pictures album in the Pictures Hub.

The XAML for the MainPage class contains an AppBarIconButton with a binding to the SaveCommand (see Listing 3).

LISTING 3. Extras Example MainPage XAML (excerpt)


<Grid x:Name="LayoutRoot" Background="Transparent">
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
  </Grid.RowDefinitions>

  <u:AppBar>
    <u:AppBarIconButton
                x:Name="button_Save"
                Command="{Binding SaveCommand}"
                Text="Save"
                IconUri="/Images/appbar.save.rest.png" />
  </u:AppBar>

  <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock Text="Windows Phone Unleashed"
                Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="extras" Margin="9,-7,0,0"
                Style="{StaticResource PhoneTextTitle1Style}"/>
  </StackPanel>

  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Image x:Name="image" />
    <ProgressBar x:Name="progressBar" IsIndeterminate="True" />
  </Grid>
</Grid>


When the SaveImage method completes, a message is displayed informing the user that the image was successfully saved (see Figure 5).

Image

FIGURE 5 The message box confirms that the image has been saved.

 
Others
 
- Windows Phone 8 : Creating a Photos Extras Application (part 2) - An Edge-Tracing Extras Application
- Windows Phone 8 : Creating a Photos Extras Application (part 1) - Adding Your App to the Extras Menu
- Java ME Subsystem Architecture : Mean and Lean Virtual Machine
- Java ME Subsystem Architecture : Application Management Software, MIDP Push
- Java ME Subsystem Architecture : Java Applications and Symbian OS
- BlackBerry Bold 9700 and 9650 Series : Surfing the Web - Going to a Web Page
- BlackBerry Bold 9700 and 9650 Series : Surfing the Web - Web Browsing on Your BlackBerry
- Windows Phone 8 : Windows Phone Toolkit (part 6) - WrapPanel Layout Container
- Windows Phone 8 : Windows Phone Toolkit (part 5) - CustomMessageBox
- Windows Phone 8 : Windows Phone Toolkit (part 4) - ToggleSwitch Control, ExpanderView Control, PhoneTextBox Control
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS