Share via


Dealing with PersonalImage

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

My current image is this:

With the power to... Melt!

(those interested into why that is my current image should post the results of their research in the comments)

PersonalImage is what is called a Singleton type, which - as you've probably already figured out - means that there can only be one of them. But, there might be none of them, so code needs to handle this case as well.

Here's some code you might find useful.

         using (Stream imageStream = System.IO.File.OpenRead(@"C:\Documents and Settings\ericgu\My Documents\My Pictures\213.jpg"))
        {
            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(@"g:\213.jpg"))
                    {
                        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);
            }
        }

Comments