简体   繁体   English

C#参数无效异常

[英]C# Parameter is not Valid Exception

I am trying to get Avatar from google talk. 我正在尝试从Google Talk中获取Avatar。 I received packet from goole talk server like: 我收到了来自goole talk服务器的数据包,例如:

<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. 我以为'<photo>'标签的十六进制表示是联系人的头像(显示图像)。 (Please correct me if I am wrong.) (如果我错了,请纠正我。)

I converted that value to byte[] and used following code to display the image. 我将该值转换为byte []并使用以下代码显示图像。

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[]: 我正在使用以下函数来从十六进制转换为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. 我在Windows 8.1机器上使用.net 3.5。

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. 我向服务器发送了“ iq”请求,并提供了头像。

Thanks a lot. 非常感谢。 Happy Coding. 编码愉快。

http://www.xmpp.org/extensions/xep-0153.html says following: http://www.xmpp.org/extensions/xep-0153.html表示以下内容:

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]. 接下来,用户的客户端根据RFC 3174 [4]计算化身图像数据本身(不是base64编码版本)的SHA1哈希。 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: 然后,此哈希作为包含'vcard-temp:x:update'命名空间的元素的子代的XML字符数据包含在用户的状态信息中,如以下示例所示:

Example 3. User's Client Includes Avatar Hash in Presence Broadcast 例子3.用户的客户端在状态广播中包括头像哈希

So, basically hex value of '' tag is not the avatar, but SHA1 hash of the avatar image. 因此,基本上'标签的十六进制值不是化身,而是化身图像的SHA1哈希。

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. 在XMPP客户端上登录后,您开始从XMPP服务器接收状态消息。
  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. 如果您没有针对散列的二进制图像,则向您接收到该用户的用户向XMPP服务器发送v卡请求。
  6. On receiving the v-card response, you will find the hash and the display image binary. 收到v卡响应后,您将找到哈希和显示图像二进制文件。 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 有关XMPP数据包的详细信息,请参阅http://www.xmpp.org/extensions/xep-0153.html#retrieve上的3.2节

According to this , the photo is Base64-encoded. 根据 ,照片是Base64编码。 So you simple need to call Convert.FromBase64String to get the byte array from the photo element InnerText. 因此,您只需调用Convert.FromBase64String即可从照片元素InnerText获取字节数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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