Control study-> enlarge the image area when the mouse moves over the image

Source: Internet
Author: User

(1). Description

Point the cursor to an area of an image. This area is enlarged to make it clearer.
Class: Graphics implementation.

(2). Image example

(3 ).Code

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. Drawing. drawing2d;

Resize the image area using namespace
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. picturebox picturebox1;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Private system. Windows. Forms. Button button1;
// Private cursor mycursor;
Cursor mycursor = new cursor ("... // .. // magnify. cur"); // custom mouse
Graphics g;
Image myimage;

Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
System. Resources. ResourceManager resources = new system. Resources. ResourceManager (typeof (form1 ));
This. picturebox1 = new system. Windows. Forms. picturebox ();
This. button1 = new system. Windows. Forms. Button ();
This. suspendlayout ();
//
// Picturebox1
//
This. picturebox1.backcolor = system. Drawing. systemcolors. controltext;
This. picturebox1.image = (system. Drawing. Image) (resources. GetObject ("picturebox1.image ")));
This. picturebox1.location = new system. Drawing. Point (24, 16 );
This. picturebox1.name = "picturebox1 ";
This. picturebox1.size = new system. Drawing. Size (440,384 );
This. picturebox1.tabindex = 0;
This. picturebox1.tabstop = false;
This. picturebox1.mousemove + = new system. Windows. Forms. mouseeventhandler (this. picturebox#mousemove );
//
// Button1
//
This. button1.location = new system. Drawing. Point (384, 96 );
This. button1.name = "button1 ";
This. button1.tabindex = 1;
This. button1.text = "button1 ";
This. button1.click + = new system. eventhandler (this. button#click );
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. backcolor = system. Drawing. systemcolors. inactivecaptiontext;
This. clientsize = new system. Drawing. Size (472,406 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. picturebox1 );
This. Name = "form1 ";
This. Text = "form1 ";
This. Load + = new system. eventhandler (this. form#load );
This. mousemove + = new system. Windows. Forms. mouseeventhandler (this. form1_mousemove );
This. resumelayout (false );

}
# Endregion

/// <Summary>
/// ApplicationProgram.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}

Private void picturebox#mousemove (Object sender, system. Windows. Forms. mouseeventargs E)
{
Cursor. Current = mycursor;
Rectangle sourcerectangle = new rectangle (E. X-10, E. Y-10, 20, 20); // area to zoom in
// Rectangle destrectangle = new rectangle (E. X-20, E. Y-20, 40, 40 );
Rectangle destrectangle = new rectangle (picturebox1.width-150, picturebox1.height-150, picturebox1.width, picturebox1.height); // zoom in ratio
G. drawimage (myimage, destrectangle, sourcerectangle, graphicsunit. pixel );
}

Private void form=mousemove (Object sender, system. Windows. Forms. mouseeventargs E)
{
Cursor. Current = cursors. default;
}

Private void form1_load (Object sender, system. eventargs E)
{
G = This. picturebox1.creategraphics ();

Myimage = This. picturebox1.image;
}

Private void button#click (Object sender, system. eventargs E)
{
// Graphics G = This. picturebox1.creategraphics ();
Graphics G = picturebox1.creategraphics ();
G. drawline (new pen (color. Red, 5), 20, 20, 50, 50 );
}
}
}

(4). Sample download

Http://www.cnblogs.com/files/chengking/magnification .rar

 

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.