Lutinore
Messages postés
3246
Date d'inscription
lundi 25 avril 2005
Statut
Membre
Dernière intervention
27 octobre 2012
41
4 mars 2007 à 02:04
Salut, tiens si tu veux un exemple.. mais c'est fait à "l'arrache".
// Certifié ISO 1664
public partial class Form1 : Form
{
private const int STEP = 32;
private PictureBox pb = null;
private Bitmap bmp = null;
private int x = 0;
private int y = 0;
private int width = 250;
private int height = 250;
private int maxX = 0;
private int maxY = 0;
private bool zoom = false;
public Form1( )
{
InitializeComponent( );
bmp = new Bitmap( "d:\\image.jpg" );
SetValues( );
pb = new PictureBox( );
pb.Location = new Point( 32, 32 );
pb.Size = new Size( 250, 250 );
pb.Paint += PictureBox_Paint;
Button left = new Button( );
left.Name = "left";
left.Top = 32;
left.Size = new Size( 32, 32 );
left.Click += Buttons_Click;
Button up = new Button( );
up.Name = "up";
up.Left = 32;
up.Size = new Size( 32, 32 );
up.Click += Buttons_Click;
Button right = new Button( );
right.Name = "right";
right.Left = 64;
right.Top = 32;
right.Size = new Size( 32, 32 );
right.Click += Buttons_Click;
Button down = new Button( );
down.Name = "down";
down.Left = 32;
down.Top = 64;
down.Size = new Size( 32, 32 );
down.Click += Buttons_Click;
Button zoom = new Button( );
zoom.Text = "z";
zoom.Name = "zoom";
zoom.Left = 32;
zoom.Top = 32;
zoom.Size = new Size( 32, 32 );
zoom.Click += Buttons_Click;
Control cross = new Control( );
cross.Location = new Point( 314, 186 );
cross.Size = new Size( 96, 96 );
cross.Controls.AddRange( new Control[ ] { left, up, right, down, zoom } );
this.ClientSize = new Size( 442, 314 );
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Controls.AddRange( new Control[ ] { pb, cross } );
}
private void Buttons_Click( object sender, EventArgs args )
{
switch ( ( ( Button )sender ).Name )
{
case "left" : x -STEP; if ( x < 0 ) x 0;
break;
case "up" : y -STEP; if ( y < 0 ) y 0;
break;
case "right" : x +STEP; if ( x > maxX ) x maxX;
break;
case "down" : y +STEP; if ( y > maxY ) y maxY;
break;
case "zoom" :
zoom = !zoom;
SetValues( );
break;
}
pb.Invalidate( pb.ClientRectangle );
}
private void SetValues( )
{
if ( zoom )
{
width = STEP;
height = STEP;
}
else
{
width = 250;
height = 250;
}
maxX = ( bmp.Width > width ) ? bmp.Width - width : 0;
maxY = ( bmp.Height > height ) ? bmp.Height - height : 0;
if ( x > maxX ) x = maxX;
if ( y > maxY ) y = maxY;
}
private void PictureBox_Paint( object sender, PaintEventArgs args )
{
Graphics g = args.Graphics;
if ( zoom )
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage
(
bmp,
new Rectangle( 0, 0, 250, 250 ),
new Rectangle( x, y, width, height ),
GraphicsUnit.Pixel
);
g.DrawRectangle( Pens.Black, 0, 0, 249, 249 );
}
}