PictureBox.Handle 在 .Netstandard 中

PictureBox.Handle in .Netstandard

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

查看源代码我需要 PictureBox.Handle 来显示实时视图。这就是我 class 库中的内容 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 中可以用什么来显示实时视图。

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

Error CS1705 Assembly 'System.Windows.Forms' with identity 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' uses 'System.ComponentModel.Primitives, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.ComponentModel.Primitives' with identity 'System.ComponentModel.Primitives, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

看来我无法解决这个问题。

任何examples/suggestions?

谢谢。

.NET Standard 不包括 WPF 或 WinForms,因此您的库应该以 .NET Framework and/or .NET Core 为目标。然后你可以只添加一个 UseWindowsForms 标签到你的项目文件,所有的引用都会被正确解析。无需手动添加引用。

WPF 控件没有 window 句柄,因此您需要使用 Windows Forms 控件(例如 PictureBox)。要将其添加到 WPF window,您需要将其包装到 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>