none
How to get Handle for a Image Control in WPF RRS feed

  • Question

  •  I have to read an image from a camera dll function.
     the function takes  a handle of the UI element to display the images.
    I was able to get the image on the Windows picturebox by using its handle property.

    But when i try to do the same thing in WPF, i found problem to do so, as WPF has a single handle
    for all controls.
    if i pass that handle  it will display the image on the Window itself.
    but i want it on Image box.
    so my question is how can i assign the WPF handle i have to the Image box or how can i get the
    handle of the Image box.
    I am aware that controls dont have handles in WPF.
    but is it not possible to get the handle using interop?

    please help me its really urgent.
    nishant singh,Bangalore
    Saturday, October 4, 2008 1:02 PM

Answers

  • There are no window handles in WPF, that's why you can't get them. The solution above will just display the picture on the entire window. One way you could achieve this would be with windows forms interop. Try placing a WindowsFormsHost in your WPF window and getting the handle from that (it has a Handle property).
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    • Marked as answer by Marco Zhou Friday, October 10, 2008 10:39 AM
    Sunday, October 5, 2008 9:22 PM

All replies

  • Hi,

    You can try this:

    HwndSource hwndSource = PresentationSource.FromVisual(YourImageControlas HwndSource;  
     
    if (hwndSource != null)  
    {  
        handle = hwndSource.Handle;  



    Let me know if it works !


    HTH


    Thomas
    Thomas Lebrun [MVP] - WPF/LINQ: http://blog.developpeur.org/tom - http://morpheus.developpez.com
    Saturday, October 4, 2008 3:58 PM
  • There are no window handles in WPF, that's why you can't get them. The solution above will just display the picture on the entire window. One way you could achieve this would be with windows forms interop. Try placing a WindowsFormsHost in your WPF window and getting the handle from that (it has a Handle property).
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    • Marked as answer by Marco Zhou Friday, October 10, 2008 10:39 AM
    Sunday, October 5, 2008 9:22 PM
  • I recently had a simular problem... I was using a 3rd party Video CoDec that required that it displayed as a child of another window, and it wanted it's own HWnd.

    What I did to solve this was to make a user control that was not much more than a rectangle of where I wanted the video to display.  It also kept a WindowHandle as a property so I could communicate with the HWnd via interop.

    When it was time to create the window, (via interop) I would pass the handle to my WPF window to a create function that would create the new window and make it a child of the WPF window's HWnd.  I also passed it the relative offset of the user control from the left-top corner of the window, and the size of the control's rectangle.

    You then have to insure (which is a good reason for a user control here) that as the user control is moved or resized that you update the non-WPF image window.

    While this seems a bit crazy, this worked very well for me.  The window that displayed the image was even embedded in an expander, and would show and hide properly.

    Hope this helps,
    Jim
    Monday, October 6, 2008 2:21 AM
  • Hi Thomas,

    I tried your approach. eventhough i am getting different values for the handle of Window and picturebox. the image is displayed onto the main window in both cases.
    any way i really appreciate your help.
    nishant singh,Bangalore
    Tuesday, October 7, 2008 1:34 PM
  • Hi Tim,

    Thanks for your valuable suggestion. i tried this and found that i am able to load the image on it.
    but if i add a selection control like rectangle, over it. it will not be visible as it becomes overlapped by the WinFormHost.


    nishant singh,Bangalore
    Tuesday, October 7, 2008 1:37 PM
  • I recently had a simular problem... I was using a 3rd party Video CoDec that required that it displayed as a child of another window, and it wanted it's own HWnd.

    What I did to solve this was to make a user control that was not much more than a rectangle of where I wanted the video to display.  It also kept a WindowHandle as a property so I could communicate with the HWnd via interop.

    When it was time to create the window, (via interop) I would pass the handle to my WPF window to a create function that would create the new window and make it a child of the WPF window's HWnd.  I also passed it the relative offset of the user control from the left-top corner of the window, and the size of the control's rectangle.

    You then have to insure (which is a good reason for a user control here) that as the user control is moved or resized that you update the non-WPF image window.

    While this seems a bit crazy, this worked very well for me.  The window that displayed the image was even embedded in an expander, and would show and hide properly.

    Hope this helps,
    Jim

     

    could you please to share some code with me? I don't understand what you said totally, or maybe I misunderstand

    Friday, August 13, 2010 7:18 PM
  • Hello Thomas

    Your suggestion worked for me but, the Image control is occupying the whole window overlapping other controls.

    Is there any work around to restrict this?

    Thanks in Advance.

    Regards

    Kamalakar Devaki

    Saturday, May 21, 2011 9:51 AM
  • Can you give me some code about the solution of above!  please send the mail to xeonfeng@hotmail.com. Thanks a lot.

    Thursday, March 28, 2013 9:11 AM
  • I recently had a simular problem... I was using a 3rd party Video CoDec that required that it displayed as a child of another window, and it wanted it's own HWnd.

    What I did to solve this was to make a user control that was not much more than a rectangle of where I wanted the video to display.  It also kept a WindowHandle as a property so I could communicate with the HWnd via interop.

    When it was time to create the window, (via interop) I would pass the handle to my WPF window to a create function that would create the new window and make it a child of the WPF window's HWnd.  I also passed it the relative offset of the user control from the left-top corner of the window, and the size of the control's rectangle.

    You then have to insure (which is a good reason for a user control here) that as the user control is moved or resized that you update the non-WPF image window.

    While this seems a bit crazy, this worked very well for me.  The window that displayed the image was even embedded in an expander, and would show and hide properly.

    Hope this helps,
    Jim

    Hi Jim,

    Now I am facing the trouble that you're done.

    But i am not clearly your answer so much.

    Could you please tell me more detail?

    Thanks & regards,

    Thuyet


    • Edited by thuyetvp Saturday, April 25, 2015 2:52 AM
    Saturday, April 25, 2015 2:51 AM