none
The calling thread must be STA, because many UI components require this.

    Question

  • Does it mean I cannot instantiate UI controls (like Image) in order to render them on a bitmap in web application?

    Jan
    Wednesday, June 11, 2008 11:00 AM

Answers

  • No, it just means that the calling thread must be STA.

    To render a WPF scene to a bitmap in a web app (e.g. using RenderTargetBitmap) create a new thread t and call t.SetApartmentState(ApartmentState.STA) before starting it. Then perform all the rendering on it.
    • Marked as answer by Jan Kučera Thursday, June 12, 2008 8:37 AM
    Wednesday, June 11, 2008 1:53 PM

All replies

  • No, it just means that the calling thread must be STA.

    To render a WPF scene to a bitmap in a web app (e.g. using RenderTargetBitmap) create a new thread t and call t.SetApartmentState(ApartmentState.STA) before starting it. Then perform all the rendering on it.
    • Marked as answer by Jan Kučera Thursday, June 12, 2008 8:37 AM
    Wednesday, June 11, 2008 1:53 PM
  • Another alternative is to use the System.Drawing (GDI+) libraries for image manipulation.
    Wednesday, June 11, 2008 1:57 PM
    Moderator
  • Thanks for the trick.  Though, I am not sure I am comfortable that creating any component will through an exception because many components has some requirements. Nevermind.

    As for the GDI+, I would say they have the same thread aparment requirements as WPF, and although I can do the image manipulation in GDI+, this is not problem in WPF. The problem is using UI controls, and these are not in Windows Forms.

    Thanks for help!
    Jan
    Thursday, June 12, 2008 8:41 AM
  • I tried the STA approach and it works good - for the first time.  Any subsequent calls to the procedure I get, "The calling Thread cannot access this object because a different thread owns it." on line

    BitmapSource image = xxxCanvas.GetBitmapImage(96, 400, 500);

    xxxCanvas is a WPF component that we do not have access to source code.  However when I create a windows app using this WPF component it generates me a good image.

    Any help is appreciated.

        [STAThread]
        public void RenderWBSImage()
        {
            try
            {
                Thread worker = new Thread(new ThreadStart(RenderWBSImageInternal));
                worker.SetApartmentState(ApartmentState.STA);
                worker.Name = "RenderWBSImage";
                worker.IsBackground = true;
                worker.Start();
                worker.Join();
            }
            catch (Exception exp)
            {
                Response.Write(exp.ToString());
            }
        }
    
    
        public void RenderWBSImageInternal()
        {
            try
            {
                ….. . . 
                BitmapSource image = xxxCanvas.GetBitmapImage(96, 400, 500);
                ….. . . 
    
                if (xxxCanvas.Dispatcher.Thread.IsAlive)
                    xxxCanvas.Dispatcher.InvokeShutdown();
    
            }
            catch (Exception exp)
            {
                ///ExceptionUtil.DisplayError(exp);
            }
    

    Friday, June 01, 2012 9:26 PM