我在一个PictureBox
上用GraphicsPath
绘制了一个string
,并允许用户通过鼠标拖动在picturebox上移动该字符串。现在我想保存图像,使字符串恰好落在他在picturebox上拖动的位置。我尝试了以下方法:
float dx, dy;
private void closeWindow()
{
MessageBox.Show(NinjaClass.NINJA.location.ToString());
NinjaClass.NINJA.location = strPoint;//new Point(strPoint.X, strPoint.Y);
NinjaClass.NINJA.imgOpened = pictureBox1.Image;
Image tmp = Image.FromFile(NinjaClass.NINJA.ImgOrignalPath);
Graphics gr = Graphics.FromImage(tmp);
gr.DrawString(NinjaClass.NINJA.copyrightStr, NinjaClass.NINJA.font, new SolidBrush(NinjaClass.NINJA.color), new PointF(dx, dy));
NinjaClass.NINJA.imgOpened = tmp;
NinjaClass.NINJA.saveOpenedFile();
this.Close();
this.Dispose();
}
/////////////////////////////////////////////////////////////
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
gp.Transform(new Matrix(1, 0, 0, 1, dx, dy));//Translate and paint
e.Graphics.FillPath(new SolidBrush(color), gp);
gp.Transform(new Matrix(1, 0, 0, 1, -dx, -dy));//translate back (reset to old location)
}
//MouseDown event handler for your pictureBox1
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
strPoint = e.Location;
if (gp.GetBounds(new Matrix(1, 0, 0, 1, dx, dy)).Contains(e.Location))
{
gp.Transform(new Matrix(1, 0, 0, 1, dx, dy));
}
}
else if (e.Button == MouseButtons.Right)
{
pboxPoint = e.Location;
}
}
//MouseMove event handler for your pictureBox1
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
dx = e.X - strPoint.X;
dy = e.Y - strPoint.Y;
pictureBox1.Invalidate();
NinjaClass.NINJA.location = new PointF(strPoint.X, strPoint.Y);
}
else if(e.Button == MouseButtons.Right)
{
pictureBox1.Left += e.X - (int)pboxPoint.X;
pictureBox1.Top += e.Y - (int)pboxPoint.Y;
//NinjaClass.NINJA.location = pictureBox1.Location;
}
}
private void ScaleWindow_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == (char)Keys.Enter) || (e.KeyChar == (char)Keys.Space) || (e.KeyChar == (char)Keys.Escape))
{
this.closeWindow();
}
}
但我遇到了一些位置问题。我的意思是,保存的图像字符串与用户在picturebox上拖动的位置不同。
更新:这是保存图像的函数:
public void saveOpenedFile()
{
imgOpened.Save(imgSavePath);
MessageBox.Show("Saved as ' " + imgSavePath + " '");
}
saveOpenedFile()
在做这件事吗?那段代码在哪里?还有,new PointF(dx, dy)
的值从哪里来的? - DonBoitnottpictureBox1
目前处于CenterImage
大小模式。回复较晚,抱歉。 - A. K. M. Tariqul Islam