2011-03-19 85 views
7

我可以成功加載下面的Bitmap,並將其顯示在視圖的Image控件中。Silverlight:將BitmapImage寫入WriteableBitmap

var bitmapImage = new BitmapImage 
          { 
           UriSource = 
            new Uri("../Images/Test.JPG", UriKind.Relative) 
          }; 

但是當我加入這行創建WriteableBitmap的出位圖,

var w = new WriteableBitmap(bitmapImage); 

我在上面的行得到一個運行時錯誤:「對象未設置爲實例一個東西。」

看來BitmapImage的創建延遲了,可能是?我應該如何解決這個問題?

更新:

我現在想這一點,但openImage似乎從未被擊中。 (即使沒有嘗試使其同步,它仍然失敗)這裏有什麼問題?

var image = new BitmapImage(); 
     image.ImageOpened += (sender, args) => resetEventBitmap.Set(); 
     image.ImageFailed += (o, eventArgs) => 
           { 
            resetEventBitmap.Set(); 
            throw eventArgs.ErrorException; 
           }; 
     image.CreateOptions = BitmapCreateOptions.IgnoreImageCache; 
     image.UriSource = uri; 

     resetEventBitmap.WaitOne(); 

謝謝,

回答

3
BitmapImage _classField; 

    void LoadImageFunction() 
    { 
     _classField = new BitmapImage(); 
     _classField.ImageOpened += new EventHandler<RoutedEventArgs>(bi_ImageOpened); 
     _classField.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(bi_ImageFailed); 
     //sorry.. totally forgot about order :) 
     _classField.UriSource = new Uri("../some/uri", UriKind.Relative); 
    } 

    void bi_ImageFailed(object sender, ExceptionRoutedEventArgs e) 
    { 
     //something has happend 
     throw e.ErrorException; 
    } 

    void bi_ImageOpened(object sender, RoutedEventArgs e) 
    { 
     //image is loaded.. now we can work with it.. 
     var w = new WriteableBitmap(_classField); 
    } 
+0

Ahhhhh它的異步。 :)非常感謝你的幫助! – Houman 2011-03-19 10:05:16

+0

嗨Ai_boy,不幸的是ImageOpened方法從來沒有命中。在URi被設置之前,我會重新調整要訂閱的事件。但即使這樣也行不通。你有什麼想法,爲什麼?它能爲您提供幫助嗎?請在主帖中查看我的更新。 – Houman 2011-03-19 14:26:50

+0

編輯了一個答案.. – 2011-03-19 14:41:40

6

參考: http://www.blog.ingenuitynow.net/Silverlight+Creating+A+WriteableBitmap+From+A+Uri+Source.aspx

基本上,位圖圖像具有依賴屬性 「CreateOptions」 其中,默認情況下,被設定爲 「DelayCreation」。這會導致位圖被延遲渲染,直到需要它爲止。因此,這會導致我們的「對象引用未設置爲對象的實例」錯誤。爲了解決這個問題,我們必須打破創建位圖出來的WriteableBitmap的構造函數,改變這個選項,然後將它放回在vb.net這看起來像:

Dim tmpUri As New Uri(yourpath.ToString) 
    Dim bmp As New BitmapImage 
    bmp.CreateOptions = BitmapCreateOptions.None 
    bmp.UriSource = tmpUri 
    Dim wb As New WriteableBitmap(bmp) 
1
img1 = new BitmapImage(new Uri("/PrjName;component/Images/image01.jpg", UriKind.RelativeOrAbsolute)); 
     img2 = new BitmapImage(new Uri("/PrjName;component/Images/image02.jpg", UriKind.RelativeOrAbsolute)); 
     img1.CreateOptions = BitmapCreateOptions.None; 
     img2.CreateOptions = BitmapCreateOptions.None; 
     img1.ImageOpened += new EventHandler<RoutedEventArgs>(img1_ImageOpened); 
     img2.ImageOpened += new EventHandler<RoutedEventArgs>(img2_ImageOpened); 


    void img2_ImageOpened(object sender, RoutedEventArgs e) 
    { 
     load2 = true; 
    } 

    void img1_ImageOpened(object sender, RoutedEventArgs e) 
    { 
     load1 = true; 
    } 

    private void LayoutRoot_Loaded(object sender, RoutedEventArgs e) 
    { 
     while (!load1 && !load2) 
     { } 
     WriteableBitmap x = new WriteableBitmap(img1); 
     WriteableBitmap y = new WriteableBitmap(img2); 
    } 

這應該工作。它爲我做了..!它使它變得複雜,但它就是這樣工作的!

相關問題