简体   繁体   English

C#:调整图像大小时缩放图形的问题

[英]C# : Issue with scaling graphics with when image is resized

I'd have a program that will allow you to draw lines over an image which will eventually be used for calculating distance . 我有一个程序,允许您在图像上画线,最终将用于计算距离

To make things simple, my current image (which is in a PictureBox) is an image of a ruler. 为简单起见,我当前的图像(在PictureBox中)是标尺的图像。 When you left click, a path is created and drawn. 当您单击鼠标左键时,将创建并绘制路径。

Originally, to zoom in, I had it so that a new bitmap would be created with the images new size and I was able to use Graphics.ScaleTransform and it worked fine but it would just crop the image. 最初,为了进行放大,我使用了新的位图,并使用了新的图像大小,并且能够使用Graphics.ScaleTransform ,并且效果很好,但只裁剪了图像。

I needed the image to actually change width and height so now what I'm doing is just adding/subtracting a constant zoom amount to the width & height when zooming in/out. 我需要图像实际更改宽度和高度,所以现在我正在做的只是在放大/缩小时向宽度和高度添加/减去恒定的缩放量。

With this approach, I can't seem to scale the graphics and the paths are skewed into different directions and not the right size when the image is zoomed in. I completely understand why this is happening, because the image is getting larger and the graphics are staying the same, I just need whatever math is required to scale the graphics. 使用这种方法,我似乎无法缩放图形,并且在放大图像时,路径会偏向不同的方向,而不是正确的大小。我完全理解为什么会发生这种情况,因为图像越来越大且图形保持不变,我只需要缩放图形所需的任何数学运算即可。

I've tried using Graphics.ScaleTransform as well as moving the graphics x & y to their current position + the current zoom amount (offset) 我尝试使用Graphics.ScaleTransform以及将图形x和y移动到其当前位置+当前缩放量(偏移)

按照@TaW的指示,我更改了缩放功能,以根据所应用的任何缩放来计算新的宽度和高度,然后创建一个新的位图,其中包含具有新宽度和高度的原始图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM