programming4us
           
 
 
Windows Phone

Programming Windows Phone 7 : The Phone’s Photo Library

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
2/16/2011 5:59:32 PM
As you take pictures with the phone and synchronize your phone with the PC, the phone accumulates a photo library. A program running on the phone can access this library in one of two ways:
  • From the perspective of your program, the PhotoChooserTask is much like the CameraCaptureTask except it takes the user to the photo library and allows the user to choose one photo, which is then returned to the program.

  • The XNA namespace Microsoft.Xna.Framework.Media has a MediaLibrary and related classes that let a program obtain collections of all the photos stored in the photo library, and present these to the user.

I’m going to show you these two approaches with two programs. Just for variety (and to demonstrate how to use XNA classes in a Silverlight program), I’ll use XNA for the first approach and Silverlight for the second.

You can run these two programs on the phone emulator. The emulator includes a small collection of photos specifically for testing programs such as these. When testing the programs on the actual phone, however, the phone must be untethered from the PC or the Zune software must be closed, because the Zune software won’t allow simultaneous access to the phone’s photo library. After you close Zune, you can run WPDTPTConnect32.exe or WPDTPTConnect64.exe program to allow Visual Studio to debug the program running on the phone.

The XnaTapToBrowse program requires a using directive for Microsoft.Phone.Tasks. It creates a PhotoChooserTask object along with the other fields:

Example 1. Silverlight Project: XnaTapToBrowse File: Game1.cs (excerpt showing fields)
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture;
PhotoChooserTask photoChooser = new PhotoChooserTask();
. . .
}

In compliance with the recommendations of the documentation, the class attaches a handler for the Completed event in the constructor:

Example 2. Silverlight Project: XnaTapToBrowse File: Game1.cs (excerpt)
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);

TouchPanel.EnabledGestures = GestureType.Tap;
photoChooser.Completed += OnPhotoChooserCompleted;
}

As usual, the Update method checks for user input. If a tap has occurred, the method calls the Show event of the PhotoChooserTask object:

Example 3. Silverlight Project: XnaTapToBrowse File: Game1.cs (excerpt)
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

while (TouchPanel.IsGestureAvailable)
if (TouchPanel.ReadGesture().GestureType == GestureType.Tap)
photoChooser.Show();

base.Update(gameTime);
}

void OnPhotoChooserCompleted(object sender, PhotoResult args)
{
if (args.TaskResult == TaskResult.OK)
texture = Texture2D.FromStream(this.GraphicsDevice, args.ChosenPhoto);
}


The handler for the Completed event then creates a Texture2D from the stream available from the ChosenPhoto property. The Draw override doesn’t attempt to render this object until it’s available:

Example 4. Silverlight Project: XnaTapToBrowse File: Game1.cs (excerpt)
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Navy);

if (texture != null)
{
spriteBatch.Begin();
spriteBatch.Draw(texture, this.GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.End();
}

base.Draw(gameTime);
}


I’m using a slight variation of the Draw method of SpriteBatch here. Rather than provide a position for the Texture2D in the second argument, I’m providing a whole rectangle equal to the size of the viewport. This causes the photo to expand (or, more likely, shrink) in size, very possibly distorting the image by not taking account of the original aspect ratio. More sophisticated code can handle those problems, of course.

The SilverlightAccessLibrary program requires a reference to the Microsoft.Xna.Framework DLL, and you’ll probably get a warning about including an XNA library in your Silverlight program. It’s OK! The content area in the MainPage.xaml file contains both a bitmap-less Image and a text-less TextBlock in the Grid:

Example 5. Silverlight Project: SilverlightAccessLibrary File: MainPage.xaml (excerpt)
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Name="img" />

<TextBlock Name="txtblk"
TextWrapping="Wrap"
TextAlignment="Center"
VerticalAlignment="Bottom" />
</Grid>

Rather than present the entire photo library to the user (a task that would be a little difficult with only the rudimentary Silverlight layout elements I’ve described so far), the program picks one at random, and picks another when the user taps the screen:

Example 6. Silverlight Project: SilverlightAccessLibrary File: MainPage.xaml.cs (excerpt)
public partial class MainPage : PhoneApplicationPage
{
MediaLibrary mediaLib = new MediaLibrary();
Random rand = new Random();

public MainPage()
{
InitializeComponent();
GetRandomPicture();
}

protected override void OnManipulationStarted(ManipulationStartedEventArgs args)
{
GetRandomPicture();

args.Complete();
base.OnManipulationStarted(args);
}

void GetRandomPicture()
{
PictureCollection pictures = mediaLib.Pictures;

if (pictures.Count > 0)
{
int index = rand.Next(pictures.Count);
Picture pic = pictures[index];

BitmapImage bmp = new BitmapImage();
bmp.SetSource(pic.GetImage());
img.Source = bmp;

txtblk.Text = String.Format("{0}\n{1}\n{2}",
pic.Name, pic.Album.Name, pic.Date);
}
}
}


The XNA MediaLibrary class is instantiated as a field. In the GetRandomPicture method, the program obtains a PictureCollection object from the MediaLibrary class and picks one at random. The Picture object has a GetImage method that returns a stream, and a Name, Album, and Data information that the program displays in the overlaying TextBlock.

A Windows Phone 7 program can also save a bitmap back into the library. All such bitmaps go into a special album called Saved Pictures.

Other -----------------
- Programming Windows Phone 7 : Capturing from the Camera
- Windows Phone 7 : Loading Local Bitmaps from Code
- Windows Phone 7 : Image and ImageSource
- Windows Phone 7 : Images Via the Web
- Windows Phone 7 : Customizing Your E-Mail Signature
- Windows Phone 7 : Managing Mail Folders
- Windows Phone 7: The Silverlight Image Element
- Windows Phone 7: XNA Texture Drawing
- Windows Phone 7: An Introduction to Touch - Routed Events
- Windows Phone 7 : Working with Attachments
- Programming Windows Phone 7: An Introduction to Touch - The Manipulation Events
- Programming Windows Phone 7: An Introduction to Touch - Low-Level Touch Events in Silverlight
- Windows Phone 7: Composing a New Message
- Programming Windows Phone 7: An Introduction to Touch - The XNA Gesture Interface
- Programming Windows Phone 7: An Introduction to Touch - Low-Level Touch Handling in XNA
- Windows Phone 7: Responding to a Message
- Windows Phone 7: Checking for New Messages
- Windows Phone 7: Sorting and Searching Your Mail
- Windows Phone 7: Customizing Your Contacts List
- Windows Phone 7: Working with the Me Card
 
 
 
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
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us