WinForm picture dragging and zooming

Source: Internet
Author: User

Recently did the project encountered upload construction floor plan, view, because the picture is larger, the general display resolution can not display the whole, and then also need to enlarge to see clearly the text content inside the picture, so need to use the picture drag and zoom function. Here to organize the operation.

First create a new form, drag a panel control into the form, drag a Pictureobx control into the panel, and then add a button to upload the picture:

Specific code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Reflection;namespaceimage Shift Zoom { Public Partial classform1:form {Bitmap mybmp; Point Mousedownpoint=NewPoint ();//record drag process mouse position        BOOLIsmove =false;//determines whether the mouse is in the drag process (the left mouse button is pressed) when moving on PictureBox        intZoomstep = -;//Zoom Step         PublicForm1 () {InitializeComponent (); }        //image Upload        Private voidButton1_Click (Objectsender, EventArgs e) {            stringfilename =""; OpenFileDialog Dlg=NewOpenFileDialog (); Dlg. Filter="TIFF File |*.tif| BMP File |*.bmp| Erdas img File |*.img| Evni file |*.hdr|jpeg file |*.jpg|raw file |*.raw|vrt file |*.vrt| all files |*.*"; Dlg. FilterIndex=8; if(DLG. ShowDialog () = =dialogresult.ok) {filename=dlg.            FileName; }            if(FileName = ="")            {                return; } mybmp=NewBitmap (filename); if(Mybmp = =NULL) {MessageBox.Show ("Read failed"); return; } TextBox1.Text=filename; pictureBox1.Image=mybmp; Picturebox1.sizemode= Pictureboxsizemode.zoom;//set PictureBox to zoom modePicturebox1.width =Mybmp.width; Picturebox1.height=Mybmp.height; }        //Mouse down function        Private voidPicturebox1_mousedown (Objectsender, MouseEventArgs e) {            if(E.button = =mousebuttons.left) {mousedownpoint.x=cursor.position.x; Mousedownpoint.y=CURSOR.POSITION.Y; Ismove=true;                           Picturebox1.focus (); }        }        //Mouse Release function        Private voidPicturebox1_mouseup (Objectsender, MouseEventArgs e) {            if(E.button = =mousebuttons.left) {ismove=false; }        }        //Mouse movement Function        Private voidPicturebox1_mousemove (Objectsender, MouseEventArgs e)            {Picturebox1.focus (); if(ismove) {intx, y; intMoveX, 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=NewPoint (x, y); Mousedownpoint.x=cursor.position.x; Mousedownpoint.y=CURSOR.POSITION.Y; }        }        //Mouse wheel scrolling function        Private voidPicturebox1_mousewheel (Objectsender, MouseEventArgs e) {            intx =e.location.x; inty =E.LOCATION.Y; intow =Picturebox1.width; intOh =Picturebox1.height; intVX, 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/Ten)                    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=NewPoint (picturebox1.location.x + VX, PICTUREBOX1.LOCATION.Y +VY); }        Private voidPanel2_mousedown (Objectsender, MouseEventArgs e) {            if(E.button = =mousebuttons.left) {mousedownpoint.x=cursor.position.x; Mousedownpoint.y=CURSOR.POSITION.Y; Ismove=true; }        }        Private voidPanel2_mouseup (Objectsender, MouseEventArgs e) {            if(E.button = =mousebuttons.left) {ismove=false; }        }        Private voidPanel2_mousemove (Objectsender, MouseEventArgs e) {Panel2.              Focus (); if(ismove) {intx, y; intMoveX, 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=NewPoint (x, y); Mousedownpoint.x=cursor.position.x; Mousedownpoint.y=CURSOR.POSITION.Y; }        }    }}

One thing to note here is that the class uses a picturebox1_mousewheel time, which is the time that the PictureBox control does not have, so you need to add this event manually, and you can add it directly to the Form1 design class.

//             //PictureBox1//              This. Picturebox1.backcolor =System.Drawing.Color.White;  This. picturebox1.location =NewSystem.Drawing.Point ( -, -);  This. Picturebox1.margin =NewSystem.Windows.Forms.Padding (2,2,2,2);  This. Picturebox1.name ="PictureBox1";  This. picturebox1.size =NewSystem.Drawing.Size ( the, the);  This. Picturebox1.tabindex =0;  This. Picturebox1.tabstop =false;  This. Picturebox1.mousedown + =NewSystem.Windows.Forms.MouseEventHandler ( This. Picturebox1_mousedown);  This. picturebox1.mousemove + =NewSystem.Windows.Forms.MouseEventHandler ( This. Picturebox1_mousemove);  This. picturebox1.mouseup + =NewSystem.Windows.Forms.MouseEventHandler ( This. Picturebox1_mouseup);  This. Picturebox1.mousewheel + =NewSystem.Windows.Forms.MouseEventHandler ( This. Picturebox1_mousewheel);

So that you can run it straight ahead.

WinForm picture dragging and zooming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.