Answered by:
How to get Handle for a Image Control in WPF

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
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
All replies
-
Hi,
You can try this:
HwndSource hwndSource = PresentationSource.FromVisual(YourImageControl) as 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 -
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
-
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 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 -
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
-
-
-
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