C#摄像头实现拍照功能的简单代码示例

开发 后端
这里将介绍一个C#摄像头实现拍照功能的简单代码示例,代码虽然不短,但是基本上实现了相对应的功能,希望对大家有所帮助。

C#摄像头实现拍照功能的简单代码示例

using System;  
using System.Runtime.InteropServices;  
using System.Drawing;  
using System.Drawing.Imaging;  
namespace Video  
{  
///   
  • /// 一个C#摄像头控制类  
  • /// 
  •   public class VideoWork   {   private const int WM_USER = 0x400;   private const int WS_CHILD = 0x40000000;   private const int WS_VISIBLE = 0x10000000;   private const int WM_CAP_START = WM_USER;   private const int WM_CAP_STOP = WM_CAP_START + 68;   private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;   private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;   private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;   private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;   private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;   private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;   private const int WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;   private const int WM_CAP_SET_OVERLAY =WM_CAP_START+ 51;    private const int WM_CAP_SET_PREVIEW =WM_CAP_START+ 50;    private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;   private const int WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;   private const int WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;   private const int WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;   private const int WM_CAP_SET_SCALE=WM_CAP_START+ 53;   private const int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;    private IntPtr hWndC;   private bool bWorkStart = false;   private IntPtr mControlPtr;   private int mWidth;   private int mHeight;   private int mLeft;   private int mTop;     ///   
  • /// 初始化显示图像  
  • /// 
  •   /// 控件的句柄   /// 开始显示的左边距   /// 开始显示的上边距   /// 要显示的宽度   /// 要显示的长度   public VideoWork(IntPtr handle, int left, int top, int width,int height)   {   mControlPtr = handle;   mWidth = width;   mHeight = height;   mLeft = left;   mTop = top;   }   [DllImport("avicap32.dll")]    private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);   [DllImport("avicap32.dll")]   private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize );   [DllImport("User32.dll")]    private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);   ///   
  • /// 开始显示图像  
  • /// 
  •   public void Start()   {   if (bWorkStart)   return;   bWorkStart = true;   byte[] lpszName = new byte[100];   hWndC = capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE ,mLeft,mTop,mWidth,mHeight,mControlPtr,0);   if (hWndC.ToInt32() != 0)   {   SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);   SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);   SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);   SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);   SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);   SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);   SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);   SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);   //Global.log.Write("SendMessage");   }   return;   }   ///   
  • /// 停止显示  
  • /// 
  •   public void Stop()   {   SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);   bWorkStart = false;   }   ///   
  • /// 抓图  
  • /// 
  •   /// 要保存bmp文件的路径   public void GrabImage(string path)   {   IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);   SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64());   }   }   }       这是一个控制摄像头进行拍照的类,我每次使用GrabImage抓图都是225K的一张照片,我想请问如何才能让我抓到的图片小一些,我想控制在70K左右。不知怎么让拍照的像素变小?       if(this.Request.QueryString["filename"]!=null)   {                   //获取原图片   string filename=this.Request.QueryString["filename"];   Bitmap bmpOld=new Bitmap(this.Server.MapPath("images/" + filename));       //计算缩小比例   double d1;   if(bmpOld.Height>bmpOld.Width)   d1=(double)(MaxLength/(double)bmpOld.Width);   else  d1=(double)(MaxLength/(double)bmpOld.Height);   //产生缩图   Bitmap bmpThumb=new Bitmap(bmpOld,(int)(bmpOld.Width*d1),(int)(bmpOld.Height*d1));   //清除缓冲   Response.Clear();   //生成图片   bmpThumb.Save(this.Response.OutputStream,ImageFormat.Jpeg);   Response.End();   //释放资源   bmpThumb.Dispose();   bmpOld.Dispose();  
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
    • 28.
    • 29.
    • 30.
    • 31.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
    • 40.
    • 41.
    • 42.
    • 43.
    • 44.
    • 45.
    • 46.
    • 47.
    • 48.
    • 49.
    • 50.
    • 51.
    • 52.
    • 53.
    • 54.
    • 55.
    • 56.
    • 57.
    • 58.
    • 59.
    • 60.
    • 61.
    • 62.
    • 63.
    • 64.
    • 65.
    • 66.
    • 67.
    • 68.
    • 69.
    • 70.
    • 71.
    • 72.
    • 73.
    • 74.
    • 75.
    • 76.
    • 77.
    • 78.
    • 79.
    • 80.
    • 81.
    • 82.
    • 83.
    • 84.
    • 85.
    • 86.
    • 87.
    • 88.
    • 89.
    • 90.
    • 91.
    • 92.
    • 93.
    • 94.
    • 95.
    • 96.
    • 97.
    • 98.
    • 99.
    • 100.
    • 101.
    • 102.
    • 103.
    • 104.
    • 105.
    • 106.
    • 107.
    • 108.
    • 109.
    • 110.
    • 111.
    • 112.
    • 113.
    • 114.
    • 115.
    • 116.
    • 117.
    • 118.
    • 119.
    • 120.
    • 121.
    • 122.
    • 123.
    • 124.
    • 125.
    • 126.
    • 127.
    • 128.
    • 129.
    • 130.
    • 131.
    • 132.
    • 133.
    • 134.
    • 135.
    • 136.
    • 137.
    • 138.
    • 139.

    C#摄像头实现拍照功能的简单代码示例就介绍到这里。

    【编辑推荐】

    1. C#服务端与客户端连接实现浅析
    2. C#服务端与客户端连接实现浅谈
    3. C#服务端与客户端通信浅析
    4. C#服务端与客户端通信详解
    5. C#服务端程序实现同步传输字符串浅析
    责任编辑:彭凡 来源: 51CTO
    相关推荐

    2009-08-21 17:55:14

    C#获取摄像头

    2009-08-21 17:17:49

    C#摄像头编程

    2009-08-21 17:24:18

    C#控制摄像头

    2016-02-22 10:30:38

    AngularJSHTML5摄像头

    2023-02-26 22:06:22

    Electron浏览器开发

    2022-04-15 11:30:59

    代码,Python保存视频

    2009-09-08 09:31:54

    c# CheckBox

    2023-09-14 10:05:33

    人工智能智能摄像头

    2025-01-13 00:00:30

    WinForm应用开发

    2017-06-20 11:45:52

    2021-03-11 10:21:55

    特斯拉黑客网络攻击

    2013-03-21 09:56:09

    2009-08-27 18:05:54

    C#索引功能

    2024-11-29 16:51:18

    2011-04-25 09:16:10

    Windows 8

    2012-06-23 20:13:44

    HTML5

    2009-08-06 10:55:46

    C#代码解释器

    2009-08-13 10:15:50

    C#读取Excel

    2009-09-07 15:27:04

    C# MessageB

    2009-08-13 17:36:54

    编译C#代码
    点赞
    收藏

    51CTO技术栈公众号