2013-02-25 101 views
0

像我有下面的代碼:WriteableBitmap的總是呈現黑色背景

var bmp = new WriteableBitmap((int)size.Width, (int)size.Height); 
    bmp.Render(new Canvas(){Background = new SolidColorBrush(Colors.White)}, null); 
    bmp.Invalidate(); 
return bmp; 

我怎麼得到的顏色:

var backColor = Application.Current.Resources["PhoneBackgroundColor"].ToString(); 
var foreColor = Application.Current.Resources["PhoneForegroundColor"].ToString(); 

我需要呈現的圖像白色背景。但是這個代碼總是渲染黑色的圖像。前景是好的,我已經測試下一個結構:

Canvas 
    Textblock - with black foreground 
    Textblock - with black foreground 

那麼問題在哪裏?

回答

0

您需要指定畫布的大小,否則它的默認寬度和高度爲0,導致透明的WriteableBitmap。

bmp.Render(new Canvas() { Background = new SolidColorBrush(Colors.White), Width=(int)size.Height, Height=(int)size.Height }, null); 
+0

如果是這樣,我的代碼將無法正常工作。 但它適用於黑暗的手機主題,不適用於光線。 var backColor = Application.Current.Resources [「PhoneBackgroundColor」]。ToString(); var foreColor = Application.Current.Resources [「PhoneForegroundColor」]。ToString(); 這是我如何獲得顏色。 – 2013-02-25 13:10:43

+0

這是另一個問題。您應該根據當前用戶主題(淺色或深色)更改畫布的背景顏色。 – 2013-02-25 13:33:05

+0

看起來你不瞭解我。 當手機主題是黑暗的 - 應用程序將呈現具有黑色背景和白色前景的圖像。 當手機主題是輕的 - 應用程序將呈現黑色背景的空白圖像。代碼未更改,但結果不同 – 2013-02-25 13:39:56