Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
The goal: To render canvas1 to canvas2.
The problem: The code doesn't work.
The question: How to solve?
The code:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Canvas canvas1 = new Canvas();
        canvas1.Width = 300;
        canvas1.Height = 300;
        canvas1.Background = Brushes.Red;

        canvas1.UpdateLayout();

        Image image = new Image();
        RenderTargetBitmap bmp = new RenderTargetBitmap((int)canvas1.Width, (int)canvas1.Height, 0, 0, PixelFormats.Pbgra32);
        bmp.Render(canvas1);
        image.Source = bmp;

        Canvas canvas2 = new Canvas();
        canvas2.Width = 300;
        canvas2.Height = 300;
        canvas2.Background = Brushes.Black;
        canvas2.Children.Add(image);

        this.Content = canvas2;
    }
}
Posted
Updated 2-Jun-15 3:58am
v7
Comments
[no name] 2-Jun-15 11:07am    
What is it that you think "doesn't work" means?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900