Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 580375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:27:58+00:00 2026-05-13T14:27:58+00:00

I’m currently trying to persist a canvas to a bitmap, and I’ve run into

  • 0

I’m currently trying to persist a canvas to a bitmap, and I’ve run into some very peculiar behavior. The source code for the 3 following cases appears at the end of the post.

Case 1: A red rectangle appears in the output file (test.png), as expected.

Case 2: No red rectangle appears in the output file.

Case 3: No red rectangle appears in the output file.

It seems that adding the rectangle to the canvas (even though that canvas is never used to render the rectangle to disk) is necessary. It also seems that the button click must initiate the drawing – that it can’t occur in the Window constructor. Neither of these makes sense to me, and I assume I’m misunderstanding something.

Also, I apologize in advance for the bad code formatting. I wrestled with it for 20 minutes, but now I’m giving up.

Thanks in advance,

— Breck Fresen

The XAML used for all 3 cases:

<Window x:Class="ScanOutlineCreator.Window1"   
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    Title="Window1" Height="250" Width="250">  
        <Grid>  
            <Grid.RowDefinitions>  
                <RowDefinition Height="20" />  
                <RowDefinition />  
            </Grid.RowDefinitions>  
            <Button x:Name="btnGo" Grid.Row="0">  
                <TextBlock Text="Go" />  
            </Button>  
            <Canvas Grid.Row="1" x:Name="canvas" Width="200" Height="500"></Canvas>  
        </Grid>  
</Window>

Case 1:

using System.IO;  
using System.Windows;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
using System.Windows.Shapes;  
using Common.Drawing;  
namespace ScanOutlineCreator  
{  
        public partial class Window1  
        {  
                static Rectangle r = new Rectangle { Width = 100, Height = 100, Fill = Brushes.Red };  
                public Window1()  
                {  
                        InitializeComponent();  
                        btnGo.Click += new RoutedEventHandler(btnGo_Click);  
                        canvas.Children.Add(r);  
                }  

                static void btnGo_Click(object sender, RoutedEventArgs e)  
                {  
                        using (Stream stm = File.Create("test.png"))  
                        {  
                                RenderTargetBitmap rtb = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32);  
                                rtb.Render(r);  
                                Util.RenderTargetBitmapToStream(rtb, stm);  
                        }  
                }  
        }  
}  

Case 2:

using System.IO;  
using System.Windows;   
using System.Windows.Media;   
using System.Windows.Media.Imaging;   
using System.Windows.Shapes;   
using Common.Drawing;

namespace ScanOutlineCreator     
{     
    public partial class Window1   
    {   
        static Rectangle r = new Rectangle { Width = 100, Height = 100, Fill = Brushes.Red };  
        public Window1()   
        {   
            InitializeComponent();   
            btnGo.Click += new RoutedEventHandler(btnGo_Click);   
        }  

        static void btnGo_Click(object sender, RoutedEventArgs e)   
        {   
            using (Stream stm = File.Create("test.png"))   
            {   
                RenderTargetBitmap rtb = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32);   
                rtb.Render(r);   
                Util.RenderTargetBitmapToStream(rtb, stm);   
            }   
        }   
    }   
}   

Case 3:

using System.IO;   
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
using System.Windows.Shapes;  
using Common.Drawing;  
namespace ScanOutlineCreator  
{  
    public partial class Window1  
    {  
        static Rectangle r = new Rectangle { Width = 100, Height = 100, Fill = Brushes.Red };  
        public Window1()
        {  
            InitializeComponent();  
            canvas.Children.Add(r);  
            using (Stream stm = File.Create("test.png"))  
            {  
                RenderTargetBitmap rtb = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32);  
                rtb.Render(r);  
                Util.RenderTargetBitmapToStream(rtb, stm);  
            }  
        }  
    } 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:27:58+00:00Added an answer on May 13, 2026 at 2:27 pm

    This is almost certainly to do with layout. If so its been covered many times before.

    After you add the children you have to make sure Layout gets performed before you render, so things are actually in the visual tree in the right location etc.

    You probably need to call some combination of Measure/Arrange before rendering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.