المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How to implement panning on image ?



C# Programming
03-29-2009, 12:11 AM
Hi
i was create a control to display images and able to zoom on images. now i want to implement panning on it, can anybody help me ?
Here is my full source code :

public class PictureBoxEx : ScrollableControl
{
private Image _image;
private Single _zoom;
private InterpolationMode _interpolationMode;
private Point lastPos;

#region Properties

[Category("Appearance"), Description("The image to be displayed.")]
public Image Image
{
get { return _image; }
set
{
_image = value;
this.UpdateScaleFactor();
this.Invalidate();
}
}

[Category("Appearance"), Description("The zoom factor. Less than 1 to reduce. More than 1 to magnify.")]
public Single Zoom
{
get { return _zoom; }
set
{
_zoom = value;
this.UpdateScaleFactor();
this.Invalidate();
}
}

[Category("Appearance"), Description("The interpolation mode used to smooth the drawing.")]
public InterpolationMode InterpolationMode
{
get { return _interpolationMode; }
set { _interpolationMode = value; }
}

public Point LastPosition
{
get { return lastPos; }
set { lastPos = value; }
}

#endregion

public PictureBoxEx()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
this.AutoScroll = true;
this.Zoom = 1f;
}

private void UpdateScaleFactor()
{
if (this.Image == null)
this.AutoScrollMargin = this.Size;
else
this.AutoScrollMinSize = new Size(Convert.ToInt32(this.Image.Width * this.Zoom + .5f), Convert.ToInt32(this.Image.Height * this.Zoom + .5f));
}

protected override void OnPaintBackground(PaintEventArgs e)
{
// Do nothing
}

protected override void OnPaint(PaintEventArgs e)
{
if (this.Image == null)
{
this.OnPaintBackground(e);
return;
}
Matrix mx = new Matrix(this.Zoom, 0, 0, this.Zoom, 0, 0);
mx.Translate(this.AutoScrollPosition.X / this.Zoom, this.AutoScrollPosition.Y / this.Zoom);
e.Graphics.Transform = mx;
e.Graphics.InterpolationMode = this.InterpolationMode;
e.Graphics.DrawImage(this.Image, new Rectangle(0, 0, this.Image.Width, this.Image.Height), 0, 0, this.Image.Width, this.Image.Height, GraphicsUnit.Pixel);
base.OnPaint(e);
}
}

Thanks