C#从picturebox中截取视频拍照并且通过接口发送照片

本文介绍如何在WPF应用中利用PictureBox控件实现视频流的实时拍照功能,并通过HTTP接口推送拍摄的照片。文章详细展示了通过P/Invoke调用Win32 API进行屏幕截图的具体步骤,同时给出了通过HTTP响应将图片发送到客户端的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF中使用winform控件picturebox,获取handle调用公司内部接口显示视频。

需求分两步,第一步:实现拍照功能;第二步:给别人提供接口将照片推送到页面中。

第一步实现:

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        [DllImport("user32.dll")]
        public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);

        [DllImport("gdi32.dll")]
        public static extern IntPtr DeleteObject(IntPtr hgdiobj);

        [DllImport("gdi32.dll")]
        public static extern int DeleteDC(IntPtr hdc);

   //对客户视频拍照
        public static Bitmap CutControlBitmap(IntPtr winCotPtr)
        {
            //1先获取控件的大小
            IntPtr hscrdc = GetWindowDC(winCotPtr);
            RECT rECT = new RECT();
            GetWindowRect(winCotPtr, ref rECT);
            IntPtr mapPtr = CreateCompatibleBitmap(hscrdc, rECT.Right - rECT.Left, rECT.Bottom - rECT.Top);
            IntPtr hmemdc = CreateCompatibleDC(hscrdc);
            SelectObject(hmemdc, mapPtr);
            PrintWindow(winCotPtr, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(mapPtr);
            DeleteObject(mapPtr);
            DeleteDC(hscrdc);
            DeleteDC(hmemdc);
     
            //bmp.Save("aabbcc.jpg");

            return bmp;
        }

 public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

以上内容参考了:https://blog.csdn.net/weixin_43542114/article/details/112918250?utm_medium=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-3.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-3.nonecas

 

第二步:http接口推送照片

多次尝试Response.OutputStream直接发送byte[]都以失败告终。改为以下方式:

 Bitmap imgByte = imageHelper.CutControlBitmap(MainWindow.mainWindowsInstance.intPtrDest);

                    using (hlc.Response.OutputStream)
                    {
                        hlc.Response.Headers["Pragma"] = "no-cache";
                        hlc.Response.Headers["Cache-Control"] = "no-cache";
                        hlc.Response.ContentType = "image/Jpeg";
                        imgByte.Save(hlc.Response.OutputStream, ImageFormat.Jpeg);
                        hlc.Response.OutputStream.Flush();
                        if (GlobalInfo.logManager != null)
                        {
                            GlobalInfo.logManager.Info(callback + "({'ret':'CustomerPhotos'})");
                        }
                    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值