Search code examples
c#wpfmvvmip-camera.net-standard-2.0

PictureBox.Handle in .Netstandard


I'm developing C# WPF MVVM application. I need to capture JPEG, and preview Live View. For that I'm using Hikvision ip camera.

Looking the source code I need a PictureBox.Handle to show the Live View. This is what I have in my class liblry 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.hPlayWnd is IntPtr, and I don't know what can I use in WPF to show the Live View.

I added System.Windows.Forms.dll in my class liblry to access PictureBox, but I get error :

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'

And it seems that I can't fix this.

Any examples/suggestions?

Thanks.


Solution

  • .NET Standard does not include WPF nor WinForms, so your library should target .NET Framework and/or .NET Core. Then you can just add a UseWindowsForms tag to your project file and all references will be correctly resolved. There is no need to add references manually.

    WPF controls do not have window handles, so you need to use a Windows Forms control (e.g. PictureBox). To add it into a WPF window, you need to wrap it into 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>