Note that there are some explanatory texts on larger screens.

plurals
  1. PODraw string on picturebox image
    primarykey
    data
    text
    <p>I draw a <code>string</code> by <code>GraphicsPath</code> on a <code>PictureBox</code> and I let users move the string on picturebox by mouse dragging. Now I want to save the image so that the string falls just on the place where he dragged it on the picturebox. I tried this:</p> <pre><code>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(); } } </code></pre> <p>But I'm facing some position problem. I mean the string of saved image gets different position than the position where user dragged it on the picturebox. </p> <p><strong>Update:</strong> Here is the function that saves the image:</p> <pre><code>public void saveOpenedFile() { imgOpened.Save(imgSavePath); MessageBox.Show("Saved as ' " + imgSavePath + " '"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload