2011-01-22 79 views
1

我想加載一張圖片,該圖片是根據(英國)郵政編碼根據Google靜態地圖提取的。將圖像加載到不同線程上的PictureBox上

可以說我有一個客戶,客戶有一個地址。客戶端的一個屬性是PostCode。我有一個加載客戶端的表單。我將客戶端ID提供給此表單的構造函數,然後使用LINQ 2 SQL加載包括地址在內的各種信息。

private void LoadBranchDetails() { 
    Text_Update_BI_Name.Text = Branch.BranchNumber; 
    Text_Update_BI_Manager.Text = String.Format("{0} {1}", Branch.PharmacyManager.FirstName, Branch.PharmacyManager.LastName); 
    DropDownList_Update_BI_Coordinator.SelectedValue = Branch.CoordinatorID; 
    DropDownList_Update_BI_ComputerSystem.SelectedValue = Branch.ComputerSystemID; 
    Text_Update_BI_Phone.Text = Branch.PhoneNumber; 
    Text_Update_BI_Fax.Text = Branch.FaxNumber; 

    Address BranchAddress = Branch.Contact.Addresses.FirstOrDefault(); 
    Text_Update_AI_House.Text = BranchAddress.HouseNumber; 
    Text_Update_AI_Street.Text = BranchAddress.Street; 
    Text_Update_AI_Area.Text = BranchAddress.Area; 
    Text_Update_AI_Post.Text = BranchAddress.PostCode; 
    DropDownList_Update_AI_City.SelectedValue = BranchAddress.City.OID; 

    MaskedText_Update_OI_NoPharmacist.Value = Branch.NumberOfPharmacists; 
    MaskedText_Update_OI_NoDispensers.Value = Branch.NumberOfDispensers; 
    MaskedText_Update_OI_NoMonFri.Value = Branch.NumberOfItemsMondayToFriday; 
    MaskedText_Update_OI_NoSat.Value = Branch.NumberOfItemsSaturday; 
    MaskedText_Update_OI_NoSun.Value = Branch.NumberOfItemsSunday; 
    MaskedText_Update_OI_NoAddicts.Value = Branch.NumberOfAddicts; 
    MaskedText_Update_OI_NoSupervised.Value = Branch.Supervised; 
    MaskedText_Update_OI_NoUnsupervised.Value = Branch.Unsupervised; 

    Check_Update_OI_ConfRoom.Checked = Branch.ConsultationRoom; 

    try {   
    PictureGoogleMaps.Image = GoogleAddressInfo.FetchMapInfo(Text_Update_AI_Post.Text).GoogleStaticMap; 

    } catch (Exception) { 
    PictureGoogleMaps.Image = Resources.DefaultGoogleMap; 

    } 
} 

加載圖像到PictureGoogleMaps線導致UI掛起的調用的時候,「.GoogleStaticMap」屬性生成谷歌靜態圖片。

在搜索互聯網,我發現這是很有幫助的例子:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     // Declare a list of URLs and their respective picture boxes 
     var items = new Dictionary<string, PictureBox> 
     { 
      { "http://www.google.com/logos/spring09.gif", new PictureBox() { Top = 0, Width = 300, Height = 80 } }, 
      { "http://www.google.com/logos/stpatricks_d4gwinner_eo09.gif", new PictureBox() { Top = 100, Width = 300, Height = 80 } }, 
      { "http://www.google.com/logos/schiaparelli09.gif", new PictureBox() { Top = 200, Width = 300, Height = 80 } }, 
      { "http://www.google.com/logos/drseuss09.gif", new PictureBox() { Top = 300, Width = 300, Height = 80 } }, 
      { "http://www.google.com/logos/valentines09.gif", new PictureBox() { Top = 400, Width = 300, Height = 80 } }, 
      { "http://www.google.com/logos/unix1234567890.gif", new PictureBox() { Top = 500, Width = 300, Height = 80 } }, 
      { "http://www.google.com/logos/charlesdarwin_09.gif", new PictureBox() { Top = 600, Width = 300, Height = 80 } }, 
     }; 

     foreach (var item in items) 
     { 
      var worker = new BackgroundWorker(); 
      worker.DoWork += (o, e) => 
      { 
       // This function will be run on a background thread 
       // spawned from the thread pool. 
       using (var client = new WebClient()) 
       { 
        var pair = (KeyValuePair<string, PictureBox>)e.Argument; 
        e.Result = new KeyValuePair<PictureBox, byte[]>(pair.Value, client.DownloadData(pair.Key)); 
       } 
      }; 
      worker.RunWorkerCompleted += (o, e) => 
      { 
       // This function will be run on the main GUI thread 
       var pair = (KeyValuePair<PictureBox, byte[]>)e.Result; 
       using (var stream = new MemoryStream(pair.Value)) 
       { 
        pair.Key.Image = new Bitmap(stream); 
       } 
       Controls.Add(pair.Key); 
      }; 
      worker.RunWorkerAsync(item); 
     } 
    } 
} 

現在我只需要弄清楚如何刪除for循環,在我的情況下使用。有任何想法嗎?

示例代碼來自此link

謝謝。

回答

1
public partial class Form1 : Form 
    {   
     private BackgroundWorker imageLoader; 

     public Form1() 
     { 
      InitializeComponent(); 

      this.imageLoader = new BackgroundWorker(); 
      this.imageLoader.DoWork += HandleOnImageLoaderDoWork; 
      this.imageLoader.RunWorkerCompleted += HandleImageLoaderOnRunWorkerCompleted; 

      this.LoadUserDetails(1); 
     } 

     private void LoadUserDetails(Int32 userID) 
     { 
      this.imageLoader.RunWorkerAsync(userID.ToString()); 
      // get the user details 
      // populate the UI controls with the data.... 
     } 

     private void HandleImageLoaderOnRunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e) 
     { 
      this.pictureBox1.Image = (Image)e.Result; 
     } 

     private void HandleOnImageLoaderDoWork(Object sender, DoWorkEventArgs e) 
     { 
      // simulate a web request for an image; 
      Thread.Sleep(3000); 
      Image image = Image.FromFile(@"test.jpg"); 
      e.Result = image; 
     } 
    } 

此外,請確保您顯示一些UI通知,後臺操作正在進行中......類似於PictureBox中的初始圖像(loading.gif)。

+0

謝謝。這將很好地工作。另外,我注意到你發送了一個用戶id到RunWorkerAsync,如:this.imageLoader.RunWorkerAsync(userID.ToString());現在我想知道是否可以使用這種方法將數據本身與圖像一起加載。 – DoomerDGR8 2011-01-24 10:08:23

1

這難以除去foreach循環嗎?你只需要加載一張圖片,就可以刪除foreach循環,並將圖片的URL和目標圖片框傳遞給背景工作者。

+0

不,刪除ForEach很容易。我在循環的最後一行有點困惑:worker.RunWorkerAsync(item); – DoomerDGR8 2011-01-24 08:22:11