简体   繁体   中英

C# Parameter is not Valid Exception

I am trying to get Avatar from google talk. I received packet from goole talk server like:

<presence from="xxxxxxxxxxxxx@gmail.com/MessagingA3e8c9465" to="xxxxxxxxxx@gmail.com/Jabber.NetF5D1AB65">
<show>away</show>
<caps:c ver="1.1" node="http://www.android.com/gtalk/client/caps" xmlns:caps="http://jabber.org/protocol/caps" />
<x xmlns="vcard-temp:x:update">
    <photo>6373f2ccdf12ef06292ca2257dc0bdc9aa1040c2</photo>
</x>

I thought the hex vale of '<photo>' tag is the avatar (display image) of the contact. (Please correct me if I am wrong.)

I converted that value to byte[] and used following code to display the image.

pictureBox1.Image = Image.FromStream(new MemoryStream(byte_array));
// byte_array is byte[] converted from hex value.

It raises exception saying:

Parameter is not valid.

I am using the following function to covert from hex to byte[]:

private static byte[] HexString2Bytes(string hexString)
{
    int bytesCount = (hexString.Length) / 2;
    byte[] bytes = new byte[bytesCount];
    for (int x = 0; x < bytesCount; ++x)
    {
        bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
    }

    return bytes;
}

I tries many ways but same result.

I also tried to convert the hex value to uppercase, but no luck, same result.

I am using .net 3.5 on windows 8.1 machine.

Thanks

Updated: Thanks to every one for their comments and answer.

I was wrong the hex value was not avatar (display image).

I sent 'iq' request to server and it gives the avatar.

Thanks a lot. Happy Coding.

http://www.xmpp.org/extensions/xep-0153.html says following:

Next, the user's client computes the SHA1 hash of the avatar image data itself (not the base64-encoded version) in accordance with RFC 3174 [4]. This hash is then included in the user's presence information as the XML character data of the child of an element qualified by the 'vcard-temp:x:update' namespace, as shown in the following example:

Example 3. User's Client Includes Avatar Hash in Presence Broadcast

So, basically hex value of '' tag is not the avatar, but SHA1 hash of the avatar image.

The hex value that you see is not the display image of the contact. It is a hash of the display image. The logic to get the display image is as follows.

  1. After login on the XMPP client, you start receiving presence messages from the XMPP server.
  2. In the presence message, you receive the hash of the avatar.
  3. Check your local storage, if you have a binary image against the received hash.
  4. If you have a binary image against the hash, then display the avatar on your client from the local storage.
  5. If you do not have a binary image against the hash, send a request for v-card to the XMPP server, for the user against which you received the presence.
  6. On receiving the v-card response, you will find the hash and the display image binary. Store this in some local storage.

For details on the XMPP packets Read section 3.2 on http://www.xmpp.org/extensions/xep-0153.html#retrieve

According to this , the photo is Base64-encoded. So you simple need to call Convert.FromBase64String to get the byte array from the photo element InnerText.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM