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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:11:24+00:00 2026-06-14T01:11:24+00:00

I have some method that converts System.Windows.Controls.Canvas Control to .png Image and save in

  • 0

I have some method that converts System.Windows.Controls.Canvas Control to .png Image and save in a directory. I Call this functions for some List <Canvas> and I get all the images in some directory. It is working fine but some times this function generates some of the images blank randomly also again calling this function generates all images perfectly. what could be reason behind this malfunctioning?
Below is the Function

  public bool GenerateICards(Canvas surface)
        {
            bool retVal = false;
            try
            {
                // Save current canvas transform
                Transform transform = surface.LayoutTransform;
                // reset current transform (in case it is scaled or rotated)
                surface.LayoutTransform = null;

                // Get the size of canvas
                Size size = new Size(surface.Width, surface.Height);

                // Measure and arrange the surface
                // VERY IMPORTANT
                surface.Measure(size);
                surface.Arrange(new Rect(size));

                // Create a render bitmap and push the surface to it
                RenderTargetBitmap renderBitmap =
                  new RenderTargetBitmap(
                    (int)size.Width,
                    (int)size.Height,
                    96d,
                    96d,
                    PixelFormats.Pbgra32);
                renderBitmap.Render(surface);

                // Create a file stream for saving image
                using (FileStream outStream = new FileStream(SessionHelper.Path.PrintImage, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    // Use png encoder for our data
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    // push the rendered bitmap to it
                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                    // save the data to the stream
                    encoder.Save(outStream);
                }
                // Restore previously saved layout
                surface.LayoutTransform = transform;
                // i++;
                retVal = true;
            }
            //}
            catch (Exception)
            {
                ErrorLbl = "Error occured while generating temporary images.";
            }
            return retVal;
        }
  • 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-06-14T01:11:25+00:00Added an answer on June 14, 2026 at 1:11 am

    Just replaced

    renderBitmap.Render(surface);

    line in the function(function in above mentioned question) with

     Rect bounds = VisualTreeHelper.GetDescendantBounds(surface);
                    DrawingVisual dv = new DrawingVisual();
                    using (DrawingContext ctx = dv.RenderOpen())
                    {
                        VisualBrush vb = new VisualBrush(surface);
                        ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
                    }
    
                    renderBitmap.Render(dv);
    

    and this solved my problem. For details check out: http://blogs.msdn.com/b/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx

    This link is provided by colinsmith(In the question’s comment part).

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

Sidebar

Related Questions

I have some logic in a method that operates on a specified type and
I have some code in method C that will get executed based upon the
I have a class with some method that depend by one parameter. What is
I have some XML that I want to parse using the lxml method in
I have an action method that depending on some conditions needs to return a
I have some class that I'm passing as a result of a service method,
I have a HtmlHelper extension method that I would like to apply some logic
I have an each method that is run on some user-submitted data. Sometimes it
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the
I have a Rails method that just returns a string, and for some reason,

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.