所以我正在实现一个可以读取图像的项目,平移它,缩放它并做其他事情......一切都很顺利,直到我尝试用鼠标右键实现绘图。
问题是当我画一条线时,图像上出现的线与我在屏幕上画的线不对应,这意味着它发生了移动,并且我知道它是因为图像的大小和缩放而改变的,但是当我绘制时图像上的线条以其原始尺寸(图像)和平移;我没有问题。
这是代码。
首先这是我单击浏览并选择图像时加载图像的方式
Myimage = new Bitmap(ImagePath);
resized = myImage.Size;
imageResize();
pictureBox.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox_Paint);
pictureBox.Invalidate();
imageResize 函数执行以下操作:
void imageResize()
{
//calculated the size to fit the control i will draw the image on
resized.Height = someMath;
resized.Width = someMath;
}
然后在 pictureBox_Paint 事件的事件处理程序中我写道:
private void pictureBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Create a local version of the graphics object for the PictureBox.
Graphics PboxGraphics = e.Graphics;
PboxGraphics.DrawImage(myImage, imageULcorner.X, imageULcorner.Y, resized.Width, resized.Height);
}
正如您所看到的,调整后的尺寸不是原始图像尺寸,我这样做是因为我希望图像集中显示在图片框控件上并填充,现在下一部分是我的问题开始的地方
我必须使用鼠标右键在图像上画线,所以我实现了 pictureBox_MouseDown 和 pictureBox_MouseUp 事件处理程序
// mouse down event handler
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
else if (mouse.Button == MouseButtons.Right)
{
mouseDown = mouse.Location;
mouseDown.X = mouseDown.X - imageULcorner.X;
mouseDown.Y = mouseDown.Y - imageULcorner.Y;
draw = true;
}
}
这是鼠标按下事件处理程序
//Mouse UP
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
else if (mouse.Button == MouseButtons.Right)
{
if (draw)
{
mouseLocationNow.X = mouse.X - imageULcorner.X;
mouseLocationNow.Y = mouse.Y - imageULcorner.Y;
//
// get graphics object of the image ( the original not the resized)
// as the resized image only appears when i draw on the graphics of the
// pictureBox control
// i know the problem lies here but how can i fix it
//
Graphics image = Graphics.FromImage(myImage);
Pen pen = new Pen(Color.Red, 2);
image.DrawLine(pen, mouseLocationNow, mouseDown);
pictureBox.Invalidate();
}
draw = false;
}
所以最后我希望能够在调整大小的图像上绘图并使其对应于真实图像以及我绘制线条的屏幕
感谢并抱歉发了这么长的帖子,但这个问题一直让我发疯。