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

  • SEARCH
  • Home
  • 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 704689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:58:10+00:00 2026-05-14T03:58:10+00:00

So I’m trying to use a WPF User Control to generate a ton of

  • 0

So I’m trying to use a WPF User Control to generate a ton of images from a dataset where each item in the dataset would produce an image…

I’m hoping I can set it up in such a way that I can use WPF databinding, and for each item in the dataset, create an instance of my user control, set the dependency property that corresponds to my data item, and then draw the user control to an image, but I’m having problems getting it all working (not sure whether databinding or drawing to the image is my problem)

Sorry for the massive code dump, but I’ve been trying to get this working for a couple of hours now, and WPF just doesn’t like me (have to learn at some point though…)

My User Control looks like this:

<UserControl x:Class="Bleargh.ImageTemplate"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:Bleargh"
    x:Name="ImageTemplateContainer"
    Height="300" Width="300">

    <Canvas>
        <TextBlock Canvas.Left="50" Canvas.Top="50"  Width="200" Height="25" FontSize="16" FontFamily="Calibri" Text="{Binding Path=Booking.Customer,   ElementName=ImageTemplateContainer}" />
        <TextBlock Canvas.Left="50" Canvas.Top="100" Width="200" Height="25" FontSize="16" FontFamily="Calibri" Text="{Binding Path=Booking.Location,   ElementName=ImageTemplateContainer}" />
        <TextBlock Canvas.Left="50" Canvas.Top="150" Width="200" Height="25" FontSize="16" FontFamily="Calibri" Text="{Binding Path=Booking.ItemNumber, ElementName=ImageTemplateContainer}" />
        <TextBlock Canvas.Left="50" Canvas.Top="200" Width="200" Height="25" FontSize="16" FontFamily="Calibri" Text="{Binding Path=Booking.Description,ElementName=ImageTemplateContainer}" />
    </Canvas>

</UserControl>

And I’ve added a dependency property of type "Booking" to my user control that I’m hoping will be the source for the databound values:

public partial class ImageTemplate : UserControl
{
    public static readonly DependencyProperty BookingProperty = DependencyProperty.Register("Booking", typeof(Booking), typeof(ImageTemplate));
    public Booking Booking
    {
        get { return (Booking)GetValue(BookingProperty); }
        set { SetValue(BookingProperty, value); }
    }

    public ImageTemplate()
    {
        InitializeComponent();
    }
}

And I’m using the following code to render the control:

List<Booking> bookings = Booking.GetSome();
for(int i = 0; i < bookings.Count; i++)
{
    ImageTemplate template = new ImageTemplate();
    template.Booking = bookings[i];

    RenderTargetBitmap bitmap = new RenderTargetBitmap(
        (int)template.Width,
        (int)template.Height,
        120.0,
        120.0,
        PixelFormats.Pbgra32);

    bitmap.Render(template);

    BitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmap));

    using (Stream s = File.OpenWrite(@"C:\Code\Bleargh\RawImages\" + i.ToString() + ".png"))
    {
        encoder.Save(s);
    }
}

EDIT:

I should add that the process works without any errors whatsoever, but I end up with a directory full of plain-white images, not text or anything… And I have confirmed using the debugger that my Booking objects are being filled with the proper data…

EDIT 2:

Did something I should have done a long time ago, set a background on my canvas, but that didn’t change the output image at all, so my problem is most definitely somehow to do with my drawing code (although there may be something wrong with my databinding too)

  • 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-14T03:58:11+00:00Added an answer on May 14, 2026 at 3:58 am

    RenderTargetBitmap renders the current state of your control. In your case your control has not initialized so it still appears white.

    To get your code to initialize properly before Render() you need to do three things:

    1. Make sure your control has been measured and arranged.
    2. If your control uses Loaded events, make sure you are attached to a PresentationSource.
    3. Make sure all DispatcherPriority.Render and above events have completed.

    If you do these three things your RenderTargetBitmap will come out identically to the way the control appears when you add it to a Window.

    Forcing a Measure/Arrange on your control

    This is as simple as:

    template.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    template.Arrange(new Rect(template.DesiredSize));
    

    This code forces measure/arrange. It is simplest to pass in double.PositiveInfinity for the width and height because it allows your UserControl to choose its own Width and Height. If you explicitly set the width/height it doesn’t matter much, but this way your UserControl has the option of using WPF’s layout system to automatically grow when necessary if the data is larger than expected. By the same token it is better to use template.DesiredSize for the Arrange rather than passing in a specific size.

    Attaching a PresentationSource

    This is only necessary if your control or elements within your control rely on the Loaded event.

    using(var source = new HwndSource(new HwndSourceParameters())
                           { RootVisual = template })
    {
      ...
    }
    

    When the HwndSource is created the visual tree of the template is notified that it has been “Loaded”. The “using” block makes sure the template is “Unloaded” at the end of the “using” statement (last closing curly brace). An alternative to a using() statement would be to use GC.KeepAlive:

    GC.KeepAlive(new HwndSource(...) { ... });
    

    Flushing the Dispatcher queue down to DispatcherPriority.Render

    Just use Dispatcher.Invoke:

    Dispatcher.Invoke(DispatcherPriority.Loaded, new Action(() => {}));
    

    This causes an empty action to be invoked after all Render and higher priority actions have completed. The Dispatcher.Invoke method processes the dispatcher queue until it is empty down to Loaded level (which is right below Render).

    The reason this is necessary is that many WPF UI components use the Dispatcher queue to delay processing until the control is ready to render. This significantly cuts down on unnecessary re-computation of visual properties during binding and other operations.

    Where to add this code

    Add all three of these steps after you set your data context (template.Booking = ...) and before you call RenderTargetBitmap.Render.

    Additional suggestions

    There is a much easier way to make your binding work. In code, just set the booking as a DataContext. This removes the need to use ElementName and the Booking property:

    foreach(var booking in Booking.GetSome())
    {
      var template = new ImageTemplate { DataContext = booking };
    
      ... code from above ...
      ... RenderTargetBitmap code ...
    }
    

    By using the DataContext, the TextBox binding is greatly simplified:

    <UserControl ...>
      <Canvas>
        <TextBlock ... Text="{Binding Customer}" />
        <TextBlock ... Text="{Binding Location}" />
        <TextBlock ... Text="{Binding ItemNumber}" />
        <TextBlock ... Text="{Binding Description}" />
    

    If you have a particular reason for using the Booking DependencyProperty you can still simplify your bindings by setting the DataContext at the <UserControl> level rather than using ElementName:

    <UserControl ...
      DataContext="{Binding Booking, RelativeSource={RelativeSource Self}}">
      <Canvas>
        <TextBlock ... Text="{Binding Customer}" />
    

    I would also recommend you use a StackPanel instead of a Canvas for this purpose, and you should also consider using a style to set the font, text size and spacing:

    <UserControl ...
      Width="300" Height="300">
    
      <UserControl.Resources>
        <Style TargetType="TextBlock">
          <Setter Property="FontSize" Value="16" />
          <Setter Property="FontFamily" Value="Calibri" />
          <Setter Property="Height" Value="25" />
          <Setter Property="Margin" Value="50 25 50 0" />
        </Style>
      </UserControl.Resources>
    
      <StackPanel>
        <TextBlock Text="{Binding Customer}" />
        <TextBlock Text="{Binding Location}" />
        <TextBlock Text="{Binding ItemNumber}" />
        <TextBlock Text="{Binding Description}" />
      </StackPanel>
    </UserControl>
    

    Note that all the layout is done by WPF’s layout given the UserControl size and the specified height and margin. Also note that the TextBlock only needs to specify the Text — everything else is handled by the style.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.