Export (0) Print
Expand All

Dealing with PersonalImage

HealthVault

PersonalImage is a type that encapsulates the picture that shows up for a person in the health record.

PersonalImage is a singleton type, which means that there can be only one of them. But there might be none of them, so code needs to handle this case as well:

 
using (Stream imageStream = System.IO.File.OpenRead(imageFileName))
{
    HealthRecordItemCollection collection =
        PersonInfo.SelectedRecord.GetItemsByType(
            PersonalImage.TypeId, HealthRecordItemSections.All);

    PersonalImage image = null;

    if (collection.Count != 0)
    {
        image = collection[0] as PersonalImage;

        using (Stream currentImageStream = image.ReadImage())
        {
            byte[] imageBytes = new byte[currentImageStream.Length];
            currentImageStream.Read(
                imageBytes, 0, (int)currentImageStream.Length);

            using (FileStream outputImage =
                System.IO.File.OpenWrite(outputFileName))
            {
                outputImage.Write(imageBytes, 0, imageBytes.Length);
            }
        }
    }

    if (image == null)
    {
        image = new PersonalImage();
        image.WriteImage(imageStream, "image/jpg");
        PersonInfo.SelectedRecord.NewItem(image);
    }
    else
    {
        image.WriteImage(imageStream, "image/jpg");
        PersonInfo.SelectedRecord.UpdateItem(image);
    }
}
Show:
© 2014 Microsoft