现已实现抓图的功能,照相后图片保存到一个固定的路径下,叫一个固定的名称,比如:C:/image.jpg.或者是C:/image.bmp
问题是,保存的图片竟然是901KB...这样的话,我想要把图片存入数据库就会超出字段范围。
如何才能让其保存的图片为100KB以内?(就是系统图片那么大也才100KB不到的)
一下是代码:
cam类:
C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace PlatForm
{
//参考AVICap技术
public class Cam
{
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 IntPtr mControlPtr;
private bool bWorkStart = false;
private int mWidth;
private int mHeight;
private int mLeft;
private int mTop;
///
/// 初始化显示图像
///
///
控件的句柄
///
开始显示的左边距
///
开始显示的上边距
///
要显示的宽度
///
要显示的长度
public Cam(IntPtr handle, int left, int top, int width, int height)
{
mControlPtr = handle;
mWidth = width;
mHeight = height;
mLeft = left;
mTop = top;
}
//如果该函数调用成功 则函数返回窗口的句柄 否则函数返回NULL。
[DllImport("avicap32.dll")]
private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
// 视频格式设置对话框
//BOOL capDlgVideoFormat( hwnd ); // hwnd:捕捉窗口句柄
//视频格式设置对话框对于每一个捕捉驱动程序来说,是唯一的。而且,有些驱动程序不一定支持这一功能。应用程序可以通过检测CAPDRIVERCAPS结构的成员变量fHasDlgVideoFormat来判断驱动程序是否支持这一功能。
[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, IntPtr wParam, IntPtr 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, IntPtr.Zero, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, IntPtr.Zero, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, IntPtr.Zero, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, IntPtr.Zero, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_SCALE, (IntPtr)1, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, (IntPtr)66, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_OVERLAY, (IntPtr)1, IntPtr.Zero);
SendMessage(hWndC, WM_CAP_SET_PREVIEW, (IntPtr)1, IntPtr.Zero);
}
return;
}
///
/// 停止显示图像
///
public void Stop()
{
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, IntPtr.Zero, IntPtr.Zero);
bWorkStart = false;
}
///
/// 抓图
///
///
要保存bmp文件的路径
public void GrabImage(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); //将System.String内容复制到非托管内存当中
SendMessage(hWndC, WM_CAP_SAVEDIB, IntPtr.Zero, hBmp);
}
///
///录像
///
///
要保存avi文件的路径
public void Kinescope(string path)
{
IntPtr hCap = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, IntPtr.Zero, hCap);
SendMessage(hWndC, WM_CAP_SEQUENCE, IntPtr.Zero, hCap);
}
///
///停止录像
///
public void StopKinescope()
{
SendMessage(hWndC, WM_CAP_STOP, IntPtr.Zero, IntPtr.Zero);
}
}
}
然后我的窗体里面有个pictureBox可以显示,代码是:
cam.Stop();
cam.Start();
//图片赋值
string myfilename="C:/image.jpg";
//摄像头抓图
cam.GrabImage(myfilename);
FileStream fs = new FileStream(myfilename, FileMode.Open, FileAccess.Read);
byte[] mybytes = new byte[fs.Length];
fs.Read(mybytes, 0, mybytes.Length);
if (mybytes.Length > 800000)
{
member.Poto = "";
}
else
{
//要存入数据库的字段
member.Poto = Convert.ToBase64String(mybytes);
}
fs.Close();