簡體   English   中英

圖像的Base64解碼給出紅色問號

[英]Base64 decoding of a image gives a red question mark

我正在unity3d中使用base64字符串對Facebook縮略圖圖像數據進行編碼和解碼。 當用戶沒有個人資料圖片時,所得圖像為紅色問號。 有什么方法可以識別出正在發送的無效圖片,以便可以用我選擇的默認圖片替換它嗎?

當人沒有人時,通過解碼facebook個人資料pic獲得的結果

我正在使用C#中的Convert.FromBase64String(字符串編碼)將字符串轉換為圖像數據。

我假設您使用某些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";
...

不是很優雅,但至少易於實現。

我想,如果在不存在個人資料圖片的情況下可預測地返回錯誤圖像(例如“紅色問號”圖像),則可以簡單地測試這種情況並替換為您選擇的另一張圖像。 您可以選擇將紅色問號圖像存儲為資源,然后將其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/zh-CN/library/2xsy4hac.aspx
http://ozgur.ozcitak.com/snippets/2009/12/21/base64-encoding-an-image-with-csharp.html

以我的經驗,這是由於錯誤的請求。 在我的情況下,返回紅色問號的錯誤API查詢是/{fbId}/picture?g&type=square&height=128&width=128我解決了刪除“?g”的問題,現在的工作查詢是/{fbId}/picture?type=square&height=128&width=128

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM