Reputation: 4302
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?
I am converting the string to image data using Convert.FromBase64String (string encoded) in c#.
Upvotes: 0
Views: 1823
Reputation: 408
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
Upvotes: 0
Reputation: 93
I suppose, if an error image (e.g. 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
Upvotes: 0
Reputation: 1579
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.
Upvotes: 1