C#实现手机拍照并且保存水印照片
程序员文章站
2022-03-10 14:28:55
图像采集需要调用摄像头就行拍照操作,网上查了一下资料,需要引用以下3个dll。
看一下运行界面
界面都比较low,主要是功能实现。
private v...
图像采集需要调用摄像头就行拍照操作,网上查了一下资料,需要引用以下3个dll。
看一下运行界面
界面都比较low,主要是功能实现。
private void camera_load(object sender, eventargs e) { this.btnsave.enabled = false; try { bordersize = getbordersize(this); captionheight = getcaptionheight(this); //initstudent("", "", ""); this.combobox_sizemode.text = "填充(保持比例)"; filterinfocollection infos = new filterinfocollection(filtercategory.videoinputdevice); if ((infos != null) && (infos.count > 0)) { } else { messagebox.show("没有视频设备"); } 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); //启动连拍 //this.shootonetime = 0; this.timer1.start(); } catch (exception ex) { messagebox.show(ex.message, "错误", messageboxbuttons.ok, messageboxicon.error); } }
//加载摄像头设备 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> /// 拍照 /// </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); } }
//选择摄像装置 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; } } }
//选择分辨率 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.desiredframerate = 1000; this.captureaforge.start(); } }
//设置数据源大小 private void combobox_sizemode_selectedindexchanged(object sender, eventargs e) { switch (this.combobox_sizemode.text) { case "默认(原始大小)": this.picturebox_camera.sizemode = pictureboxsizemode.normal; break; case "居中(原始大小)": this.picturebox_camera.sizemode = pictureboxsizemode.centerimage; break; case "填充(拉伸图像)": this.picturebox_camera.sizemode = pictureboxsizemode.stretchimage; break; case "填充(保持比例)": this.picturebox_camera.sizemode = pictureboxsizemode.zoom; break; } }
//保存照片 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("该生已经有照片文件,是否覆盖? ", "", messageboxbuttons.okcancel, messageboxicon.warning) == dialogresult.ok) { //存储到本地 this.picturebox_tx.image.save(filename); //ondatachanged(this, new dataeventargs(string.format("头像采集(覆盖),学号:{0},姓名:{1},考生号:{2},身份证号:{3}", this.xh, this.xm, this.ksh, this.sfzh))); this.txtksh.focus(); this.txtksh.selectall(); } else { return; } } else { this.picturebox_tx.image.save(filename); //ondatachanged(this, new dataeventargs(string.format("头像采集,学号:{0},姓名:{1},考生号:{2},身份证号:{3}", this.xh, this.xm, this.ksh, this.sfzh))); this.txtksh.focus(); this.txtksh.selectall(); } this.timer1.start(); } catch { } finally { //启用下面一行,点击保存后头像会立即启动刷新 //this.timer1.start(); this.btnsave.enabled = false; } }
保存照片可以选择自动覆盖同名照片或者在照片中加入水印效果。
操作时候可以自行拖动长方形的框进行选择拍照
以上所述是小编给大家介绍的c#实现手机拍照并且保存水印照片,希望对大家有所帮助
下一篇: WPF MVVM制作发送短信小按钮