How do I upload a photo for a contact using the Google Contact dotnet client library

51 views
Skip to first unread message

Camel

unread,
Mar 1, 2009, 10:20:15 PM3/1/09
to Google Contacts API
After some research, I've tried the following code:

System.Drawing.Bitmap Image = new Bitmap( @"C:\test.jpg" );
System.IO.MemoryStream Memory = new MemoryStream();
Image.Save( Memory, System.Drawing.Imaging.ImageFormat.Jpeg );

Service.Update
(
Contact.PhotoEditUri,
Memory,
"image/jpeg",
null
);

The Service is using ClientLogin authentication. Before this code
runs, I have successfully retrieved the contact.

After this code runs, however, I get a GDataRequestException. The
error message returned is, "A temporary internal problem has occurred.
Try again later". The error message in the InnerException contains the
following, "The remote server returned an error: (500) Internal Server
Error."

After yet more research, I found hints that the error codes from the
Google service may not be all that descriptive - and may simply return
error 500.

So maybe the problem is with my code? Does anybody have sample code to
demonstrate how this should be done?

Thanks,

Camel

Julian (Google)

unread,
Mar 3, 2009, 10:31:07 AM3/3/09
to Google Contacts API
Hi Camel,

Are you getting the error only with this particular image? Please try
a smaller one.

You can also try the following:

System.IO.FileStream fs = new System.IO.FileStream("C:\\foto.jpg",
System.IO.FileMode.Open);
contactsService.Update(entry.PhotoEditUri, fs, "image/jpg", "Contact
Photo");

Something important, the latest library was updated to use Contacts V2
and the EditPhotoUri is not longer used.

-Julian

Camel

unread,
Mar 4, 2009, 11:22:04 PM3/4/09
to Google Contacts API
Hi Julian,

I switched from Image/MemoryStream to FileStream as you suggested and
this has resolved the issue! Many thanks - I've been banging my head
against a brick wall way too long on this issue.

Then I moved onto the next stage of my mini-project. I'm using a 3rd
party library to read vCard files. In the vCard library, the Photo
field is implemented as an Image. So I'm back to square one! Although
at least now I know that the problem has something to do with the
Image.Save( Stream, ... ) function.

I did a little more testing and found out the following:

* Tried Image.Save( MemoryStream, Image.RawFormat ) - that didn't make
any difference.
* Tried Image.Save( MemoryStream, ImageCodecInfo, Parameters ) passing
up the Compression parameter for a Jpeg. This also made no difference.
* Tried Image.Save( FileName ) and then opened the file using a newly
constructed FileStream. This worked, so there must something I'm
missing with the other versions of the routine.

I'd like to do this in memory. Any suggestions?

Regards,

Camel

Frank Mantek

unread,
Mar 5, 2009, 2:32:24 AM3/5/09
to google-co...@googlegroups.com
I think the main reason the code failed with the memory stream is, that after you save the image to the stream, the location inside the stream is at the end, so when we try to send it, it won't work... you probably want to rewind the stream before you give it to the service method.


Frank

Camel

unread,
Mar 5, 2009, 6:12:58 PM3/5/09
to Google Contacts API
Excellent! Thank you! Here's the final working code:

System.Drawing.Bitmap Image = new Bitmap( @"C:\test.jpg" );
System.IO.MemoryStream Memory = new MemoryStream();
Image.Save( Memory, System.Drawing.Imaging.ImageFormat.Jpeg );
Memory.Seek( 0, SeekOrigin.Begin );

Service.Update
(
Contact.PhotoEditUri,
Memory,
"image/jpeg",
null
);

On Mar 5, 6:32 pm, Frank Mantek <fman...@gmail.com> wrote:
> I think the main reason the code failed with the memory stream is, that
> after you save the image to the stream, the location inside the stream is at
> the end, so when we try to send it, it won't work... you probably want to
> rewind the stream before you give it to the service method.
>
> Frank
>
Reply all
Reply to author
Forward
0 new messages