简体   繁体   中英

Base64 decoding of a image gives a red question mark

I am encoding and decoding facebook thumbnail image data using base64 strings in unity3d. When the user does not have a profile picture, the resulting image is a red question mark. Is there any way to recognise that an invalid picture is being sent so that I can replace it with a default pic of my choosing?

当人没有人时,通过解码facebook个人资料pic获得的结果

I am converting the string to image data using Convert.FromBase64String (string encoded) in c#.

I assume that you use some API to retrieve the base64 encoded string from an URL?

In that case, you could just dump the retrieved string once to the console and then copy it into your source code and compare this to the string you get in the future. Of course, this will break if the facebook API you use decides to deliver a different icon, in which case you would have to dump the new "unknown user" thumbnail.

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";
...

Not very elegant, but at least easy to implement.

I suppose, if an error image (eg the "red question-mark" image) is predictably returned when no profile picture exists, you could simply test for that case and replace with another image of your choosing. You may choose to store the red question-mark image as a resource and then compare its Base64 string to the Base64 of each image returned from your requests.

In that case, the pivotal bit of code might look something like this (assuming you've already stored the image's Base64 string in a resource):

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);
}

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

In my experience it was due to a wrong request. In my case the wrong API query returning Red Questionmark was /{fbId}/picture?g&type=square&height=128&width=128 I solved removing "?g", the working query now is /{fbId}/picture?type=square&height=128&width=128

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