0

I have next code:

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

How I get colors:

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

I need to render image with white background. But this code always render image with black back. Foreground is ok, I've tested with next structure:

Canvas
   Textblock - with black foreground
   Textblock - with black foreground

So where is the issue?

Anton Shakalo
  • 437
  • 1
  • 6
  • 23

1 Answers1

0

You need to specify the size of the canvas, otherwise it has a default Width and Height of 0, resulting in a transparent WriteableBitmap.

bmp.Render(new Canvas() { Background = new SolidColorBrush(Colors.White), Width=(int)size.Height, Height=(int)size.Height }, null);
Olivier Payen
  • 15,198
  • 7
  • 41
  • 70
  • If so, my code would not work always. But it works on dark phone theme and don't work on light. var backColor = Application.Current.Resources["PhoneBackgroundColor"].ToString(); var foreColor = Application.Current.Resources["PhoneForegroundColor"].ToString(); it's a how I get colors. – Anton Shakalo Feb 25 '13 at 13:10
  • That's another problem. You should change the Background color of the Canvas based on the current user theme (light or dark). – Olivier Payen Feb 25 '13 at 13:33
  • Looks like you don't understand me. When phone theme is dark - app will render image with black background and white foreground. When phone theme is light - app will render blank image with black background. Code not changed, but results is different – Anton Shakalo Feb 25 '13 at 13:39
  • The phone theme can't change the WriteableBitmap background color automatically. I think that the rendered image background is transparent, that's why you when placed in a page you think it's based on the phone theme. Place it in a grid with a red background and tell me what color it is. – Olivier Payen Feb 25 '13 at 15:03
  • I create bitmaps with text for live tiles. When I create new tile, I always use new WriteableBitmap instance with current phone settings. I can even set phone theme to light, deploy app and result will be same. looks like it's a writeablebitmap issue or phone os issue. – Anton Shakalo Feb 25 '13 at 15:24
  • Ok, I didn't know it was for creating a live tile. Did you try to set the background canvas color to (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"] – Olivier Payen Feb 25 '13 at 15:39
  • I've tested again and found that background simple doesn't work. In black theme it's look like background is black and tile look pretty. So I need to set background and all should works – Anton Shakalo Feb 25 '13 at 17:51