0
我需要將圖像(從Web服務獲取)轉換爲base64String以將它們存儲在SQLite數據庫中。從Web鏈接獲取圖像並將其轉換爲循環中的base64string
編輯:我使用ImageOpened,使圖像加載/下載之前,我將它們轉換成base64String但ImageOpened doest運行。我如何完成這項任務?我的意思是將圖像數組轉換爲base64。
這是我的代碼:
public async void LoadData()
{
//get Json
string str = await Helper.getJSON();
//Deserialize json
MainClass apiData = JsonConvert.DeserializeObject<MainClass>(str);
//the problem loop
for (int i = 0; i < apiData.Categories.Count;i++)
{
//apiData.Categoriesp[i].icon_image is the url of the image
BitmapImage image = new BitmapImage(new Uri(apiData.Categories[i].icon_image,UriKind.RelativeOrAbsolute));
//it never runs
image.ImageOpened += (s, e) =>
{
//code for conversion of image into memory stream
image.CreateOptions = BitmapCreateOptions.None;
WriteableBitmap wb = new WriteableBitmap(image);
MemoryStream ms = new MemoryStream();
wb.SaveJpeg(ms, image.PixelWidth, image.PixelHeight, 0, 100);
byte[] imageBytes = ms.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
//setting imageInSql property of the list (but it never runs)
apiData.Categories[i].imageInSql = base64String;
};
}
//Inserting into database
using(var db= new SQLiteConnection(App.dbPath)){
//geting null in imageinSql property.
db.InsertAll(apiData.Categories,typeof(Category));
}
}
在此先感謝。
@did您試圖設置image.CreateOptions = BitmapCreateOptions.None。 – Ramashankar
是的...不工作 –
然後你應該驗證圖像的網址。 – Ramashankar