0

注意我是 Wpf 中的新手> 我有一个项目可以使用 opencv 库 throw web cam 解码 qr 代码> 并且它运行成功

现在我想在新的 Wpf 项目中使用这个项目 > 添加新的 wpf 项目并参考 winFrom 应用程序 >

这是我打开winfrom的简单代码>

public void runnow(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CameraCapture.cameraCapture()); }

通过破坏给我这个例外>

'Emgu.CV.CvInvoke' 的类型初始化程序引发了异常。>

我能做些什么来解决这个问题

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();
    }}
4

1 回答 1

0

如果要在 WPF 中托管 WinForm,则需要使用宿主控件 System.Windows.Forms.Integration.WindowsFormsHost

WPF 提供了许多具有丰富功能集的控件。但是,有时您可能希望在 WPF 页面上使用 Windows 窗体控件。例如,您可能对现有的 Windows 窗体控件进行了大量投资,或者您可能拥有一个提供独特功能的 Windows 窗体控件。

示例代码

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);
}

检查 => http://msdn.microsoft.com/en-us/library/ms751761.aspx

于 2012-07-24T15:19:42.707 回答