codice:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PhotoWebcam
{
public partial class captureWindow : Form
{
private int hHwnd;
private short iDevice;
//coordinate e dimensioni originali della PictureBox
private int ox, oy, ow, oh;
//coordinate e dimensioni della finestra webcam
private int camW, camH;
private int camX, camY;
//risoluzione webcam
int w = 800;
short h = 600;
public captureWindow()
{
InitializeComponent();
ox = imgCam.Left;
oy = imgCam.Top;
ow = imgCam.Width;
oh = imgCam.Height;
txtFilename.Text = PhotoWebcam.Properties.Settings.Default.defName;
comboExt.Text = PhotoWebcam.Properties.Settings.Default.defExt;
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void cmdTrayExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void impostazioniToolStripMenuItem_Click(object sender, EventArgs e)
{
preferWin pref = new preferWin();
pref.Show(this);
pref.Focus();
}
private void btnStart_Click(object sender, EventArgs e)
{
avviaWebcam();
}
private void avviaWebcam()
{
//Calcolo la dimesione e posizione della finestra
//dimensioni immagine richieste
int imgLar = PhotoWebcam.Properties.Settings.Default.imgLar;
int imgAlt = PhotoWebcam.Properties.Settings.Default.imgAlt;
if ((ow / oh) > (imgLar / imgAlt))
{
imgLar = Convert.ToInt32(1.0 * oh * imgLar / imgAlt);
imgAlt = oh;
}
else
{
imgAlt = Convert.ToInt32(1.0 * ow * imgAlt / imgLar);
imgLar = ow;
}
if ((w / h) > (imgLar / imgAlt))
{
camW = Convert.ToInt32(1.0 * imgAlt * w / h);
camH = imgAlt;
}
else
{
camW = imgLar;
camH = Convert.ToInt32(1.0 * imgLar * h / w);
}
imgCam.Width = imgLar;
imgCam.Height = imgAlt;
imgCam.Left = ((gb.Width - imgLar) / 2);
imgCam.Top = ((gb.Height - imgAlt) / 2);
camX = (imgLar - camW) / 2;
camY = (imgAlt - camH) / 2;
//imposto il device selezionato
iDevice = PhotoWebcam.Properties.Settings.Default.intDevice;
if (PhotoWebcam.Capture.capGetDriverDescriptionA(iDevice, "", 100, "", 100))
{
//Dico alla dll di dirigere il flusso alla mia finestra di preview
hHwnd = PhotoWebcam.Capture.capCreateCaptureWindowA(iDevice.ToString(), PhotoWebcam.Capture.WS_VISIBLE | PhotoWebcam.Capture.WS_CHILD, camX, camY, w, h, imgCam.Handle.ToInt32(), 0);
//mi collego al device
if (Convert.ToBoolean(PhotoWebcam.Capture.SendMessageA(hHwnd, PhotoWebcam.Capture.WM_CAP_DRIVER_CONNECT, iDevice, 0)))
{
//Imposto la preview
PhotoWebcam.Capture.SendMessageA(hHwnd, PhotoWebcam.Capture.WM_CAP_SET_SCALE, 1, 0);
//imposto il rate in millisecondi
PhotoWebcam.Capture.SendMessageA(hHwnd, PhotoWebcam.Capture.WM_CAP_SET_PREVIEWRATE, 66, 0);
//Faccio partire la preview
PhotoWebcam.Capture.SendMessageA(hHwnd, PhotoWebcam.Capture.WM_CAP_SET_PREVIEW, 1, 0);
//Scalo la preview all'interno della dimensione del mio box immagine
PhotoWebcam.Capture.SetWindowPos(hHwnd, PhotoWebcam.Capture.HWND_BOTTOM, camX, camY, camW, camH, PhotoWebcam.Capture.SWP_NOMOVE | PhotoWebcam.Capture.SWP_NOZORDER);
btnStart.Enabled = false;
btnStop.Enabled = true;
btnGrab.Enabled = true;
btnGrab.Image = PhotoWebcam.Properties.Resources.camera;
}
else
{
//Non è possibile accedere al device
PhotoWebcam.Capture.DestroyWindow(hHwnd);
System.Windows.Forms.MessageBox.Show(this, "WebCam non impostata.\nScegliere Strumenti -> Preferenze e selezionare un dispositivo","PhotoWebcam");
}
}
else
{
System.Windows.Forms.MessageBox.Show(this, "WebCam non impostata.\nScegliere Strumenti -> Preferenze e selezionare un dispositivo","PhotoWebcam");
}
}
private void btnStop_Click(object sender, EventArgs e)
{
//chiudo il flusso
PhotoWebcam.Capture.SendMessageA(hHwnd, PhotoWebcam.Capture.WM_CAP_DRIVER_DISCONNECT, iDevice, 0);
PhotoWebcam.Capture.DestroyWindow(hHwnd);
//sblocco i bottoni
btnStart.Enabled = true;
btnStop.Enabled = false;
btnGrab.Enabled = false;
}
private void btnGrab_Click(object sender, EventArgs e)
{
PhotoWebcam.Capture.SendMessageA(hHwnd, Convert.ToInt32(PhotoWebcam.Capture.WM_CAP_EDIT_COPY), 0, 0);
IDataObject ido = Clipboard.GetDataObject();
if (ido.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
{
Bitmap bmpImage = (Bitmap)ido.GetData(System.Windows.Forms.DataFormats.Bitmap);
Bitmap bmpCrop = bmpImage.Clone(new Rectangle(-camX, -camY, imgCam.Width, imgCam.Height), bmpImage.PixelFormat);
imgGrab.Image = (Image)bmpCrop;
}
Clipboard.Clear();
}
private void btnSave_Click(object sender, EventArgs e)
{
if (txtFilename.Text == "" || txtFilename.Text == null)
{
MessageBox.Show("Inserire un nome per il file", "PhotoWebcam", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (imgGrab.Image != null)
{
string percorso = PhotoWebcam.Properties.Settings.Default.defPath;
if (!System.IO.Directory.Exists(percorso))
System.IO.Directory.CreateDirectory(percorso);
percorso += "\\" + txtFilename.Text + "." + comboExt.Text;
System.Drawing.Imaging.ImageFormat imf;
switch (comboExt.Text)
{
case "jpg":
imf = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "gif":
imf = System.Drawing.Imaging.ImageFormat.Gif;
break;
case "png":
imf = System.Drawing.Imaging.ImageFormat.Png;
break;
case "bmp":
imf = System.Drawing.Imaging.ImageFormat.Bmp;
break;
default:
imf = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
}
imgGrab.Image.Save(percorso, imf);
MessageBox.Show(this, "Immagine salvata!", "PhotoWebcam", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
else
{
MessageBox.Show(this, "Nessuna Immagine", "PhotoWebcam", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
private void cancellaToolStripMenuItem_Click(object sender, EventArgs e)
{
imgGrab.Image = null;
}
private void copiaImmagineToolStripMenuItem_Click(object sender, EventArgs e)
{
if (imgGrab.Image != null)
{
Clipboard.Clear();
Clipboard.SetData(System.Windows.Forms.DataFormats.Bitmap, imgGrab.Image);
}
}
private void salvaConNomeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (imgGrab.Image != null)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Salva Immagine con nome";
sfd.FileName = PhotoWebcam.Properties.Settings.Default.defName;
sfd.Filter = "Immagine JPEG |*.jpg";
sfd.Filter += "|Immagine GIF |*.gif";
sfd.Filter += "|Immagine PNG |*.png";
sfd.Filter += "|Immagine BMP |*.bmp";
System.Drawing.Imaging.ImageFormat imf;
if (sfd.ShowDialog(this) == DialogResult.OK)
{
switch (sfd.FilterIndex)
{
case 1:
imf = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case 2:
imf = System.Drawing.Imaging.ImageFormat.Gif;
break;
case 3:
imf = System.Drawing.Imaging.ImageFormat.Png;
break;
case 4:
imf = System.Drawing.Imaging.ImageFormat.Bmp;
break;
default:
imf = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
}
imgGrab.Image.Save(sfd.FileName, imf);
MessageBox.Show(this, "Immagine salvata!", "PhotoWebcam", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
}
else
{
MessageBox.Show(this, "Nessuna Immagine", "PhotoWebcam", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
}
}