2011-11-03 27 views
1

我有一個簡單的頁面,有一個全景和一個按鈕,必須更改全景的背景圖片。原本圖片是1200x800。 如果我使用資源的圖片,一切都很好:全景縮小從孤立存儲的背景圖片

Uri uri = new Uri("Resources/Panorama.png", UriKind.Relative); 
var bitmap2 = new BitmapImage(uri); 

// here from debugging: bitmap2.CreateOptions == DelayCreation, bitmap2.PixelWidth == 0 and bitmap2.PixelHeight == 0 

var lcBrush2 = new ImageBrush() { 
    Stretch = Stretch.Fill, 
    ImageSource = bitmap2 
}; 

panoMain.Background = lcBrush2; 

,但如果我從獨立存儲圖片:

var picStream = ...getting a stream of file....; 
BitmapImage bitmap = new BitmapImage(); 
bitmap.SetSource(picStream); 

// here from debugging: bitmap.PixelWidth == 1200 and bitmap.PixelHeight == 800 

var lcBrush = new ImageBrush() { 
    Stretch = Stretch.Fill, 
    ImageSource = bitmap 
}; 

panoMain.Background = lcBrush; 

然後將圖片收縮到480×800

我我做錯了嗎?或者它是來自MS的錯誤?

+0

你可以使用相同的畫筆加載代碼加載資源從文件?您可能需要將資源設置爲CopyToOutput的屬性 – AlSki

+0

我不明白你的意思。 – giacoder

+0

您可以使用bitmap = new BitmapImage(uri);在我想要的代碼 – AlSki

回答

1

看起來像this is a bug。從該線程一個解決方法:

一個解決方法我發現是設置一個「默認」的背景圖片中 XAML這是所希望的大小的。如果我這樣做,那麼在我的MainPage_Loaded事件中更新 Background屬性,新圖像顯示爲 與默認圖像大小相同。

還有另一個解決方法,在該線程的底部有代碼。

+0

此鏈接不再有效 –

+0

@FactorMystic - 在AppHub升級中,似乎之前的鏈接已被殺害。這是更新的鏈接http://social.msdn.microsoft.com/Forums/en-US/wpdevelop/thread/0743ee59-f407-4e8a-afc8-93569698a924/ – keyboardP