C # Image taking and saving watermark photos,
Because I have been working in colleges and universities and involved in enrollment, I have to collect student images during enrollment, so I wrote an image collection tool by hand.
Image Acquisition requires you to call the camera to take a photo. After checking the information on the Internet, you need to reference the following three dll files.
Take a look at the running interface
The interface is relatively low, mainly for function implementation.
Private void Camera_Load (object sender, EventArgs e) {this. btnSave. enabled = false; try {borderSize = GetBorderSize (this); captionHeight = GetCaptionHeight (this); // InitStudent ("", "", ""); this. comboBox_SizeMode.Text = "fill (keep ratio)"; FilterInfoCollection infos = new FilterInfoCollection (FilterCategory. videoInputDevice); if (infos! = Null) & (infos. count> 0) {} else {MessageBox. show ("no video device");} this. loadVedio (); this. splitContainer1.Panel2MinSize = 280; this. splitContainer1.SplitterDistance = this. splitContainer1.Width-this. splitContainer1.Panel2MinSize + 1; mf = new BorderForm (); mf. show (this); // mf. left = (this. left + this. splitContainer1.Panel1. width-mf. width)/2; // mf. top = (this. top + this. splitContainer1.Panel1. height-mf. height)/2; // marLeft = mf. left-this. left; // marTop = mf. top-this. top; Rectangle rtPic = this. pictureBox_Camera.RectangleToScreen (this. pictureBox_Camera.ClientRectangle); Rectangle rtMF = this. mf. rectangleToScreen (this. mf. clientRectangle); if (rtPic = null | rtMF = null | rtPic. width = 0 | rtMF. width = 0) {return;} mf. left = (rtMF. width + rtMF. left) + (rtPic. width + rtPic. left)/2; mf. top = (rtMF. height + rtMF. top) + (rtPic. height + rtPic. top)/2; mf. sizeChanged + = new EventHandler (mf_SizeChanged); mf. locationChanged + = new EventHandler (mf_LocationChanged); pictureBox_Camera_SizeChanged (sender, e); // start a video connection // this. shootOneTime = 0; this. timer1.Start ();} catch (Exception ex) {MessageBox. show (ex. message, "error", MessageBoxButtons. OK, MessageBoxIcon. error );}}
// Load the camera device private void LoadVedio () {FilterInfoCollection infos = new FilterInfoCollection (FilterCategory. VideoInputDevice); if (infos! = Null) & (infos. count> 0) {int index = 0; foreach (FilterInfo info in infos) {this. cmbCaptureDevice. items. add (new DeviceInfo (info. name, info. monikerString, index, FilterCategory. videoInputDevice); index ++;} this. cmbCaptureDevice. selectedIndex = 0 ;}}
/// <Summary> /// take a photo /// </summary> private void Shoot () {try {if (this. pictureBox_Camera.Image! = Null & (int) this. numericUpDown1.Value> 0 & (int) this. numericUpDown2.Value> 0) {Bitmap resultImage = new Bitmap (int) this. numericUpDown1.Value, (int) this. numericUpDown2.Value); Graphics g = Graphics. fromImage (resultImage); g. copyFromScreen (new Point (this. mf. location. X + 1, this. mf. location. Y + 1), new Point (6, 6 + (isWin7? 2: 0), new Size (resultImage. Size. Width, resultImage. Size. Height-(6 + (isWin7? 2: 0); if (! String. IsNullOrEmpty (XH) {string str = ""; if (this. XH! = "") {Str = this. XH;} else if (this. SFZH! = "") {Str = this. SFZH;} else if (this. KSH! = "") {Str = this. KSH;} if (this. checkBox2.Checked) {str = XM + "" + str;} int txtWidth = (int) (g. measureString (str, new Font ("", 9 )). width * 1.1); Rectangle rec = new Rectangle (resultImage. width-txtWidth)/2, resultImage. height-16, txtWidth, 15); g. fillRectangle (Brushes. white, rec); StringFormat sf = new StringFormat (); sf. lineAlignment = StringAlignment. center; sf. alignment = StringAlignment. center; rec. height ++; g. drawString (str, new Font ("", 9), Brushes. black, rec, sf);} this. pictureBox_tx.Image = resultImage;} else {this. pictureBox_tx.Image = null ;}} catch (Exception ex) {MessageBox. show (ex. message, "", MessageBoxButtons. OK, MessageBoxIcon. error );}}
// Select the camera device private void cmbCaptureDevice_SelectedIndexChanged (object sender, EventArgs e) {if (this. cmbCaptureDevice. SelectedItem! = Null) {this. cmbDeviceCapability. items. clear (); VideoCaptureDevice device = new VideoCaptureDevice (DeviceInfo) this. cmbCaptureDevice. selectedItem ). monikerString); for (int I = 0; I <device. videoCapabilities. length; I ++) {VideoCapabilities capabilities = device. videoCapabilities [I]; DeviceCapabilityInfo item = new DeviceCapabilityInfo (capabilities. frameSize); this. cmbDeviceCapability. items. add (Item);} DeviceInfo selectedItem = (DeviceInfo) this. cmbCaptureDevice. SelectedItem; if (this. captureAForge! = Null) {this. captureAForge. newFrame-= new NewFrameEventHandler (this. captureAForge_NewFrame); // this. captureAForge. snapshotFrame-= new NewFrameEventHandler (this. captureAForge_SnapshotFrame); if (this. captureAForge. isRunning) {this. captureAForge. signalToStop ();} this. captureAForge. waitForStop (); this. captureAForge = null;} this. captureAForge = new VideoCaptureDevice (selectedItem. monikerString); this. captureAForge. provideSnapshots = true; this. captureAForge. newFrame + = new NewFrameEventHandler (this. captureAForge_NewFrame); // this. captureAForge. snapshotFrame + = new NewFrameEventHandler (this. captureAForge_SnapshotFrame); if (this. cmbDeviceCapability. items. count> 0) {this. cmbDeviceCapability. selectedIndex = 0 ;}}}
// Select the resolution private void cmbDeviceCapability_SelectedIndexChanged (object sender, EventArgs e) {string [] strArray = this. cmbDeviceCapability. text. trim (). split (new char [] {'X'}); int width = int. parse (strArray [0]); int height = int. parse (strArray [1]); if (this. captureAForge! = Null) {if (this. captureAForge. isRunning) {this. captureAForge. signalToStop ();} this. captureAForge. waitForStop (); this. captureAForge. desiredFrameSize = new Size (width, height); this. captureAForge. desiredSnapshotSize = new Size (width, height); // this. captureAForge. desired framerate = 1000; this. captureAForge. start ();}}
// Set the data source size private void comboBox_SizeMode_SelectedIndexChanged (object sender, EventArgs e) {switch (this. comboBox_SizeMode.Text) {case "default (original size)": this. pictureBox_Camera.SizeMode = PictureBoxSizeMode. normal; break; case "center (original size)": this. pictureBox_Camera.SizeMode = PictureBoxSizeMode. centerImage; break; case "fill (stretch image)": this. pictureBox_Camera.SizeMode = PictureBoxSizeMode. stretchImage; break; case "fill (keep ratio)": this. pictureBox_Camera.SizeMode = PictureBoxSizeMode. zoom; break ;}}
// Save the photo private void buttonSave_Click (object sender, EventArgs e) {try {string filename = Path. combine (Application. startupPath, "StuImages", "Newstuimages", this. XH + ". JPG "); if (! This. checkBox1.Checked & File. Exists (filename) {if (MessageBox. Show ("the generated photo File already Exists. Do you want to overwrite it? "," ", MessageBoxButtons. OKCancel, MessageBoxIcon. warning) = DialogResult. OK) {// STORE this. pictureBox_tx.Image.Save (filename); // OnDataChanged (this, new DataEventArgs (string. format ("profile picture collection (overwrite), student ID: {0}, name: {1}, student ID: {2}, ID number: {3}", this. XH, this. XM, this. KSH, this. SFZH); this.txt KSH. focus (); this.txt KSH. selectAll () ;}else {return ;}} else {this. pictureBox_tx.Image.Save (filename); // OnDataChanged (this, new DataEventArgs (string. format ("profile picture collection, student ID: {0}, name: {1}, student ID: {2}, ID number: {3}", this. XH, this. XM, this. KSH, this. SFZH); this.txt KSH. focus (); this.txt KSH. selectAll ();} this. timer1.Start ();} catch {} finally {// enable the following line. After you click Save, the Avatar will be refreshed immediately. // this. timer1.Start (); this. btnSave. enabled = false ;}}
You can choose to automatically overwrite a photo of the same name or add a watermark to the photo.
During the operation, you can drag the rectangular box to select a photo
Source code download: http://files.cnblogs.com/files/luoxiaozhao/Image_acquisitionForm.rar
Please support originality. For reposted content, please indicate the source.