我想使用矩阵对点进行简单的缩放。我尝试了几种方法,例如使用zoomfactor缩放矩阵,但它只能缩放到左上角(0,0)。
如何通过鼠标将其缩放到任意给定位置?
那是我有atm的代码。
private void DrawToBitmap()
{
Brush b = new SolidBrush(Color.White);
graphics.Clear(pic.BackColor);
graphics.SmoothingMode = SmoothingMode.HighQuality;
float width = maxX - minX; //8000 units wide
float height = maxY - minY;//8000 units high
transform = new Matrix();
transform.Scale(bmp.Width / width, bmp.Height / height);
transform.Translate(-minX, -minY, MatrixOrder.Prepend);
graphics.Transform = transform;
//Works fine until now
}
// using this method to draw the image of the bmp to the picturebox
private void Pic_Paint(object sender, PaintEventArgs e)
{
DrawToBitmap();
Graphics g = e.Graphics;
g.DrawImage(bmp, 0, 0);
}
//that's what I've already gathered from the internet...it works fine
when i define a rectangle inside the Pic_Paint Method(just like the code
example shows below) and call draw
rectangle. It zooms in at the desired point just like i want it, but it
doesn't work with the DrawToBitmap method... to be totally honest I'm
pretty overtaxed when it comes to matrices since i've never used them
private void Pic_Paint(object sender, PaintEventArgs e)
{
//Graphics g = e.Graphics;
//g.Transform = transform;
//Pen mypen = new Pen(Color.Red, 5);
//Rectangle rect = new Rectangle(10, 10, 30, 30);
//e.Graphics.DrawRectangle(mypen, rect);
}
protected override void OnMouseWheel(MouseEventArgs mea) //changed
{
pic.Focus();
if (pic.Focused == true && mea.Delta !=0 )
{
Point picPoint =
pic.PointToClient(this.PointToScreen(mea.Location));
ZoomScroll(picPoint, mea.Delta > 0);
}
private void ZoomScroll(Point location, bool zoomIn)
{
float newScale = Math.Min(Math.Max(zoomingfactor + (zoomIn ?
scrollvalue : -scrollvalue), 0.1f), 10);
if (newScale != zoomingfactor)
{
float adjust = newScale / zoomingfactor;
zoomingfactor = newScale;
transform.Translate(-location.X, -location.Y,
MatrixOrder.Append);
transform.Scale(adjust, adjust, MatrixOrder.Append);
transform.Translate(location.X, location.Y,
MatrixOrder.Append);
pic.Invalidate();
}
}
可能要知道的重要一点是,我使用计时器不断调用DrawBitmap(),该对象通过图形绘制对象的位置。FillEllipse(...)
如果能帮助我,将非常感谢,请随时询问更多信息!