C#操作摄像头照相(考生的大头照),怎样弄个取景框?
大家好!
我现在要做一个关于摄像头照相的程序,现在要求有个取景框,点“照相”时得到的照片就是取景框中的图像部分。现在已经做出点“照相”是全部的图像部分,但图片有多余的部分(是考生的大头像,只要求照的是头部)。
取景框的要求是进入照相的窗口时就存在,可以拖动。
各位帮帮忙,谢谢了!
------解决方案--------------------
实惠点,直接上代码吧
两个PictureBox,一个显示原图pb_Shoot,一个显示框选的图pb_Photo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form2 : WeifenLuo.WinFormsUI.DockContent
{
int _RectangleX; //矩形框的左上顶点的X坐标
int _RectangleY; //矩形框的左上顶点的Y坐标
int _OldMouseX; //鼠标左键按下时的X点坐标
int _OldMouseY; //鼠标左键按下时的Y点坐标
int _NewMouseX; //鼠标移动时的X点坐标
int _NewMouseY; //鼠标移动时的Y点坐标
bool _Draging = false; //是否在拖拽状态
bool _HasDraw = false; //是否已经画过矩形
int _RectangleWidth; //选择区域的宽度
int _RectangleHeight; //选择区域的高度
Pen _Pen = new Pen(Color.LightBlue, 2);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
pb_Shoot.Image = Image.FromFile(@"C:\abc15.gif");
_RectangleWidth = pb_Photo.Width;
_RectangleHeight = pb_Photo.Height;
}
void InitFrame()
{
_RectangleX = pb_Shoot.Left + pb_Shoot.ClientRectangle.Width / 2 - _RectangleWidth / 2;
_RectangleY = pb_Shoot.Top + pb_Shoot.ClientRectangle.Height / 2 - _RectangleHeight / 2;
Graphics g = pb_Shoot.CreateGraphics();
g.DrawRectangle(_Pen, _RectangleX, _RectangleY, _RectangleWidth, _RectangleHeight);
g.Dispose();
GC.Collect();
}
Image InterceptImage()
{
Graphics g1 = this.pb_Shoot.CreateGraphics();
Image MyImage = new Bitmap(_RectangleWidth, _RectangleHeight, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, _RectangleWidth, _RectangleHeight, dc1, _RectangleX, _RectangleY, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
return MyImage;
}
private void pb_Shoot_MouseDown(object sender, MouseEventArgs e)
{
_OldMouseX = e.X;
_OldMouseY = e.Y;
}
private void pb_Shoot_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_Draging = true;
}
else
{
if (!_HasDraw)
{
InitFrame();