Hi John,
I think you shouldn't do it like this. You create a Graphics object out of your picture and then redraw the picture to the same object, this is why you still see the "old" pictures (they are still on the graphics). I'd avoid the problem like this: just remove the complete cloning and drawing part of Scale Image like this:
public void ScaleImage(float percent)
{
this.m_bmpScale = percent * 0.01f;
Refresh();
}
and in your draw method just scale before drawing:
private void RenderOverlay(PaintEventArgs e)
{
try
{
e.Graphics.ScaleTransform(m_bmpScale, m_bmpScale);
e.Graphics.DrawImage(CurrentBitmap, m_bmpOrigin);
}
catch (Exception ex)
{
if (ex != null) { }
}
}
You have another problem when moving the image because you don't consider the current scale.
I'd do the whole thing like this:
using System;
using System.Windows.Forms;
using System.Drawing;
namespace Common.Controls
{
public class PictureContainer : UserControl
{
bool bMoving;
Point ptOffsetFromImagePosition;
public Image BackgroundImage { get; set; }
public Image Picture { get; set; }
public float PictureScale { get; set; }
public Point PicturePosition { get; set; }
public PictureContainer() : this(null, null) { }
public PictureContainer(Image imagePicture) : this(imagePicture, null) { }
public PictureContainer(Image imagePicture, Image imageBackground)
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
Picture = imagePicture;
BackgroundImage = imageBackground;
PictureScale = 1f;
}
public Rectangle CurrentPictureRectangle
{
get
{
return Picture != null ?
new Rectangle(PicturePosition.X, PicturePosition.Y,
(int)(PictureScale * Picture.Width), (int)(PictureScale * Picture.Height)) :
new Rectangle();
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (Picture != null)
{
e.Graphics.ScaleTransform(PictureScale, PictureScale);
e.Graphics.DrawImage(Picture, PicturePosition);
}
base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
if (BackgroundImage != null)
e.Graphics.DrawImage(BackgroundImage, ClientRectangle);
else
base.OnPaintBackground(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left &&
CurrentPictureRectangle.Contains(e.Location))
{
ptOffsetFromImagePosition = new Point(e.X - PicturePosition.X, e.Y - PicturePosition.Y);
bMoving = true;
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
bMoving = false;
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (bMoving)
{
PicturePosition = new Point(e.X - ptOffsetFromImagePosition.X, e.Y - ptOffsetFromImagePosition.Y);
Refresh();
}
base.OnMouseMove(e);
}
}
}