-2

I have some problems. Please help me. Here is my outline

I tried this code

Graphics g = Graphics.FromImage(bmp);
g.DrawString(yazi,
   new Font("Tahoma",Convert.ToInt32(txtboyut.Text)),
   new SolidBrush(renk), 50,50);
pictureBox1.Image = bmp;

and I want to move this drawString, please help me

AGB
  • 2,230
  • 1
  • 14
  • 21
jackquin
  • 534
  • 1
  • 7
  • 19

1 Answers1

0

Looking at your form I can guess what you want and what you are missing at the moment..

Your code:

g.DrawString(someText, someFont, someBrush, 50,50);

will always print to a fixed location. (And it doesn't even show :-()

So the first thing to do is to create a class level variable to hold the location:

Point textLocation = new Point(-123, -321);

For cleaner code let's add one more:

readonly Point Invalid = new Point(-123, -321);

Later we want to test if the location is valid but a Point is a structure and can't be null and while there is a Point.Empty this really is (0,0), which makes it a quite normal value.. So we use a magic number but only here and with a helpful name ;-)

Now we can apply the text using the new variable:

private void cb_ApplyText_Click(object sender, EventArgs e)
{
    Graphics g = Graphics.FromImage(pictureBox1.Image);
    g.DrawString(textBox1.Text,
        new Font("Tahoma", Convert.ToInt32(numericUpDown1.Value)),
        new SolidBrush(foreColor), textLocation);
    textLocation = Invalid;
    pictureBox1.Refresh();
}

Note that I get my font size from a NumericUpDown.

More interesting: By calling Refresh we nudge the PictureBox to show the changes in the Image..

Also note how I set the testLocation variable to Invalid, so we do not draw it onto the surface after having drawn in into the image pixels..

Now for the real issue: How to make the location dynamic. For this you need to code the MouseMove event:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (!e.Button.HasFlag(MouseButtons.Left)) return;
    textLocation = e.Location;
    pictureBox1.Invalidate();
}

Note the Invalidate call: This triggers the Paint event, which we need to draw the text at the current mouse position:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (textLocation != Invalid)
        e.Graphics.DrawString(textBox1.Text,
            new Font("Tahoma", Convert.ToInt32(numericUpDown1.Value)),
            new SolidBrush(foreColor), textLocation);

}

Nothing special here except the check for a valid text location.

Now while holding the mouse button down you can move the text and when its position is ok you apply it with the button.

Note one more potential problem: The two DrawString calls use two different Graphics objects and this will only work if both have the same resolution. e.Graphics will have the resolution of the screen but an image you load can have any resolution or even none..

The simplest way to avoid problems here is to change the Image's resolution until saving it; then reset to the values you have to store after the load..

Update: Here is an example of a versatile save method that let's you pick various file formats:

private void cb_Save_Click(object sender, EventArgs e)
{
    SaveFileDialog sfd = new SaveFileDialog();
    // .. add more formats if you want here!
    sfd.Filter = "Jpg|*.jpg|Jpeg|*.jpeg|PNG|*.png";
    ImageFormat format = ImageFormat.Jpeg;
    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        if (sfd.FileName.ToLower().EndsWith(".png")) format = ImageFormat.Png;
        // ..and here!
    }
    ((Bitmap)pictureBox1.Image).SetResolution(ImgDpi.X, ImgDpi.Y);
    pictureBox1.Image.Save(sfd.FileName, format); 
}
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Thank you for your helping these codes helped me. I can move the text but i have one more question. I want to save it latest picture i'm using this code saveFileDialog1.ShowDialog(); pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Jpeg); i can save the picture but i cant save the text. – jackquin Apr 12 '16 at 18:00
  • Did you apply the text with the `cb_ApplyText_Click` button? This should save the Image incuding the pixels it has written into the Image.. – TaW Apr 12 '16 at 18:20
  • Yes i can apply the text over the picturebox but when i save the picture to desktop i cant see the text over the picture. note: I can see the text over the picture at the running time. – jackquin Apr 12 '16 at 18:54
  • I want to save picture to file with the text. – jackquin Apr 12 '16 at 18:56
  • I really don't know what else to say. The code does just that. You can move the text and, indeed, when you now save the text is not saved. __you need to call the apply text code__. If you do the text will be saved. does it actually get called??? (Set a breakpoint to it and test if it gets hit! If it isn't then you have not [hooked it up](http://stackoverflow.com/questions/36377726/visual-effect-on-a-mouse-click-in-c-sharp/36378219?s=1|0.7055#36378219) – TaW Apr 12 '16 at 19:21
  • i can write text like this : http://hizliresim.com/o3mkBb after i want to save picture with the text but i cant see text : http://i.hizliresim.com/ljDWnJ.png – jackquin Apr 12 '16 at 19:32
  • Yes, but please answer my questions! Does the code in the `cb_ApplyText_Click` get called? Did you include it?? And did you hook it up??? – TaW Apr 12 '16 at 19:34
  • I'm using cb_apply_text when i press the "Write Text On PictureBox" button is that wrong ? – jackquin Apr 12 '16 at 19:40
  • Well, that sounds right. Does it really get called? Does the breakpoint get hit?? – TaW Apr 12 '16 at 19:46
  • i'm recording a video. You will see what happend exactly. – jackquin Apr 12 '16 at 19:56
  • Why so complicated? What I need to know is simple: Does the code in the cb_ApplyText_Click event run? Maybe you don't know how to work with breakpoints? – TaW Apr 12 '16 at 19:59
  • yes cb_apply_text is running i try with breakpoint. Code is running but when i click button second time text location and text size changing – jackquin Apr 12 '16 at 20:04
  • Hm, it works just fine here. You did adapt my textBox1 and my numUpDown1 to your controls, right? Can you place several different texts on different places? You do have different buttons for Apply and for Save, right? – TaW Apr 12 '16 at 20:08
  • I tried to set the text difrent location after click the apply button, location again changed :( – jackquin Apr 12 '16 at 20:14
  • i think this problem because of invalid textlocation :( – jackquin Apr 12 '16 at 20:15
  • textLocation should be reset to a valid value in the MouseMove.. - Hm, maybe you can upload your code somewhere so I can have a look.. – TaW Apr 12 '16 at 20:17
  • https://www.wetransfer.com/downloads/7382006559c45657ca3eb209b88d374220160412202555/d8907f you can download my full source code – jackquin Apr 12 '16 at 20:27
  • Your code basically works; it does have two errors, however. The one you don't see is that you don't use the color you have set in the apply code. Change `new SolidBrush(ForeColor)` to `new SolidBrush(renk)` - The reason why nothing seems to get saved is probably that you forgot to add the proper file extension. I have added an example that makes use of the Filter property in the FileSaveDialog to the answer.. – TaW Apr 12 '16 at 20:43
  • can you change the wrong lines and the upload wetransfer ? i changed new solidbrush(renk) but nothing change :( – jackquin Apr 12 '16 at 20:52
  • Did you also pick up the save code?? – TaW Apr 12 '16 at 20:52
  • it would be great :) – jackquin Apr 12 '16 at 20:57
  • It is in the updated answer. Also note that you don't set useful intial values for your variables, neither the text, pick it up in the `MouseMove` or in the `TextChanged`!. Also the Color, set a nice intial value: `Color renk = Color.Pink;` ;-) - And pick up the font size as well; best place it also in a variable.. – TaW Apr 12 '16 at 21:01
  • did you try it on my source code ? – jackquin Apr 12 '16 at 21:11
  • Yes. It works if you set a valid extension. Do note my remarks about the resolution! Your included image has 72dpi. My monitor has 120dpi. This changes size and position of the text.. – TaW Apr 12 '16 at 21:20
  • thank you so much. Everything running verry good thank you a so much – jackquin Apr 12 '16 at 21:43