2014-01-10 59 views
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)); 
     } 
    } 

在此先感謝。

+0

@did您試圖設置image.CreateOptions = BitmapCreateOptions.None。 – Ramashankar

+0

是的...不工作 –

+0

然後你應該驗證圖像的網址。 – Ramashankar

回答

0

對我來說,它看起來像你在事件處理程序中設置CreateOptions屬性。

image.ImageOpened += (s, e) => 
{ 
    //code for conversion of image into memory stream 
    image.CreateOptions = BitmapCreateOptions.None; 

你應該這樣做喜歡 -

image.CreateOptions = BitmapCreateOptions.None; 
image.ImageOpened += (s, e) => 
{ 

試試下面的代碼。

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

     image.CreateOptions = BitmapCreateOptions.None 
    //it never runs 
     image.ImageOpened += (s, e) => 
     { 
     //code for conversion of image into memory stream 

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

是的,我嘗試把BitmapCreateOption外部事件...沒有工作 –

相關問題