WinForm实现的图片拖拽与缩放功能示例
程序员文章站
2023-11-12 19:32:52
本文实例讲述了winform实现的图片拖拽与缩放功能。分享给大家供大家参考,具体如下:
最近做项目的时候遇到上传施工平面布置图,查看,因为图片比较大,一般的显示器分辨率无...
本文实例讲述了winform实现的图片拖拽与缩放功能。分享给大家供大家参考,具体如下:
最近做项目的时候遇到上传施工平面布置图,查看,因为图片比较大,一般的显示器分辨率无法显示全,然后还需要放大看清楚图片里面的文字内容,所以需要用到图片的拖拽与缩放功能。这里整理下具体操作。
首先新建一个窗体,拖一个panel控件到窗体中,然后在拖一个pictureobx控件到panel中,然后在添加个上传图片的按钮:
具体代码:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.reflection; namespace 图像平移缩放 { public partial class form1 : form { bitmap mybmp; point mousedownpoint = new point(); //记录拖拽过程鼠标位置 bool ismove = false; //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下) int zoomstep = 20; //缩放步长 public form1() { initializecomponent(); } //图片上传 private void button1_click(object sender, eventargs e) { string filename = ""; openfiledialog dlg = new openfiledialog(); dlg.filter = "tiff文件|*.tif|bmp文件|*.bmp|erdas img文件|*.img|evni文件|*.hdr|jpeg文件|*.jpg|raw文件|*.raw|vrt文件|*.vrt|所有文件|*.*"; dlg.filterindex = 8; if (dlg.showdialog() == dialogresult.ok) { filename = dlg.filename; } if (filename == "") { return; } mybmp = new bitmap(filename); if (mybmp == null) { messagebox.show("读取失败"); return; } textbox1.text = filename; picturebox1.image = mybmp; picturebox1.sizemode = pictureboxsizemode.zoom; //设置picturebox为缩放模式 picturebox1.width = mybmp.width; picturebox1.height = mybmp.height; } //鼠标按下功能 private void picturebox1_mousedown(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { mousedownpoint.x = cursor.position.x; mousedownpoint.y = cursor.position.y; ismove = true; picturebox1.focus(); } } //鼠标松开功能 private void picturebox1_mouseup(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { ismove = false; } } //鼠标移动功能 private void picturebox1_mousemove(object sender, mouseeventargs e) { picturebox1.focus(); if (ismove) { int x, y; int movex, movey; movex = cursor.position.x - mousedownpoint.x; movey = cursor.position.y - mousedownpoint.y; x = picturebox1.location.x + movex; y = picturebox1.location.y + movey; picturebox1.location = new point(x, y); mousedownpoint.x = cursor.position.x; mousedownpoint.y = cursor.position.y; } } //鼠标滚轮滚动功能 private void picturebox1_mousewheel(object sender, mouseeventargs e) { int x = e.location.x; int y = e.location.y; int ow = picturebox1.width; int oh = picturebox1.height; int vx, vy; if (e.delta > 0) { picturebox1.width += zoomstep; picturebox1.height += zoomstep; propertyinfo pinfo = picturebox1.gettype().getproperty("imagerectangle", bindingflags.instance | bindingflags.nonpublic); rectangle rect = (rectangle)pinfo.getvalue(picturebox1, null); picturebox1.width = rect.width; picturebox1.height = rect.height; } if (e.delta < 0) { if (picturebox1.width < mybmp.width / 10) return; picturebox1.width -= zoomstep; picturebox1.height -= zoomstep; propertyinfo pinfo = picturebox1.gettype().getproperty("imagerectangle", bindingflags.instance | bindingflags.nonpublic); rectangle rect = (rectangle)pinfo.getvalue(picturebox1, null); picturebox1.width = rect.width; picturebox1.height = rect.height; } vx = (int)((double)x * (ow - picturebox1.width) / ow); vy = (int)((double)y * (oh - picturebox1.height) / oh); picturebox1.location = new point(picturebox1.location.x + vx, picturebox1.location.y + vy); } private void panel2_mousedown(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { mousedownpoint.x = cursor.position.x; mousedownpoint.y = cursor.position.y; ismove = true; } } private void panel2_mouseup(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { ismove = false; } } private void panel2_mousemove(object sender, mouseeventargs e) { panel2.focus(); if (ismove) { int x, y; int movex, movey; movex = cursor.position.x - mousedownpoint.x; movey = cursor.position.y - mousedownpoint.y; x = picturebox1.location.x + movex; y = picturebox1.location.y + movey; picturebox1.location = new point(x, y); mousedownpoint.x = cursor.position.x; mousedownpoint.y = cursor.position.y; } } } }
这里需要注意一点,类里面用到了一个 picturebox1_mousewheel 时间,这个是picturebox控件没有的时间,所以需要手动去添加这个事件,可以直接在form1的设计类中添加
// // picturebox1 // this.picturebox1.backcolor = system.drawing.color.white; this.picturebox1.location = new system.drawing.point(20, 20); this.picturebox1.margin = new system.windows.forms.padding(2, 2, 2, 2); this.picturebox1.name = "picturebox1"; this.picturebox1.size = new system.drawing.size(67, 34); this.picturebox1.tabindex = 0; this.picturebox1.tabstop = false; this.picturebox1.mousedown += new system.windows.forms.mouseeventhandler(this.picturebox1_mousedown); this.picturebox1.mousemove += new system.windows.forms.mouseeventhandler(this.picturebox1_mousemove); this.picturebox1.mouseup += new system.windows.forms.mouseeventhandler(this.picturebox1_mouseup); this.picturebox1.mousewheel += new system.windows.forms.mouseeventhandler(this.picturebox1_mousewheel);
这样就能直接运行成功了
更多关于c#相关内容感兴趣的读者可查看本站专题:《winform控件用法总结》、《c#窗体操作技巧汇总》、《c#数据结构与算法教程》、《c#常见控件用法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结》
希望本文所述对大家c#程序设计有所帮助。
上一篇: pyqt5 实现工具栏文字图片同时显示