How to add a photo in contact ?

Topics: User Forum
Dec 29, 2006 at 9:15 AM
I try to add a photo in a windows contact item.

Uri url = new Uri( @"D:\Photo.jpeg" );
Photo photoItem = new Photo( url );
contact.Photos.Add( photoItem );


When I view contact added with my soft, the picture is not displayed.

Other fields are ok.

Anybody can help me ?
Coordinator
Jan 3, 2007 at 4:39 AM
To get the photo to display in the WAB/Explorer Reading Pane UI, you need to add the UserTile label.

E.g. you can change your code to:

Uri url = new Uri( @"D:\Photo.jpeg" );
Photo photoItem = new Photo( url );
contact.Photos"UserTile" = photoItem;

I missed adding the supported labels for Photos in Beta0. If you're building the library from more recent sources I kust checked in the fix so you can use PhotoLabels.UserTile instead of the string.
Jan 3, 2007 at 7:19 AM
Hello,

Thanks for your reply.

I have found another method (I think it's better), to add a picture in contact manager.

Bitmap bm = new Bitmap("...");
if (bm != null)
{
MemoryStream imgStream = new MemoryStream();
if (imgStream != null)
{
bm.Save( imgStream,
ImageFormat.Jpeg );

string nodeName = contact.AddNode (PropertyNames.PhotoCollection, true);

contact.SetBinaryProperty(nodeName +
PropertyNames.Value,
imgStream,
"image/jpeg");

contact.GetLabelCollection(nodeName).Add("UserTile");

imgStream.Dispose();
}
bm.Dispose();
}
Coordinator
Jan 3, 2007 at 5:31 PM
Yes, putting the image stream directly in the contact is generally a better solution. It's more portable, but it also increases the size (bmps more so than jpegs). Depending on how you plan on the photo being displayed, it may make sense to scale the image down (e.g. Explorer will never display a user tile larger than 128x128) if it's very large before storing it in the contact. MSXML can start having issues with very large XML files.

Glad it's working for you :)
Jan 5, 2007 at 8:04 AM
yes I think it's better too, but It's possible to add a big picture (like 2000x2000 and over) with Explorer without any resize.

And in this case, contact file is very big.

What is the max size and max dimensions for contact picture?
Coordinator
Jan 5, 2007 at 8:44 PM
There's no imposed limit on either size or dimensions. But since the stream is stored in XML and an XSD is used to validate, you will see probably see performance degredation if you put too much data of any kind into a contact, and MSXML may just choke on super large files. That's leaving alone OutOfMemory exceptions you may start getting from .Net. It's up to your application to determine whether scaling (or cropping, for that matter) is appropriate given the context; sometimes it is, sometimes not. For user tiles, I think it generally would be okay to scale down very large pictures.

The same caveat exists for all property types. You could put the full text of War and Peace into the Notes property, but having UI that limits the number of characters there would generally be OK.
Jan 8, 2007 at 9:48 AM
Thanks for your comment.
Jan 15, 2007 at 9:03 AM
In your sample code from Wiki page, there is an error.

using (Bitmap bitmap = new Bitmap("D:\\MyPicture.jpg"))
{
using (MemoryStream memstream = new MemoryStream())
{
bitmap.Save(memstream, ImageFormat.Jpeg);
// By adding the preferred label here, programs will
// generally use this photo when looking for a UserTile
// instead of one without "Preferred".
contact.Photos.Add(new Photo(memstream), PropertyLabels.Preferred, PhotoLabels.UserTile);
}
}

There is no constructor for "new Photo(memstream)".
The best constructor is "Photo(Stream stream, string type)".
But what is type value ?


Coordinator
Jan 15, 2007 at 6:06 PM
Thanks for catching that. I'll update the page.

Type is a mime-type string. Generally it would be of the form "image/jpeg" or the like.

Though really it can be anything. Programs can optionally use it to determine rendering effects and capabilities. Most .Net image classes that work against GDI+ will infer the type of the image just based on the stream contents, so it's not strictly necessary to use or provide it in that context.