1

I have 2 bitmaps (foreground and background) which I need to overlay on top of each other to form a new bitmap and use it as the ImageBrush for a button. The code would look something like this (Windows 8.1 store app):

WriteableBitmap foregroundBitmap = GetForegroundBitmap();
WriteableBitmap backgroundBitmap = GetBackgroundBitmap();
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = Overlay(foregroundBitmap, backgroundBitmap); 
Button button = new Button();
button.Background = imageBrush;

How can I implement the Overlay(...) method above?

I tried:

backgroundBitmap.Blit(
    new Rect(0, 0, backgroundBitmap.PixelWidth, backgroundBitmap.PixelHeight),
    foregroundBitmap,
    new Rect(0, 0, foregroundBitmap.PixelWidth, foregroundBitmap.PixelHeight),
    WriteableBitmapExtensions.BlendMode.None);

but it didn't work (with BlendedMode None or Additive).

Thanks.

chue x
  • 18,573
  • 7
  • 56
  • 70
alexbtr
  • 3,292
  • 2
  • 13
  • 25

1 Answers1

0

Try this

 <Window.Resources>
    <BitmapImage x:Key="image1" UriSource="DefaultImage.png"></BitmapImage>
    <BitmapImage x:Key="image2" UriSource="ImageRoation.png"></BitmapImage>
 </Window.Resources>

<Button Height="200" Width="200">
    <Grid Height="200" Width="200">
        <Grid.Background>
            <ImageBrush ImageSource="{StaticResource image1}" Stretch="Fill" ></ImageBrush>
        </Grid.Background>
        <Grid Margin="5" Width="50" Height="50"> 
            <Grid.Background>
                <ImageBrush ImageSource="{StaticResource image2}" Stretch="Fill"></ImageBrush>
            </Grid.Background>
        </Grid>
    </Grid>
</Button>
Heena
  • 8,450
  • 1
  • 22
  • 40
  • Thanks for the sample. I actually have about 50 various buttons like this loaded at runtime and need to do it in code rather than XAML. In any case, I found the problem in my code and now "blit" works. Thanks again. – alexbtr Jan 09 '14 at 22:21