PDA

View Full Version : Windows Presentation Foundation (WPF) and IC control - Compatible?



Moe_adham
June 24, 2011, 12:00:53
Hello,

We have been developing our Visual Basic application using the new Windows Presentation Foundation (WPF) layer ontop of VB.NET. (http://msdn.microsoft.com/en-us/library/ms754130.aspx)

I was wondering if IC Imaging control's .NET libraries will be functional with this, or will we have to migrate back to "only" VB.NET

Does anyone have experience with IC+WPF? I cannot foresee any reason why it would not work, but there is some slight GUI changes in the WPF system.

Thank you

Stefan Geissler
June 24, 2011, 12:59:16
Hello,

you can use the WindowsFormsHost in order to use IC in WPF.

Moe_adham
June 27, 2011, 18:04:17
Hello,

Do you think you could be more specific? The simple act of adding a reference to the IC 3.2 causes any WPF program to cease compiling.

Thank you,
M. Adham

Stefan Geissler
June 28, 2011, 10:25:52
Hello,

In WPF there is a special container for non WPF controls.

Add an instance of IC Imaging Control to your window class:

TIS.Imaging.ICImagingControl icImagingControl1 = new TIS.Imaging.ICImagingControl("Insert your
license key here");

On the WPF designer, insert a WindowsFormsHost element at the area where you want the live
display.
(In XAML, this is <WindowsFormsHost Name="windowsFormsHost1"/> ).

In the Window_Loaded event handler, set

windowsFormsHost1.Child = icImagingControl1;

You now have IC Imaging Control visible and ready to work in the WPF application.

Please note, IC 3.2 has no license key anymore.

Moe_adham
June 28, 2011, 12:01:35
Thank you for your reply. We manage to root-cause the issue to the fact that VB2010 was using an old DLL for TIS.imaging. For anyone working with WPF + .net on VB2010, here is the correct procedure to get it going:


Create new WPF project
add the following references:
-System.Windows.Forms
- WindowsFormsIntegration
Go to add references
- Browse: C:\Program Files\Common Files\IC Imaging Control 3.2\bin\v4.0.30319\x86\TIS.Imaging.ICImagingContro l32.dll
*** For some reason, VB2010 with WPF only picked up the older DLL (v2.0.50727) in the default .net references. This will not work.
Code:


XAML


<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WindowsFormsHost Name="windowsFormsHost1"/>
</Grid>
</Window>

Class:



Class MainWindow
Friend WithEvents icImagingControl1 As TIS.Imaging.ICImagingControl

Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
windowsFormsHost1.Child = icImagingControl1
icImagingControl1 = New TIS.Imaging.ICImagingControl
End Sub
End Class

Stefan Geissler
June 28, 2011, 15:23:00
Hi,

Thank you for posting the correct way. I only want to suggest

Class MainWindow
Friend WithEvents icImagingControl1 As TIS.Imaging.ICImagingControl

Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
icImagingControl1 = New TIS.Imaging.ICImagingControl
windowsFormsHost1.Child = icImagingControl1 ' Lines exchanged
End Sub
End Class

Moe_adham
July 8, 2011, 10:38:23
Thank you Stefan. One last question, we are always getting an Unhandled exception when exiting our WPF application when using the IC reference. Is there a disconnect or clean-up method that needs to be handled before application closing?

Thank you,
M. Adham

Stefan Geissler
July 8, 2011, 16:57:11
Hello,

I am not sure about this unhandled exception. But I suggest to stop the live video and close the device (.Device="") before deleting the IC Imaging Control instance.

Moe_adham
July 25, 2011, 10:37:32
In case anyone is looking for an easy way to display an image from an IC stream using WPF, it is not as straightforward as it is using a normal VB form:



Dim hBitmap As IntPtr
Dim destination As BitmapSource
Dim sizeOptions As BitmapSizeOptions = BitmapSizeOptions.FromEmptyOptions()
icImagingControl1.MemorySnapImage()
hBitmap = icImagingControl1.ImageActiveBuffer.Bitmap.GetHbit map()
destination = System.Windows.Interop.Imaging.CreateBitmapSourceF romHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions)
destination.Freeze()
img.Source = destination


and you can define the image in XAML:


<Image Name="img" />


Not sure if this is the most efficient method, but it was the only way I could get it to work.

Cheers,
Moe

Moe_adham
April 25, 2012, 14:56:39
In Case anyone else is using WPF, to ensure you do not have a Null Exeption error when closing your application, make sure that in the "closed" routine, you set your WindowsFormHost.Child = null. This was an error that took us some months to solve.

Example:


private void Closed(System.Object sender, System.EventArgs e)
{
icImagingControl1.LiveStop();
icImagingControl2.LiveStop();
icImagingControl1.Device = "";
icImagingControl2.Device = "";
icImagingControl1.Dispose();
icImagingControl2.Dispose();

fromHost.Child = null;
fromHost2.Child = null;
}

Stefan Geissler
April 25, 2012, 15:58:41
Hello,

Thank you very much for posting your solution. I really appreciate this.

RParker
August 2, 2012, 07:00:34
The information in this thread has been very useful and I have managed to display the output from from an ImagingSource DFK 31AU 03 camera.

I have discovered though that I can only display using the YUY2 video format at 7.5FPS and below, or the Y800 format at 30FPS, but not the BY8 format at any frame rate. When the program fails it closes apparently without any exception or device lost event.

The none of the CPU cores are taxed and your DemoApp can run BY8 at 30FPS and the YUY2 up to 7.5FPS, so it appears that the USB2 is not the limitation. Ditto the VB_Dialog_Template run in debug under VB2010.

I've attached the .xaml and .xaml.vb files as text but I'm new to this forum, so I don't know whether you'll see them.

Can you suggest how I might improve this code?

Stefan Geissler
August 2, 2012, 09:24:22
Hi

I have a suspect only: It may happen, that your image buffer does not get unlocked, because the wait in the frameTick() using 50 ms is longer than the frame rate's time interval of 33 ms at 30 fps. I would expect all buffers being locked at one point of time. Then more or or less randomly one buffer is unlocked, because your time calls the ShowImagebuffer() function, which unlocks an image buffer, so you can receive the next buffer. If there is only one image buffer usable, then the frame rate of the image display is only the half frame rate or less.

If you send me the complete project in a zip file, I will try to correct it.

RParker
August 3, 2012, 00:28:04
Thanks Stefan,

Attached is the complete project as used in my previous post. You will note that it references the TIS...dll located in the Debug directory to avoid the problem indicated by Moe_adham.

For your possible interest, we plan to display the outputs from two of these cameras on a single screen for a Science Museum exhibit that demonstrates the use of the Tan function in calculation of range. If you ever visit Questacon in Canberra, Australia, you may get to see the fruits of our combined effort.

For this application the frame rate is not too critical but reliability is. Any thing you can do to improve my code will be greatly appreciated and given the fantastic performance of these cameras, we are likely to use them in future exhibits.

Regards

Robert Parker

RParker
August 3, 2012, 07:06:58
As with most video streams, wpf will not overlay the camera video using xaml.

I tried quickly to see if I could grab images and use those (as I have previously done using the AForge library) but the documentation seemed to suggest that this was a poor alternative to using the IC imaging control overlay system.

Following the examples in the IC Imaging Control .NET Component User's Guide, I've got this working but I did have to make one change to the ShowBitmap function in order for a valid Graphics object to be found.

The attached file has the code which has been dropped into the vb solution attached to my previous post. The argument supplied to the ShowBitmap function would not generate a suitable Graphic object, so using the example in the GetGraphics help, I have used the icImagingControl1.OverlayBitmap object directly with positive results.

Do you have any insight on what has happened here?

Regards

Robert Parker

Stefan Geissler
August 3, 2012, 12:16:37
Hi Robert,

I corrected your project. The frameready timer, or what it was called is removed. The application now runs fine with high frame rates.

I also added the overlay bitmap, it is working now.

I wonder, why you use WPF, because it makes things more complicated than necessary. I suggest to create a simple Windows Forms application. IC Imaging Control offers easy to use wizards for this.

Unfortunately, Canberra, Australia is slightly out of my range at this point of time, therefore, I am not able to see the "fruits".