.Netstandard 中的 PictureBox.Handle

问题描述

我正在开发 C# WPF MVVM 应用程序。我需要捕获 JPEG,并预览实时视图。为此,我使用了 Hikvision 网络摄像头。

查看源代码,我需要一个 PictureBox.Handle显示实时视图。这是我在班级图书馆中拥有的netstandard2.0

public void LivePreview(System.Windows.Forms.PictureBox pictureBox1)
{
   // SiginIn the user
   SignInHik();
   // If m_lUserID  > 0 means that the user signed in successfully
   if (m_lUserID < 0)
     {
       iLastErr = CHCNetSDK.NET_DVR_GetLastError();
       // Failed to login and output the error code
       str = "Login Failed,error code= " + iLastErr;
    
       return;
     }
     else
     {
       // taking all chanels number
       dwAChanTotalNum = (uint)DeviceInfo.byChanNum;
       dwDChanTotalNum = (uint)DeviceInfo.byIPChanNum + 256 * (uint)DeviceInfo.byHighDChanNum;
     if (dwDChanTotalNum > 0)
     {
         InfoIPChannel();
     }
     else
     {
       for (int i = 0; i < dwAChanTotalNum; i++)
       {
          ListAnalogChannel(i + 1,1);
          iChannelNum[i] = i + (int)DeviceInfo.byStartChan;
       }
    
         //comboBoxView.SelectedItem = 1;
         // MessageBox.Show("This device has no IP channel!");
      }
    }
    
   if (m_bRecord)
   {
      iLastErr = CHCNetSDK.NET_DVR_GetLastError();
      str = "Please stop recording firstly!,error code= " + iLastErr; // Failed to login and output the error code
    
       return;
   }
    
   if (m_lRealHandle < 0)
   {
      CHCNetSDK.NET_DVR_PREVIEWINFO lpPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();
      //live view window
      lpPreviewInfo.hPlayWnd = pictureBox1.Handle; // HERE IS THE PROBLEM!!!
      //the device channel number
     lpPreviewInfo.lChannel = iChannelNum[(int)iSelIndex];
     //the device channel number
     lpPreviewInfo.lChannel = iChannelNum[(int)iSelIndex];
     //Stream type: 0-main stream,1-sub stream,2-stream 3,3-stream 4,and so on
     lpPreviewInfo.dwStreamType = 0;
     //Connection mode: 0- TCP mode,1- UDP mode,2- multicast mode,3- RTP mode,4-RTP/RTSP,5-RSTP/HTTP
     lpPreviewInfo.dwLinkMode = 0;
     //0- non-blocking access,1- blocking access
     lpPreviewInfo.bBlocked = true;
     //The maximum number of frames in the display buffer of the playback library
     lpPreviewInfo.dwdisplayBufNum = 15;
     //User data
    IntPtr pUser = IntPtr.Zero;
    
   m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID,ref lpPreviewInfo,RealData,pUser);
    
      if (m_lRealHandle < 0)
        {
          iLastErr = CHCNetSDK.NET_DVR_GetLastError();
          //Failed to start live view,and output the error code.
          str = "NET_DVR_RealPlay_V40 Failed,error code= " + iLastErr; 
          //DebugInfo(str);
          return;
        }
       else
       {
           //Preview is successful
           DebugInfo("NET_DVR_RealPlay_V40 succ!");
           btnPreview = "Stop View";
       }
    }
    else
    {
       //Stop live view 
       if (!CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle))
       {
           iLastErr = CHCNetSDK.NET_DVR_GetLastError();
           str = "NET_DVR_StopRealPlay Failed,error code= " + iLastErr;
           return;
      }
    
         m_lRealHandle = -1;
         // btnPreview.Text = "Live View";
         //pictureBox1.Invalidate();//刷新窗口 refresh the window
      }
          return;
   }

lpPreviewInfo.hPlayWndIntPtr,我不知道我可以在 WPF 中使用什么来显示实时视图。

我在类库中添加System.Windows.Forms.dll 以访问 PictureBox,但出现错误

错误 CS1705 程序集“System.Windows.Forms”,标识为“System.Windows.Forms,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”使用“System.ComponentModel.Primitives,Version=4.2.2.0” Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a' 其版本高于引用的程序集 'System.ComponentModel.Primitives',标识为 'System.ComponentModel.Primitives,Version=4.1.2.0,PublicKeyToken=b03f5f7f11d50a3a'

而且我似乎无法解决这个问题。

有什么例子/建议吗?

谢谢。

解决方法

.NET Standard 不包括 WPF 和 WinForms,因此您的库应面向 .NET Framework 和/或 .NET Core。然后您只需将 UseWindowsForms 标记添加到您的项目文件中,所有引用都将被正确解析。无需手动添加引用。

WPF 控件没有窗口句柄,因此您需要使用 Windows 窗体控件(例如 PictureBox)。要将其添加到 WPF 窗口中,您需要将其包装到 WindowsFormsHost 中:

<Window x:Class="HostingWfInWpfWithXaml.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
    <Grid>    
        <WindowsFormsHost>
            <forms:PictureBox x:Name="picturebox1"/>
        </WindowsFormsHost>    
    </Grid>
</Window>