C#实现鼠标裁剪图像功能
程序员文章站
2023-11-29 14:59:46
本文实例为大家分享了c#实现鼠标裁剪图像的具体代码,供大家参考,具体内容如下
c#的图像裁剪很容易操作,这里给个实现的例子。
关键是需要处理鼠标的事件和一些更新
实现...
本文实例为大家分享了c#实现鼠标裁剪图像的具体代码,供大家参考,具体内容如下
c#的图像裁剪很容易操作,这里给个实现的例子。
关键是需要处理鼠标的事件和一些更新
实现鼠标移动的代码.注意更新不要全部重画,只有选择矩形部分重画
private void form1_mousemove(object sender, mouseeventargs e) { if (track_move) endpoint = new point(e.x, e.y); else { return; } rect1 = new rectangle(stpoint.x, stpoint.y, endpoint.x - stpoint.x, endpoint.y - stpoint.y); rectangle tempr = new rectangle(rect1.x, rect1.y, rect1.width + 2, rect1.height + 2); this.invalidate(tempr); }
选择结束的处理代码.
private void form1_mouseup(object sender, mouseeventargs e) { if (e.button == mousebuttons.left && track_move==true ) { track_move = false; endpoint = new point(e.x, e.y); rect1 = new rectangle(stpoint.x, stpoint.y, endpoint.x - stpoint.x, endpoint.y - stpoint.y); rectangle rectorg = new rectangle(borg.x, borg.y, image1.width, image1.height); if (rect1.width <= 0) return; if (rect1.height <= 0) return; if (rectorg.contains(rect1)) { rectangle rectadj = new rectangle(rect1.x - borg.x, rect1.y - borg.y, rect1.width, rect1.height); bitmap cropimge = image1.clone(rectadj, system.drawing.imaging.pixelformat.format24bpprgb); picturebox2.image = cropimge; } else { picturebox2.image = null; } this.invalidate(); } }
程序的整个代码
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.runtime.interopservices; namespace imageforms { static class program { /// <summary> /// 应用程序的主入口点。 /// </summary> [stathread] static void main() { application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); application.run(new form1()); } } public partial class form1 : form { private system.windows.forms.picturebox picturebox2; private system.windows.forms.label label1; public form1() { initializecomponent(); } private void picturebox1_click(object sender, eventargs e) { } private void form1_load(object sender, eventargs e) { showimg(); } bitmap image1; private void showimg() { int wd = 400; int hg = 200; int len = wd * hg * 3; byte[] pdata = new byte[len]; for (int i = 0; i < len; i++) { if (i > 3 * wd * (hg / 2)) { pdata[i] = 255; } else { pdata[i] = 0; } } try { image1 = new bitmap(wd, hg, system.drawing.imaging.pixelformat.format24bpprgb); for (int y = 0; y < hg; y++) { for (int x = 0; x < wd; x++) { color crr = color.fromargb(pdata[3 * wd * y + x], pdata[3 * wd * y + x], pdata[3 * wd * y + x]); image1.setpixel(x, y, crr); } } // set the picturebox to display the image. // picturebox1.image = image1; } catch (argumentexception) { messagebox.show("there was an error check data."); } } point stpoint,endpoint; rectangle rect1; point borg = new point(20, 20); protected override void onpaint(painteventargs e) { base.onpaint(e); e.graphics.drawimage(image1, borg); if (rect1 != null ) { e.graphics.drawrectangle(new pen(color.red, 1), rect1); } } private void form1_mousedown(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { stpoint = new point(e.x, e.y); track_move = true; return; } track_move = false; } private void form1_mouseup(object sender, mouseeventargs e) { if (e.button == mousebuttons.left && track_move==true ) { track_move = false; endpoint = new point(e.x, e.y); rect1 = new rectangle(stpoint.x, stpoint.y, endpoint.x - stpoint.x, endpoint.y - stpoint.y); rectangle rectorg = new rectangle(borg.x, borg.y, image1.width, image1.height); if (rect1.width <= 0) return; if (rect1.height <= 0) return; if (rectorg.contains(rect1)) { rectangle rectadj = new rectangle(rect1.x - borg.x, rect1.y - borg.y, rect1.width, rect1.height); bitmap cropimge = image1.clone(rectadj, system.drawing.imaging.pixelformat.format24bpprgb); picturebox2.image = cropimge; } else { picturebox2.image = null; } this.invalidate(); } } bool track_move=false ; private void form1_mousemove(object sender, mouseeventargs e) { if (track_move) endpoint = new point(e.x, e.y); else { return; } rect1 = new rectangle(stpoint.x, stpoint.y, endpoint.x - stpoint.x, endpoint.y - stpoint.y); rectangle tempr = new rectangle(rect1.x, rect1.y, rect1.width + 2, rect1.height + 2); this.invalidate(tempr); } private system.componentmodel.icontainer components = null; private void initializecomponent() { this.picturebox2 = new system.windows.forms.picturebox(); this.label1 = new system.windows.forms.label(); ((system.componentmodel.isupportinitialize)(this.picturebox2)).begininit(); this.suspendlayout(); // // picturebox2 // this.picturebox2.location = new system.drawing.point(605, 103); this.picturebox2.name = "picturebox2"; this.picturebox2.size = new system.drawing.size(227, 173); this.picturebox2.tabindex = 1; this.picturebox2.tabstop = false; // // label1 // this.label1.autosize = true; this.label1.location = new system.drawing.point(602, 58); this.label1.name = "label1"; this.label1.size = new system.drawing.size(127, 15); this.label1.tabindex = 2; this.label1.text = "鼠标左键选择裁剪"; // // form1 // this.autoscaledimensions = new system.drawing.sizef(8f, 15f); this.autoscalemode = system.windows.forms.autoscalemode.font; this.clientsize = new system.drawing.size(844, 558); this.controls.add(this.label1); this.controls.add(this.picturebox2); this.name = "form1"; this.text = "form1"; this.load += new system.eventhandler(this.form1_load); this.mouseup += new system.windows.forms.mouseeventhandler(this.form1_mouseup); this.mousedown += new system.windows.forms.mouseeventhandler(this.form1_mousedown); this.mousemove += new system.windows.forms.mouseeventhandler(this.form1_mousemove); ((system.componentmodel.isupportinitialize)(this.picturebox2)).endinit(); this.resumelayout(false); this.performlayout(); } protected override void dispose(bool disposing) { if (disposing && (components != null)) { components.dispose(); } base.dispose(disposing); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 实用的JS表单验证提示效果