首页 > 其他分享 >大恒相机-Winform\WPF 视频流显示

大恒相机-Winform\WPF 视频流显示

时间:2023-03-21 21:23:24浏览次数:40  
标签:bmiHeader int 视频流 System pc using WPF Winform objBitmapInfo

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using ZyDebugPlatform.UI.Disorder.ViewModels;
using Image = System.Windows.Controls.Image;

namespace ZyDebugPlatform.UI.Disorder.DHCamera
{
public class BitMaps:IDisposable
{

    static private AlgorithmC.RealCallback realCallBack = null;
    static private AlgorithmC.CaptureCallback captureCallback = null;
    Action<bool> action1 = null;
    Action<BitmapImage, bool> updatecallback;
    CWin32Bitmaps.BITMAPINFO m_objBitmapInfo = new CWin32Bitmaps.BITMAPINFO();
    IntPtr m_pHDC = IntPtr.Zero;
    IntPtr m_pBitmapInfo = IntPtr.Zero;
    PictureBox pc;
    Image image;
    System.Drawing.Graphics m_objGC = null;
    int mwidht = 1000;
    int mheight = 1000;
    public void RealCallbackFun(IntPtr buffer, int width, int height)
    {
        action1?.Invoke(true);
        //检查图像是否改变并更新Buffer
        __UpdateBufferSize(width, height);
        byte[] byMonoBufferTmp = new byte[width * height * 3];
        Marshal.Copy(buffer, byMonoBufferTmp, 0, width * 3 * height);

        if (null != pc)
        {
            CWin32Bitmaps.SetStretchBltMode(m_pHDC, 3);
            CWin32Bitmaps.StretchDIBits(
                        m_pHDC,
                        0,
                        0,
                        (int)pc.Width,
                        (int)pc.Height,
                        0,
                        0,
                        width,
                        height,
                        byMonoBufferTmp,
                        m_pBitmapInfo,
                        0,
                        0x00CC0020);
        }


    }

    public void RealCallbackFun(IntPtr buffer, int width, int height,bool isfocuse)
    {
        //检查图像是否改变并更新Buffer
        __UpdateBufferSize(width, height);
        byte[] byMonoBufferTmp = new byte[width * height * 3];
        Marshal.Copy(buffer, byMonoBufferTmp, 0, width * 3 * height);

        if (null != pc)
        {
            CWin32Bitmaps.SetStretchBltMode(m_pHDC, 3);
            CWin32Bitmaps.StretchDIBits(
                        m_pHDC,
                        0,
                        0,
                        (int)pc.Width,
                        (int)pc.Height,
                        0,
                        0,
                        width,
                        height,
                        byMonoBufferTmp,
                        m_pBitmapInfo,
                        0,
                        0x00CC0020);
        }


    }
    public void ImageCallback(IntPtr buffer, int width, int height)
    {
        var CameraData = new Bitmap(width, height, width * 3, PixelFormat.Format24bppRgb, buffer);
        BitmapImage objdataImage = BitmapToBitmapImage(CameraData);
        //image.Dispatcher.BeginInvoke(new Action(()=> { image.Source = objdataImage; }));
        updatecallback?.Invoke(objdataImage,false);
    }
    /// <summary>
    /// 检查图像是否改变并更新Buffer
    /// </summary>
    /// <param name="objIBaseData">图像数据对象</param>
    private void __UpdateBufferSize(int nwidth, int nheight)
    {

        if (nwidth != mwidht || nheight != mheight)
        {
            //更新BitmapInfo
            m_objBitmapInfo.bmiHeader.biWidth = nwidth;
            m_objBitmapInfo.bmiHeader.biHeight = nheight;
            Marshal.StructureToPtr(m_objBitmapInfo, m_pBitmapInfo, false);
        }

    }

   

    public BitMaps(PictureBox _pc)
    {
        pc = _pc;
        bitmap();
        m_objGC = pc.CreateGraphics();
        m_pHDC = m_objGC.GetHdc();
        
        realCallBack = RealCallbackFun;
        //captureCallback = RealCallbackFun;
        AlgorithmC.StartRealPlay(realCallBack);
        //AlgorithmC.OpenStream(captureCallback);
    }

    public BitMaps(Action<BitmapImage,bool> _updatecallback) {
        realCallBack = ImageCallback;
        updatecallback = _updatecallback;
        AlgorithmC.StartRealPlay(realCallBack);
    }
    public BitMaps(PictureBox _pc, Action<bool> _action)
    {
        pc = _pc;
        bitmap();
        m_objGC = pc.CreateGraphics();
        m_pHDC = m_objGC.GetHdc();
        realCallBack = RealCallbackFun;
        action1 = _action;
        AlgorithmC.StartRealPlay(realCallBack);
    }

    public BitMaps(Image _image, Action<bool> _action)
    {
        image = _image;
        action1 = _action;
        realCallBack = ImageCallback;
        AlgorithmC.StartRealPlay(realCallBack);
    }
    void bitmap()
    {
        m_objBitmapInfo.bmiHeader.biSize = (uint)Marshal.SizeOf(typeof(CWin32Bitmaps.BITMAPINFOHEADER));
        m_objBitmapInfo.bmiHeader.biWidth = 648;
        m_objBitmapInfo.bmiHeader.biHeight = 1800;
        m_objBitmapInfo.bmiHeader.biPlanes = 1;
        m_objBitmapInfo.bmiHeader.biBitCount = 24;
        m_objBitmapInfo.bmiHeader.biCompression = 0;
        m_objBitmapInfo.bmiHeader.biSizeImage = 0;
        m_objBitmapInfo.bmiHeader.biXPelsPerMeter = 0;
        m_objBitmapInfo.bmiHeader.biYPelsPerMeter = 0;
        m_objBitmapInfo.bmiHeader.biClrUsed = 0;
        m_objBitmapInfo.bmiHeader.biClrImportant = 0;
        m_pBitmapInfo = Marshal.AllocHGlobal(2048);
        Marshal.StructureToPtr(m_objBitmapInfo, m_pBitmapInfo, false);
    }
    private BitmapImage BitmapToBitmapImage(Bitmap bitmap)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Save(stream, ImageFormat.Png);
            stream.Position = 0;
            BitmapImage result = new BitmapImage();
            result.BeginInit();
            result.CacheOption = BitmapCacheOption.OnLoad;
            result.StreamSource = stream;
            result.EndInit();
            result.Freeze();
            return result;
        }
    }
    public void Dispose()
    {
        pc = null;
        m_pHDC = IntPtr.Zero;
        m_pBitmapInfo = IntPtr.Zero;
    }
}

}

标签:bmiHeader,int,视频流,System,pc,using,WPF,Winform,objBitmapInfo
From: https://www.cnblogs.com/kafeibuhuizui/p/17241501.html

相关文章

  • WPF加载网页与交互
     参考资料:https://www.jianshu.com/p/039dc834b2b9;https://zhuanlan.zhihu.com/p/102688922方法1:使用【WebBrowser】,能加载大部分网页  xmlns:wf="clr-namespace......
  • wpf实现FFmpeg获取摄像头实时画面
    gitee地址如下​如何获取摄像头验证码和ip首先获取摄像头底部的验证码及ip(测试使用的是萤石摄像头,需要PC下载萤石客户端查看ip)未连接之前可以通过VLC进行测试在左......
  • WPF命令绑定汇总
    1、Button等自带Command属性的控件,直接绑定命令<ButtonHorizontalAlignment="Left"Width="105"Height="32"Command="{BindingClickCommand}"><TextBlockText=......
  • wpf不在主线程的话放到主线程执行
    ///<summary>///刷新页面状态///</summary>///<paramname="overViewState"></param>///<paramname="strValue"></param>......
  • wpf自定义行为库(一)
    起因:我有个相对简单的WPF上位机程序,逻辑还是事件驱动那一套,虽然写的时候方便,但是代码的复用性太差了,并且各个模块的耦合度也很高,修改难度较大,于是我萌生了一个想法,将其改造......
  • Winform-ToolStripButton实现高清红黄绿指示灯(改善图片造成的模糊)
     //自定义控件(ToolStripButton)publicpartialclassRoundButton:ToolStripButton{protectedoverridevoidOnPaint(PaintEventArgspevent)......
  • 使用双缓冲技术解决winform窗体控件卡顿(dataGridView加载数据缓慢)
    背景:在做C/S项目中,做好的窗体出现了页面加载控件缓慢,放大、缩小窗体之后,窗体加载卡顿,以及数据渲染缓慢。可以利用双缓冲技术去解决这个问题。那么什么是双缓冲?百度介绍:我们......
  • winform绘图与前端canvas绘图效率对比
    先说结论:前端canas的绘图效率更高。因为项目使用winform的缘故,最近要实现一些波形展示的功能。涉及到绘制,肯定离不开GDI+的内容,但是还有替代的方案吗?当然是有的,可双用Web......
  • .NET中的winform、wpf、winui和maui你都知道吗?
    前言    年初.NET工程师的求职者反馈不好找工作,尤其是B/S开发,C/S开发稍微好点。这种情况下有好多小伙伴都想转行了,于是了解了一下JAVA,比.NET还卷,还是走.NET内部转行吧......
  • 能快速构建和定制网络拓扑图的WPF开源项目-NodeNetwork
    大家好,我是沙漠尽头的狼,今天介绍一个WPF开源项目-NodeNetwork,它可以帮助我们快速构建和定制网络拓扑图。一、前言在现代软件开发中,数据可视化和可交互性越来越受到关注。......