【问题标题】:Base64 decoding of a image gives a red question mark图像的 Base64 解码给出红色问号
【发布时间】:2013-05-29 16:25:16
【问题描述】:

我在 unity3d 中使用 base64 字符串编码和解码 facebook 缩略图图像数据。当用户没有头像时,生成的图像是一个红色问号。有什么方法可以识别发送的图片无效,以便我可以将其替换为我选择的默认图片?

我正在使用 c# 中的 Convert.FromBase64String(字符串编码)将字符串转换为图像数据。

【问题讨论】:

  • 您可以只对无效图像进行 Base64 编码并将其与您收到的图像进行比较。

标签: c# base64 unity3d


【解决方案1】:

我假设您使用一些 API 从 URL 中检索 base64 编码的字符串?

在这种情况下,您只需将检索到的字符串转储到控制台一次,然后将其复制到您的源代码中,并将其与您将来获得的字符串进行比较。当然,如果您使用的 facebook API 决定提供不同的图标,这将中断,在这种情况下,您将不得不转储新的“未知用户”缩略图。

string encoded = ... // however you obtain your thumbnail
print encoded; // dump the string to the console once. remove this statement later
if (encoded == "...here comes the (rather large) string you just copied")
    encoded = "...here comes some other image you like to use, encoded as string";
...

不是很优雅,但至少很容易实现。

【讨论】:

    【解决方案2】:

    我想,如果在不存在个人资料图片时可以预见地返回错误图像(例如“红色问号”图像),您可以简单地测试这种情况并用您选择的另一个图像替换。您可以选择将红色问号图像存储为资源,然后将其 Base64 字符串与请求返回的每个图像的 Base64 进行比较。

    在这种情况下,代码的关键部分可能如下所示(假设您已经将图像的 Base64 字符串存储在资源中):

    ResourceManager rm =
        new ResourceManager("ExampleAppData", typeof(ExampleApp).Assembly);
    
    String errorImageBase64 = rm.GetString("ErrorImageBase64");
    
    // the image you get from your request
    String resultImageBase64 = GetProfileImageBase64();
    
    Image missingProfile;
    if(resultImageBase64.Equals(errorImageBase64))
    {
        missingProfile = ImageFromBase64String(rm.GetString("MissingProfileBase64"));
    }
    else
    {
        missingProfile = ImageFromBase64String(resultImageBase64);
    }
    

    参考资料:
    http://msdn.microsoft.com/en-us/library/2xsy4hac.aspx
    http://ozgur.ozcitak.com/snippets/2009/12/21/base64-encoding-an-image-with-csharp.html

    【讨论】:

      【解决方案3】:

      根据我的经验,这是由于请求错误造成的。 在我的情况下,返回红色问号的错误 API 查询是 /{fbId}/picture?g&type=square&height=128&width=128 我解决了删除“?g”,现在的工作查询是 /{fbId}/picture?type=square&height=128&width=128

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-21
        • 1970-01-01
        • 2013-10-25
        • 1970-01-01
        • 2012-01-10
        • 2020-12-04
        • 2020-07-23
        相关资源
        最近更新 更多