[英]open Windows Form with opcv From wpf windows
note i am new in Wpf > i have project that decode qr code by using opencv library throw web cam > and it running successfully 注意我是Wpf的新手>我有使用opencv库抛出网络摄像头解码qr代码的项目>并且它成功运行
now i wanna to using this project in new Wpf project > after adding new wpf project and make reference to to winFrom application > 现在我想在新的Wpf项目中使用此项目>添加新的wpf项目并参考winFrom应用程序之后>
and this my simple code to open winfrom > 这是我打开winfrom的简单代码>
public void runnow(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CameraCapture.cameraCapture()); }
by ruining give me this exception > 通过破坏给我这个例外>
The type initializer for 'Emgu.CV.CvInvoke' threw an exception.> “ Emgu.CV.CvInvoke”的类型初始值设定项引发了异常。
what can i do for solve this 我该怎么做才能解决这个问题
C# code C#代码
public partial class CameraCapture : Form
{
Capture capture;
bool Capturing;
Bitmap bimap;
private Reader reader;
private Hashtable hint;
libAES libEncryption = new libAES();
string Mykey = "";
public static String dataDecrypted="";
public CameraCapture()
{
InitializeComponent();
}
private void Mains(object sender, EventArgs arg) // Start function main to encode Qr code
{
Image<Bgr, Byte> image = capture.QueryFrame();
if (image != null)
{
bimap = image.ToBitmap();
pictureBox1.Image = bimap;
reader = new QRCodeReader();
hint = new Hashtable(); // Add some elements to the hash table. There are no duplicate keys, but some of the values are duplicates.
hint.Add(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
RGBLuminanceSource source = new RGBLuminanceSource(bimap, bimap.Width, bimap.Height); //This class is used to help decode images from files which arrive as RGB data from* Android bitmaps. It does not support cropping or rotation.
BinaryBitmap img = new BinaryBitmap(new GlobalHistogramBinarizer(source));
Result result = null;
try
{
result = reader.decode(img, hint);
dataDecrypted = libEncryption.Decrypt(result.Text, Mykey);
}
catch
{
dataDecrypted = "";
}
if (result == null)
{
label1.Text = " no decode";
}
else
{
label4.Text = result.Text;
label1.Text = dataDecrypted;
capture.Dispose();
}
}
} // end function Main
private void btnStart_Click(object sender, EventArgs e)
{
if (capture == null)
{
try
{
capture = new Capture(); // **the exption thown here**
}
catch (NullReferenceException exception)
{
MessageBox.Show(exception.Message);
}
}
if (capture != null)
{
if (Capturing)
{
btnStart.Text = "Start Capture";
Application.Idle -= Mains;
}
else
{
btnStart.Text = "Stop Capture";
Application.Idle += Mains;
}
Capturing = !Capturing;
}
}
private void Release()
{
if (capture != null)
capture.Dispose();
}}
If you want to host WinForm in WPF, you need to use host control System.Windows.Forms.Integration.WindowsFormsHost 如果要在WPF中托管WinForm,则需要使用主机控件System.Windows.Forms.Integration.WindowsFormsHost
WPF provides many controls with a rich feature set. WPF为许多控件提供了丰富的功能集。 However, you may sometimes want to use Windows Forms controls on your WPF pages. 但是,有时您可能想在WPF页面上使用Windows窗体控件。 For example, you may have a substantial investment in existing Windows Forms controls, or you may have a Windows Forms control that provides unique functionality. 例如,您可能对现有的Windows Forms控件进行了大量投资,或者您可能具有提供独特功能的Windows Forms控件。
Example code 范例程式码
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the MaskedTextBox control.
MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");
// Assign the MaskedTextBox control as the host control's child.
host.Child = mtbDate;
// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
}
Check => http://msdn.microsoft.com/en-us/library/ms751761.aspx 检查=> http://msdn.microsoft.com/en-us/library/ms751761.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.