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 937409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:28:27+00:00 2026-05-15T21:28:27+00:00

I’m trying something along these lines. //Somewhere in my app System.Uri resourceLocater = new

  • 0

I’m trying something along these lines.

//Somewhere in my app
 System.Uri resourceLocater = new System.Uri("/MyApp;component/Users.xaml", System.UriKind.Relative);
var obj = Application.LoadComponent(resourceLocater);

//Invoke the renderxaml element
var image=RenderXamlElement(obj as UserControl);

//Then I may save this image to a file..





//RenderXamlElement looks like this

byte[] RenderXamlElement(UserControl control)
            {
                Rect rect = new Rect(new Size(control.Width,control.Height));
                RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right,
                  (int)rect.Bottom, 96d, 96d, System.Windows.Media.PixelFormats.Default);
                rtb.Render(control);
                //endcode as PNG
                BitmapEncoder pngEncoder = new PngBitmapEncoder();
                pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

                //save to memory stream
                System.IO.MemoryStream ms = new System.IO.MemoryStream();

                pngEncoder.Save(ms);
                ms.Close();
                return ms.ToArray();
            } 

The usercontrol’s snapshot is not rendered properly. Any thoughts?

  • 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-15T21:28:27+00:00Added an answer on May 15, 2026 at 9:28 pm

    What you want to do is use the VisualBrush class built into WPF. This should provide the functionality I think you’re looking for.

    VisualBrush class on MSDN

    Cheers.

    EDIT: Expanded answer for expanded question

    This code snippet should put you in the right direction for saving the image to a file.

    Stefan Wick’s blog – Rendering ink and image to a bitmap using WPF

    // render InkCanvas' visual tree to the RenderTargetBitmap
    RenderTargetBitmap targetBitmap =
        new RenderTargetBitmap((int)inkCanvas1.ActualWidth,
                               (int)inkCanvas1.ActualHeight,
                               96d, 96d,
                               PixelFormats.Default);
    targetBitmap.Render(inkCanvas1);
    
    // add the RenderTargetBitmap to a Bitmapencoder
    BmpBitmapEncoder encoder = new BmpBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
     
    // save file to disk
    FileStream fs = File.Open(fileName, FileMode.OpenOrCreate);
    encoder.Save(fs);
    

    EDIT: More code to try and demonstrate possible solution

    The XAML:

    <Page  
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      x:Class="UsingVisualBrush.PaintingWithVisuals" >
    
        <StackPanel Name="MyStackPanel"  
                    Orientation="Horizontal" 
                    Margin="10" Background="White" 
                    HorizontalAlignment="Left"  >
          <Rectangle Name="myRect" Width="150" Height="150" 
                     Stroke="Black" Margin="0,0,5,0" Loaded="UserControl_Loaded"  >
          </Rectangle>
        </StackPanel>
    </Page>
    

    Now the code behind…

    namespace UsingVisualBrush
    {
        public partial class PaintingWithVisuals : Page
        {
            public PaintingWithVisuals() 
            {
    
            }
    
            private void UserControl_Loaded(object sender, RoutedEventArgs args)
            {
                Uri resourceLocater = new Uri("/component/Users.xaml", UriKind.Relative);
                var obj = Application.LoadComponent(resourceLocater);
    
                // Using UserControl-derived class
                var mylocatedControl = obj as UserControl;
                mylocatedControl.Background = Brushes.Blue;
                mylocatedControl.Width = 20;
                mylocatedControl.Height = 20;
    
                // Using UserControl-derived class
                var myControl = new UserControl();
                myControl.Background = Brushes.Blue;
                myControl.Width = 20;
                myControl.Height = 20;
    
                // using UIElement-derived class
                var myVisualBrush = new VisualBrush();
                var panel = new StackPanel { Background = Brushes.Transparent };
    //            panel.Children.Add(new TextBlock { Background = Brushes.Green, Foreground = Brushes.Black, FontSize = 10, Margin = new Thickness(10), Text = "Hello World from Dave" });
    //            panel.Children.Add(myControl);
                panel.Children.Add((UserControl)obj);
                myVisualBrush.Visual = panel;
                ((UserControl)obj).Background = Brushes.Blue;
                myRect.Fill = myVisualBrush;
            }
        }
    }
    

    I hope this gets you a bit closer…

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
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 new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.