Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# WPF ImageBrush Delegate

Posted on 2009-12-17
7
Medium Priority
?
1,438 Views
Last Modified: 2013-12-17
Hi to all,

I'm currently coding in WPF and have a Rectangle that i need to update with a bitmap from another thread.

I'm getting the ImageBrush to contain the image but it can't get past the fill part. i think because of a threading issue. Any help would be great.

Here is what i have:


using (MemoryStream ms = new MemoryStream(ea.Frame.Buffer))
{
    Bitmap bmp = (Bitmap)System.Drawing.Image.FromStream(ms);
    ImageBrush ibrush = new	ImageBrush(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),	IntPtr.Zero, Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions()));
    backRect.Fill = ibrush;
}

Open in new window

0
Comment
Question by:drewbuckley
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 26070014
What is the error message that you get?

I remember from a previous WPF project that some objects have to be created on the main thread, then passed on to the background thread to work with them, and then can be used by the main thread without problems.  If the objects were created by the background thread, the objects could not be used by the main thread.  I don't remember exactly which type of objects it was, but I think it were bitmaps.
0
 

Author Comment

by:drewbuckley
ID: 26070479
Thanks for your response,

This is the error.

System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.Threading.DispatcherObject.VerifyAccess()
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at System.Windows.Shapes.Shape.set_Fill(Brush value)
   at iLAUNCHVUE_WPF_V2.Window1.FrameVideoReceived(Object sender, FrameReceivedEventArgs ea)

The rectangle was created on the main thread. I've used similar code in normal C# and used a delegate to handle this for a picturebox but i can't seem to do that in WPF.

Any ideas?
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 26070629
You are probably modifying your UI object backRect from the background thread.  In that case you should modify your code to do that only on the main thread.  You can use the Dispatcher for that.

Something like this:

backRect.Dispatcher.BeginInvoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                    (Action<ImageBrush>)delegate(ImageBrush theBrush)
                    {
                        backRect.Fill = theBrush;
                    }, ibrush);

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 27

Expert Comment

by:VBRocks
ID: 26072972
Add this code to the form containing the Rectangle:

private delegate void SetRectangleImageBrushDelegate(ImageBrush ibrush);

private void SetRectangleImageBrush(ImageBrush ibrush)
{
      if (backRect.Dispatcher.CheckAccess())
      {
            backRect.Fill = ibrush;
      }
      else
      {
            backRect.Dispatcher.Invoke(
                  new SetRectangleImageBrushDelegate
                                                                (SetRectangleImageBrush),
                  ibrush);
      }

}



'Then, call it like this:

    Bitmap bmp = (Bitmap)System.Drawing.Image.FromStream(ms);
    ImageBrush ibrush = new ImageBrush
             (System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),    
             IntPtr.Zero, Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions()));

    SetRectangleImageBrush(ibrush);



0
 

Author Comment

by:drewbuckley
ID: 26075061
Thanks for answers. I tried both and this is the error now:

System.InvalidOperationException: Cannot use a DependencyObject that belongs to a different thread than its parent Freezable

I had received this one with other things i tried before posting. I can fill with an image on creation but can't seem to do it again from this thread. I'm at a loss.
0
 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 1000 total points
ID: 26075316
Try creating the brush and maybe also the bitmap on the UI thread, instead of just moving the call to the Fill method to the UI thread.    That way, the brush (and bitmap) belong to the same thread.  

I haven't tested it, but the ImageBrush is a DependencyObject, so it makes sense that the error message refers to that object.

Hope this helps.
0
 

Author Comment

by:drewbuckley
ID: 26075820
Thanks joriszwaenepoel,

I created a function with an external bitmap and a timer that calls the function and it works. Would be cleaner to have it done in the callback thread but for now, i'm happy with that.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Integration Management Part 2

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question