2012-06-01 48 views

回答

2

比如說這樣?

BitmapImage bmpImage = new BitmapImage(new Uri("http://dantonybrown.com/brownsoft/SweepyCleaner.png")); 
    MessageBox.Show(bmpImage.PixelWidth.ToString()); 

這很有道理。圖像按需加載,並在背景上加載。你有多種選擇這裏:

  1. 分配的BitmapImage到圖像控制。在ImageLoaded發生的事件後,您可以訪問屬性:

    BitmapImage bmpImage = new BitmapImage(new Uri("http://dantonybrown.com/brownsoft/SweepyCleaner.png")); 
        bmpImage.ImageOpened += (sender, args) => Dispatcher.BeginInvoke(() => MessageBox.Show(
         bmpImage.PixelWidth.ToString(CultureInfo.InvariantCulture))); 
        imageCtrl.Source = bmpImage; 
    
  2. 裝入的BitmapImage與CreateOptions.None。這將仍然加載圖像中的背景,但你不必像分配到控制它開始加載之前:

    BitmapImage bmpImage = new BitmapImage(new Uri("http://dantonybrown.com/brownsoft/SweepyCleaner.png")) 
               {CreateOptions = BitmapCreateOptions.None}; 
        bmpImage.ImageOpened += (sender, args) => Dispatcher.BeginInvoke(() => MessageBox.Show(
         bmpImage.PixelWidth.ToString(CultureInfo.InvariantCulture)));