在picturebox图像上绘制字符串。

3

我在一个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 + " '");
}

我已经编辑了你的标题。请参考“问题的标题应该包含“标签”吗?”,在那里达成共识是“不应该”。 - John Saunders
图片框的SizeMode是什么?它可能参与其中! - Sriram Sakthivel
saveOpenedFile()在做这件事吗?那段代码在哪里?还有, new PointF(dx, dy) 的值从哪里来的? - DonBoitnott
请查看更新。pictureBox1 目前处于 CenterImage 大小模式。回复较晚,抱歉。 - A. K. M. Tariqul Islam
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接