【问题标题】:Draw string on picturebox image在图片框图像上绘制字符串
【发布时间】:2013-12-03 16:48:03
【问题描述】:

我在PictureBox 上通过GraphicsPath 绘制string,并让用户通过鼠标拖动来移动图片框上的字符串。现在我想保存图像,以便字符串正好落在他在图片框上拖动它的地方。我试过这个:

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 + " '");
}

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 图片框的SizeMode是什么?那可能会参与其中!
  • saveOpenedFile() 这样做了吗?那个代码在哪里?这个值来自哪里:new PointF(dx, dy)?
  • 请查看更新。 pictureBox1 当前处于 CenterImage SizeMode。抱歉回复晚了。

标签: c# winforms graphics


【解决方案1】:

在方法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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 2013-05-14
    • 1970-01-01
    相关资源
    最近更新 更多