我在string
上通过GraphicsPath
绘制了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();
}
}
但我面临一些立场问题。我的意思是保存的图像字符串的位置与用户在图片框上拖动它的位置不同。
更新:以下是保存图片的功能:
public void saveOpenedFile()
{
imgOpened.Save(imgSavePath);
MessageBox.Show("Saved as ' " + imgSavePath + " '");
}
答案 0 :(得分:0)
在方法closeWindow
中,我想知道为什么要将NinjaClass.NINJA.imgOpened
的值设置两次。我认为你必须对 pictureBox1 中的图像进行更改,而不是加载到 tmp 对象的原始图像。
你可以尝试这段代码吗?
float dx, dy;
private void closeWindow()
{
MessageBox.Show(NinjaClass.NINJA.location.ToString());
NinjaClass.NINJA.location = strPoint;//new Point(strPoint.X, strPoint.Y);
Graphics gr = Graphics.FromImage(pictureBox1.Image);
gr.DrawString(NinjaClass.NINJA.copyrightStr, NinjaClass.NINJA.font, new SolidBrush(NinjaClass.NINJA.color), new PointF(dx, dy));
NinjaClass.NINJA.imgOpened = pictureBox1.Image;
NinjaClass.NINJA.saveOpenedFile();
this.Close();
this.Dispose();
}