We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Putting text on an image?

mykyl
mykyl asked
on
Medium Priority
335 Views
Last Modified: 2010-04-06
I can get text onto an image. But I want to label any edits that I make but when I try to go to another edit and label it the previous label disapears. Can anyone help please?

Here is the code I am Using.

procedure TMapEditor.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
textdialog.showmodal;
Mapeditor.Canvas.TextOut(x,y, textdialog.edit1.text);
end;
end;
Comment
Watch Question

Hello Mykyl,
  What exactly are you trying to do? How do you want to label edits? What is the purpose of textdialog? It will pop up every time there is a mouse up. Then, if MapEditor is your form, the text may be covered by the image. I tried a project with your code, it works fine for me. What is the problem?

Author

Commented:
Ill try to explain.
I have an image of a map on screen. I click on any point on the image and a small marker circle appears on the image at this point on mouseclickup a dialog box opens up for the user to enter text to label the circle. This works so far. Now I want to click on another point on the image I mouseclickdown the marker circle appears but the previous labelled marker circle's label disappears. What I want to do is label all marker circles as I go along.
I hope you can understand what Im asking and can help.
Thank you.
Hi Mykyl,

  Your problem lies elsewhere. Step back from the project and try to find other causes. The code you supplied works perfectly in conjunction with the events you are using:

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  textdialog.showmodal;
  Image1.Canvas.TextOut(X, Y, textdialog.edit1.text);
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Image1.Canvas.Ellipse(X, Y, (X+50), (Y+50));
end;

  This code works, it does just what you want. Show me what you are doing OnMouseDown, you have probably made a small error there.

Edo

Author

Commented:
Here is the code for mouse down.

procedure TMapEditor.Image1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
Clipboard.Assign(Image1.Picture);{make a copy for undo}
  if Button = mbLeft then
  begin
    if ssShift in Shift then
      Image1.Canvas.Rectangle (X-Radius, Y-Radius,
        X+Radius, Y+Radius)
    else
      Image1.Canvas.Ellipse (X-Radius, Y-Radius,
        X+Radius, Y+Radius);
    Changed := True;
  end;
end;
Hi Mykyl,

Here is what I tested:

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Clipboard.Assign(Image1.Picture);

  if Button = mbLeft then
    Image1.Canvas.Ellipse (X-Radius, Y-Radius, X+Radius, Y+Radius);

end;

  I have had no problems with this code. Your problem is generated from somewhere else. Trace into the code, because something else is preventing the label from staying where it should. I have to go now (2pm eastern time GMT - 5) but would like to help you out. If I were you, I would create a new application for the purposes of debugging what you have. Add the code we have here, and make sure it works. Then, add one element at a time of what you have, and keep testing. You should find the problem this way. Just use any bitmap, and keep your testing code as simple as possible, while maintaining a parallel with what you are attempting to do. If that doesn't appeal to you, put on your hunting hat, and trace into what you've got. Set breakpoints where you expect the code to go and check your values.

I will check out your progress around 9 pm.

Author

Commented:
Im almost too embarrased to tell you what was wrong.

Mapeditor.Canvas.TextOut(x,y, textdialog.edit1.text); is what I was putting but It should have been

IMAGE1.Canvas.TextOut(x,y, textdialog.edit1.text);

What an idiot Ive Been. I couldnt see the wood for the trees as they say here.
Any way Is there a way to get the label to have an invisible background so that only text is showing up not text with a border.
Thank you.
It does not matter if you cant or dont want to answer this bit as Ill send you the points anyway.
Mike

Hi Mike,
  I have programmed a fair amount, and let me tell you that seeing the wood for the trees is a MAJOR source of bugs. You would laugh at bugs that I and co-workers have had. Yes, sometimes you just want to hit yourself. Just remember, when you have a bug like this (i.e. not a 'flow of logic' (flaw of logic!) style of bug), the best thing to do (at least for me) is to take a coffee break(!). Don't think about your bug. After a minimum of 10 minutes, go back and scrutinize each line of code, especially where 'the action' should be taking place. It's a way of increasing your scope. I think of it like horses running a race. They wear blinders towards their goal, and sometimes as programmers looking at specifics wear them too. The trick is knowing how to take them off. Another good technique is having fresh eyes: get a friend to look at what you are doing. Explain each line of your code to them. You will be amazed to see how often a friend can spot the problem area! Anyway, enough rambling from this peanut gallery.
  So... you want a label with a 'clear' background. Well, that's not such a tall order, but it will require more than just setting a property. In fact, what we should do is create a customized label. Specifically, we should derive a new component from TCustomLabel and override the paint method. Then, in MapEditor (Image1, I mean ;) ) we should create instances of this new type and place them at the given x and y coordinates. The other benefit of creating our own component is that we might be able to include some other helpful behaviour and encapsulate it within our new control.  I will try and make the clear label for tomorrow. You should think about what else the label might need as extra behavior. What exactly is the label labelling? Is there anything else that would be cool for the label to be able (gotta love the alliteration) to do?

Regards,

Edo
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
The label I was meaning is the text label on the image. At the moment after putting the circle marker and entering the text the background of the text is the same colour as the circle. I would prefer it to be transparent.

Any ideas??

Thank you
Mike
Hi Mike,
  Thanks for the points.
  If you want to control more closely the color of the text, just change the Label.Color to be the same. I would also suggest using a TShape instead of letting Windows paint for you. This means that you can access the color by means of Shape.Brush.Color. And, you'll have access to the object later later on: you might change the color or respond to click events.... I think that using TShape and TLabel is the way to go. That way it will be something you can do stuff with, and not be just a mark on your canvas. Did you know you can even drag them? Check out the properties of both objects and let your imagination run wild!
  Have fun, and let me know how you are doing. We can communicate through this thread, or through egarson@hotmail.com if you prefer.

Edo
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.