<code>public class DrawCanvas : Control
{
private int _scale = 10000;
private Image m_bkImg = null;
private Image m_bkScaledImg = null;
private Point m_ptStart;
private Point m_ptEnd;
private Rectangle m_dstRect;
private Rectangle m_srcRect;
private VScrollBar m_vScrollBar;
private HScrollBar m_hScrollBar;
public DrawCanvas()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
m_vScrollBar = new VScrollBar();
m_vScrollBar.Scroll += OnScroll;
m_hScrollBar = new HScrollBar();
m_hScrollBar.Scroll += OnScroll;
Controls.Add(m_vScrollBar);
Controls.Add(m_hScrollBar);
m_dstRect = new Rectangle();
m_srcRect = ClientRectangle;
}
public float ScaleValue { get => _scale / 100.0f; }
private void OnScroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
m_dstRect.Y = e.NewValue;
if (m_dstRect.Y < 0)
m_dstRect.Y = 0;
}
else if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
m_dstRect.X = e.NewValue;
if (m_dstRect.X < 0)
m_dstRect.X = 0;
}
Invalidate();
}
private void CalcScrollSize()
{
m_srcRect = ClientRectangle;
int curImgWidth = m_bkScaledImg == null ? 0 : m_bkScaledImg.Width;
int curImgHeight = m_bkScaledImg == null ? 0 : m_bkScaledImg.Height;
bool hVisiable = false;
bool vVisiable = false;
if (curImgHeight > Height)
vVisiable = true;
if (curImgWidth > Width)
hVisiable = true;
if (!vVisiable)
{
if (hVisiable)
{
vVisiable = curImgHeight > (Height - m_hScrollBar.Height);
}
}
if (!hVisiable)
{
if (vVisiable)
{
hVisiable = curImgWidth > (Width - m_vScrollBar.Width);
}
}
m_vScrollBar.Visible = vVisiable;
m_hScrollBar.Visible = hVisiable;
if (m_vScrollBar.Visible)
{
m_vScrollBar.Location = new Point(Width - m_vScrollBar.Width, 0);
m_vScrollBar.Height = m_hScrollBar.Visible ? Height - m_hScrollBar.Height : Height;
m_vScrollBar.SmallChange = Height / 20;
m_vScrollBar.LargeChange = Height / 10;
m_vScrollBar.Maximum = m_hScrollBar.Visible ? (curImgHeight - Height + m_hScrollBar.Height) : curImgHeight - Height;
m_vScrollBar.Maximum += m_vScrollBar.LargeChange - 1;
m_srcRect.Width = Width - m_vScrollBar.Width;
}
if (m_hScrollBar.Visible)
{
m_hScrollBar.Location = new Point(0, Height - m_hScrollBar.Height);
m_hScrollBar.Width = m_vScrollBar.Visible ? Width - m_vScrollBar.Width : Width;
m_hScrollBar.SmallChange = Width / 20;
m_hScrollBar.LargeChange = Width / 10;
m_hScrollBar.Maximum = m_vScrollBar.Visible ? (curImgWidth - Width + m_vScrollBar.Width) : curImgWidth - Width;
m_hScrollBar.Maximum += m_hScrollBar.LargeChange - 1;
m_srcRect.Height = Height - m_hScrollBar.Height;
}
m_dstRect.Width = m_srcRect.Width;
m_dstRect.Height = m_srcRect.Height;
if (m_dstRect.X > 0 && m_dstRect.Right > curImgWidth)
{
m_dstRect.X = curImgWidth - m_dstRect.Width;
if (m_dstRect.X < 0)
m_dstRect.X = 0;
}
if (m_dstRect.Y > 0 && m_dstRect.Bottom > curImgHeight)
{
m_dstRect.Y = curImgHeight - m_dstRect.Height;
if (m_dstRect.Y < 0)
m_dstRect.Y = 0;
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
CalcScrollSize();
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.FromArgb(0xcc, 0xcc, 0xcc));
if (m_bkScaledImg != null && m_dstRect.Width * m_dstRect.Height > 0)
{
e.Graphics.DrawImage(m_bkScaledImg, m_srcRect, m_dstRect, GraphicsUnit.Pixel);
}
}
public void SetImage(string _imgFile)
{
m_bkImg = Image.FromFile(_imgFile);
m_bkScaledImg = m_bkImg.Clone() as Image;
CalcScrollSize();
Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
m_ptStart = e.Location;
}
private Rectangle GetClientArea()
{
int w = m_vScrollBar.Visible ? (Width - m_vScrollBar.Width) : Width;
int h = m_hScrollBar.Visible ? (Height - m_hScrollBar.Height) : Height;
return new Rectangle(0, 0, w, h);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
int curImgWidth = m_bkScaledImg.Width;
int curImgHeight = m_bkScaledImg.Height;
m_ptEnd = e.Location;
//_currentObject?.ChangeSize(m_ptEnd);
int xoffset = m_ptEnd.X - m_ptStart.X;
int yoffset = m_ptEnd.Y - m_ptStart.Y;
m_dstRect.X -= xoffset;
m_dstRect.Y -= yoffset;
if (m_dstRect.X < 0) m_dstRect.X = 0;
if (m_dstRect.Y < 0) m_dstRect.Y = 0;
m_ptStart = e.Location;
var rc = GetClientArea();
if (curImgWidth - m_dstRect.X < rc.Width)
m_dstRect.X = curImgWidth - rc.Width;
if (curImgHeight - m_dstRect.Y < rc.Height)
m_dstRect.Y = curImgHeight - rc.Height;
if (m_dstRect.X < 0) m_dstRect.X = 0;
if (m_dstRect.Y < 0) m_dstRect.Y = 0;
m_vScrollBar.Value = m_dstRect.Y;
m_hScrollBar.Value = m_dstRect.X;
Invalidate();
}
}
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
if ((ModifierKeys & Keys.Control) == Keys.Control)
{
if (e.Delta < 0)
{
ScaleWith(5);
CalcScrollSize();
Invalidate();
}
if (e.Delta > 0)
{
ScaleWith(-5);
CalcScrollSize();
Invalidate();
}
}
}
public Image GetImage()
{
return m_bkImg;
}
public void AutoImage()
{
float r = Math.Min(Width * 1.0f / m_bkImg.Width, Height * 1.0f / m_bkImg.Height);
ScaleTo(r);
CalcScrollSize();
Invalidate(false);
}
public void ResetImage()
{
Restore();
CalcScrollSize();
Invalidate(false);
}
public void ScaleUpImage()
{
if (_scale < 400000)
{
ScaleWith(5);
CalcScrollSize();
Invalidate(false);
}
}
public void ScaleDownImage()
{
if (_scale > 5)
{
ScaleWith(-5);
CalcScrollSize();
Invalidate(false);
}
}
//public static Image Resize(this Image image, float scale)
//{
// int w = (int)(image.Width * scale);
// int h = (int)(image.Height * scale);
// Bitmap bmp = new Bitmap(w, h, image.PixelFormat);
// using (var g = Graphics.FromImage(bmp))
// {
// g.DrawImage(image, new Rectangle(0, 0, w, h), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
// }
// return bmp;
//}
private void ScaleWith(int val)
{
if (val < 0 && _scale > 500 || val > 0 && _scale < 4000000)
{
_scale += val * 100;
m_bkScaledImg?.Dispose();
m_bkScaledImg = m_bkImg.Resize(_scale / 10000.0f);
}
}
public void ScaleTo(float val, bool _invalidate = false)
{
_scale = (int)(val * 10000);
m_bkScaledImg?.Dispose();
m_bkScaledImg = m_bkImg.Resize(val);
if (_invalidate)
{
CalcScrollSize();
Invalidate(false);
}
}
private void Restore()
{
_scale = 10000;
m_bkScaledImg?.Dispose();
m_bkScaledImg = m_bkImg.Clone() as Image;
}
}
</code>
<code>public class DrawCanvas : Control
{
private int _scale = 10000;
private Image m_bkImg = null;
private Image m_bkScaledImg = null;
private Point m_ptStart;
private Point m_ptEnd;
private Rectangle m_dstRect;
private Rectangle m_srcRect;
private VScrollBar m_vScrollBar;
private HScrollBar m_hScrollBar;
public DrawCanvas()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
m_vScrollBar = new VScrollBar();
m_vScrollBar.Scroll += OnScroll;
m_hScrollBar = new HScrollBar();
m_hScrollBar.Scroll += OnScroll;
Controls.Add(m_vScrollBar);
Controls.Add(m_hScrollBar);
m_dstRect = new Rectangle();
m_srcRect = ClientRectangle;
}
public float ScaleValue { get => _scale / 100.0f; }
private void OnScroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
m_dstRect.Y = e.NewValue;
if (m_dstRect.Y < 0)
m_dstRect.Y = 0;
}
else if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
m_dstRect.X = e.NewValue;
if (m_dstRect.X < 0)
m_dstRect.X = 0;
}
Invalidate();
}
private void CalcScrollSize()
{
m_srcRect = ClientRectangle;
int curImgWidth = m_bkScaledImg == null ? 0 : m_bkScaledImg.Width;
int curImgHeight = m_bkScaledImg == null ? 0 : m_bkScaledImg.Height;
bool hVisiable = false;
bool vVisiable = false;
if (curImgHeight > Height)
vVisiable = true;
if (curImgWidth > Width)
hVisiable = true;
if (!vVisiable)
{
if (hVisiable)
{
vVisiable = curImgHeight > (Height - m_hScrollBar.Height);
}
}
if (!hVisiable)
{
if (vVisiable)
{
hVisiable = curImgWidth > (Width - m_vScrollBar.Width);
}
}
m_vScrollBar.Visible = vVisiable;
m_hScrollBar.Visible = hVisiable;
if (m_vScrollBar.Visible)
{
m_vScrollBar.Location = new Point(Width - m_vScrollBar.Width, 0);
m_vScrollBar.Height = m_hScrollBar.Visible ? Height - m_hScrollBar.Height : Height;
m_vScrollBar.SmallChange = Height / 20;
m_vScrollBar.LargeChange = Height / 10;
m_vScrollBar.Maximum = m_hScrollBar.Visible ? (curImgHeight - Height + m_hScrollBar.Height) : curImgHeight - Height;
m_vScrollBar.Maximum += m_vScrollBar.LargeChange - 1;
m_srcRect.Width = Width - m_vScrollBar.Width;
}
if (m_hScrollBar.Visible)
{
m_hScrollBar.Location = new Point(0, Height - m_hScrollBar.Height);
m_hScrollBar.Width = m_vScrollBar.Visible ? Width - m_vScrollBar.Width : Width;
m_hScrollBar.SmallChange = Width / 20;
m_hScrollBar.LargeChange = Width / 10;
m_hScrollBar.Maximum = m_vScrollBar.Visible ? (curImgWidth - Width + m_vScrollBar.Width) : curImgWidth - Width;
m_hScrollBar.Maximum += m_hScrollBar.LargeChange - 1;
m_srcRect.Height = Height - m_hScrollBar.Height;
}
m_dstRect.Width = m_srcRect.Width;
m_dstRect.Height = m_srcRect.Height;
if (m_dstRect.X > 0 && m_dstRect.Right > curImgWidth)
{
m_dstRect.X = curImgWidth - m_dstRect.Width;
if (m_dstRect.X < 0)
m_dstRect.X = 0;
}
if (m_dstRect.Y > 0 && m_dstRect.Bottom > curImgHeight)
{
m_dstRect.Y = curImgHeight - m_dstRect.Height;
if (m_dstRect.Y < 0)
m_dstRect.Y = 0;
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
CalcScrollSize();
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.FromArgb(0xcc, 0xcc, 0xcc));
if (m_bkScaledImg != null && m_dstRect.Width * m_dstRect.Height > 0)
{
e.Graphics.DrawImage(m_bkScaledImg, m_srcRect, m_dstRect, GraphicsUnit.Pixel);
}
}
public void SetImage(string _imgFile)
{
m_bkImg = Image.FromFile(_imgFile);
m_bkScaledImg = m_bkImg.Clone() as Image;
CalcScrollSize();
Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
m_ptStart = e.Location;
}
private Rectangle GetClientArea()
{
int w = m_vScrollBar.Visible ? (Width - m_vScrollBar.Width) : Width;
int h = m_hScrollBar.Visible ? (Height - m_hScrollBar.Height) : Height;
return new Rectangle(0, 0, w, h);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
int curImgWidth = m_bkScaledImg.Width;
int curImgHeight = m_bkScaledImg.Height;
m_ptEnd = e.Location;
//_currentObject?.ChangeSize(m_ptEnd);
int xoffset = m_ptEnd.X - m_ptStart.X;
int yoffset = m_ptEnd.Y - m_ptStart.Y;
m_dstRect.X -= xoffset;
m_dstRect.Y -= yoffset;
if (m_dstRect.X < 0) m_dstRect.X = 0;
if (m_dstRect.Y < 0) m_dstRect.Y = 0;
m_ptStart = e.Location;
var rc = GetClientArea();
if (curImgWidth - m_dstRect.X < rc.Width)
m_dstRect.X = curImgWidth - rc.Width;
if (curImgHeight - m_dstRect.Y < rc.Height)
m_dstRect.Y = curImgHeight - rc.Height;
if (m_dstRect.X < 0) m_dstRect.X = 0;
if (m_dstRect.Y < 0) m_dstRect.Y = 0;
m_vScrollBar.Value = m_dstRect.Y;
m_hScrollBar.Value = m_dstRect.X;
Invalidate();
}
}
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
if ((ModifierKeys & Keys.Control) == Keys.Control)
{
if (e.Delta < 0)
{
ScaleWith(5);
CalcScrollSize();
Invalidate();
}
if (e.Delta > 0)
{
ScaleWith(-5);
CalcScrollSize();
Invalidate();
}
}
}
public Image GetImage()
{
return m_bkImg;
}
public void AutoImage()
{
float r = Math.Min(Width * 1.0f / m_bkImg.Width, Height * 1.0f / m_bkImg.Height);
ScaleTo(r);
CalcScrollSize();
Invalidate(false);
}
public void ResetImage()
{
Restore();
CalcScrollSize();
Invalidate(false);
}
public void ScaleUpImage()
{
if (_scale < 400000)
{
ScaleWith(5);
CalcScrollSize();
Invalidate(false);
}
}
public void ScaleDownImage()
{
if (_scale > 5)
{
ScaleWith(-5);
CalcScrollSize();
Invalidate(false);
}
}
//public static Image Resize(this Image image, float scale)
//{
// int w = (int)(image.Width * scale);
// int h = (int)(image.Height * scale);
// Bitmap bmp = new Bitmap(w, h, image.PixelFormat);
// using (var g = Graphics.FromImage(bmp))
// {
// g.DrawImage(image, new Rectangle(0, 0, w, h), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
// }
// return bmp;
//}
private void ScaleWith(int val)
{
if (val < 0 && _scale > 500 || val > 0 && _scale < 4000000)
{
_scale += val * 100;
m_bkScaledImg?.Dispose();
m_bkScaledImg = m_bkImg.Resize(_scale / 10000.0f);
}
}
public void ScaleTo(float val, bool _invalidate = false)
{
_scale = (int)(val * 10000);
m_bkScaledImg?.Dispose();
m_bkScaledImg = m_bkImg.Resize(val);
if (_invalidate)
{
CalcScrollSize();
Invalidate(false);
}
}
private void Restore()
{
_scale = 10000;
m_bkScaledImg?.Dispose();
m_bkScaledImg = m_bkImg.Clone() as Image;
}
}
</code>
public class DrawCanvas : Control
{
private int _scale = 10000;
private Image m_bkImg = null;
private Image m_bkScaledImg = null;
private Point m_ptStart;
private Point m_ptEnd;
private Rectangle m_dstRect;
private Rectangle m_srcRect;
private VScrollBar m_vScrollBar;
private HScrollBar m_hScrollBar;
public DrawCanvas()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
m_vScrollBar = new VScrollBar();
m_vScrollBar.Scroll += OnScroll;
m_hScrollBar = new HScrollBar();
m_hScrollBar.Scroll += OnScroll;
Controls.Add(m_vScrollBar);
Controls.Add(m_hScrollBar);
m_dstRect = new Rectangle();
m_srcRect = ClientRectangle;
}
public float ScaleValue { get => _scale / 100.0f; }
private void OnScroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
m_dstRect.Y = e.NewValue;
if (m_dstRect.Y < 0)
m_dstRect.Y = 0;
}
else if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
m_dstRect.X = e.NewValue;
if (m_dstRect.X < 0)
m_dstRect.X = 0;
}
Invalidate();
}
private void CalcScrollSize()
{
m_srcRect = ClientRectangle;
int curImgWidth = m_bkScaledImg == null ? 0 : m_bkScaledImg.Width;
int curImgHeight = m_bkScaledImg == null ? 0 : m_bkScaledImg.Height;
bool hVisiable = false;
bool vVisiable = false;
if (curImgHeight > Height)
vVisiable = true;
if (curImgWidth > Width)
hVisiable = true;
if (!vVisiable)
{
if (hVisiable)
{
vVisiable = curImgHeight > (Height - m_hScrollBar.Height);
}
}
if (!hVisiable)
{
if (vVisiable)
{
hVisiable = curImgWidth > (Width - m_vScrollBar.Width);
}
}
m_vScrollBar.Visible = vVisiable;
m_hScrollBar.Visible = hVisiable;
if (m_vScrollBar.Visible)
{
m_vScrollBar.Location = new Point(Width - m_vScrollBar.Width, 0);
m_vScrollBar.Height = m_hScrollBar.Visible ? Height - m_hScrollBar.Height : Height;
m_vScrollBar.SmallChange = Height / 20;
m_vScrollBar.LargeChange = Height / 10;
m_vScrollBar.Maximum = m_hScrollBar.Visible ? (curImgHeight - Height + m_hScrollBar.Height) : curImgHeight - Height;
m_vScrollBar.Maximum += m_vScrollBar.LargeChange - 1;
m_srcRect.Width = Width - m_vScrollBar.Width;
}
if (m_hScrollBar.Visible)
{
m_hScrollBar.Location = new Point(0, Height - m_hScrollBar.Height);
m_hScrollBar.Width = m_vScrollBar.Visible ? Width - m_vScrollBar.Width : Width;
m_hScrollBar.SmallChange = Width / 20;
m_hScrollBar.LargeChange = Width / 10;
m_hScrollBar.Maximum = m_vScrollBar.Visible ? (curImgWidth - Width + m_vScrollBar.Width) : curImgWidth - Width;
m_hScrollBar.Maximum += m_hScrollBar.LargeChange - 1;
m_srcRect.Height = Height - m_hScrollBar.Height;
}
m_dstRect.Width = m_srcRect.Width;
m_dstRect.Height = m_srcRect.Height;
if (m_dstRect.X > 0 && m_dstRect.Right > curImgWidth)
{
m_dstRect.X = curImgWidth - m_dstRect.Width;
if (m_dstRect.X < 0)
m_dstRect.X = 0;
}
if (m_dstRect.Y > 0 && m_dstRect.Bottom > curImgHeight)
{
m_dstRect.Y = curImgHeight - m_dstRect.Height;
if (m_dstRect.Y < 0)
m_dstRect.Y = 0;
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
CalcScrollSize();
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.FromArgb(0xcc, 0xcc, 0xcc));
if (m_bkScaledImg != null && m_dstRect.Width * m_dstRect.Height > 0)
{
e.Graphics.DrawImage(m_bkScaledImg, m_srcRect, m_dstRect, GraphicsUnit.Pixel);
}
}
public void SetImage(string _imgFile)
{
m_bkImg = Image.FromFile(_imgFile);
m_bkScaledImg = m_bkImg.Clone() as Image;
CalcScrollSize();
Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
m_ptStart = e.Location;
}
private Rectangle GetClientArea()
{
int w = m_vScrollBar.Visible ? (Width - m_vScrollBar.Width) : Width;
int h = m_hScrollBar.Visible ? (Height - m_hScrollBar.Height) : Height;
return new Rectangle(0, 0, w, h);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
int curImgWidth = m_bkScaledImg.Width;
int curImgHeight = m_bkScaledImg.Height;
m_ptEnd = e.Location;
//_currentObject?.ChangeSize(m_ptEnd);
int xoffset = m_ptEnd.X - m_ptStart.X;
int yoffset = m_ptEnd.Y - m_ptStart.Y;
m_dstRect.X -= xoffset;
m_dstRect.Y -= yoffset;
if (m_dstRect.X < 0) m_dstRect.X = 0;
if (m_dstRect.Y < 0) m_dstRect.Y = 0;
m_ptStart = e.Location;
var rc = GetClientArea();
if (curImgWidth - m_dstRect.X < rc.Width)
m_dstRect.X = curImgWidth - rc.Width;
if (curImgHeight - m_dstRect.Y < rc.Height)
m_dstRect.Y = curImgHeight - rc.Height;
if (m_dstRect.X < 0) m_dstRect.X = 0;
if (m_dstRect.Y < 0) m_dstRect.Y = 0;
m_vScrollBar.Value = m_dstRect.Y;
m_hScrollBar.Value = m_dstRect.X;
Invalidate();
}
}
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
if ((ModifierKeys & Keys.Control) == Keys.Control)
{
if (e.Delta < 0)
{
ScaleWith(5);
CalcScrollSize();
Invalidate();
}
if (e.Delta > 0)
{
ScaleWith(-5);
CalcScrollSize();
Invalidate();
}
}
}
public Image GetImage()
{
return m_bkImg;
}
public void AutoImage()
{
float r = Math.Min(Width * 1.0f / m_bkImg.Width, Height * 1.0f / m_bkImg.Height);
ScaleTo(r);
CalcScrollSize();
Invalidate(false);
}
public void ResetImage()
{
Restore();
CalcScrollSize();
Invalidate(false);
}
public void ScaleUpImage()
{
if (_scale < 400000)
{
ScaleWith(5);
CalcScrollSize();
Invalidate(false);
}
}
public void ScaleDownImage()
{
if (_scale > 5)
{
ScaleWith(-5);
CalcScrollSize();
Invalidate(false);
}
}
//public static Image Resize(this Image image, float scale)
//{
// int w = (int)(image.Width * scale);
// int h = (int)(image.Height * scale);
// Bitmap bmp = new Bitmap(w, h, image.PixelFormat);
// using (var g = Graphics.FromImage(bmp))
// {
// g.DrawImage(image, new Rectangle(0, 0, w, h), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
// }
// return bmp;
//}
private void ScaleWith(int val)
{
if (val < 0 && _scale > 500 || val > 0 && _scale < 4000000)
{
_scale += val * 100;
m_bkScaledImg?.Dispose();
m_bkScaledImg = m_bkImg.Resize(_scale / 10000.0f);
}
}
public void ScaleTo(float val, bool _invalidate = false)
{
_scale = (int)(val * 10000);
m_bkScaledImg?.Dispose();
m_bkScaledImg = m_bkImg.Resize(val);
if (_invalidate)
{
CalcScrollSize();
Invalidate(false);
}
}
private void Restore()
{
_scale = 10000;
m_bkScaledImg?.Dispose();
m_bkScaledImg = m_bkImg.Clone() as Image;
}
}
Now there is a problem, add a picture to the control (larger than the client area of the control). Reduce the image to the approximate size of the control, drag the image to scroll the scroll bar to the far right, and then click the scroll bar right button to find that the image has moved,that’s a problen.
I tried to print some key information of pictures and scroll bars during various operations, but I haven’t found the problem yet.