1

I am developing an image editor in which I have implemented rectangle, lines and ellipse drawing functionality using mouse event and by using Graphics.DrawLine(), Graphics.DrawRectangle() and Graphics.DrawEllipse().

I was searching for writing text on image but could not find any solution, So what I mean is whenever click on image at any location the cursor will change (like writing text in textbox) and I can start typing on that location.

The Graphics.DrawString method is similar to what I am looking for but it does not support dynamic typing

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
user969068
  • 2,818
  • 5
  • 33
  • 64
  • What do you mean "does not support dynamic"???? – Alexei Levenkov Aug 24 '13 at 02:02
  • 1
    Side note: please avoid "thank you notes" in the body of your post and tags in the title. Edited out, feel free to discuss on [meta](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts) – Alexei Levenkov Aug 24 '13 at 02:04
  • I mean I have to write on top of image but that method accept only a string but I have to dynamically write ( like in notepad ) on top image. (And I will avoid those thanks again,... I am very novice in c#). – user969068 Aug 24 '13 at 02:07

2 Answers2

2

Alex Fr provided an excellent set of drawing tools in his DrawTools article and these tools serve as a basis for Draw Tool Redux.

I also use the Transparent Textbox from: http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo

To add the Textbox control to the Drawing Tools you need to make a class ToolText and DrawText.

In the ToolText class, I show a form "TextDialog" without border that has the textbox:

internal class ToolText : ToolObject
{
public ToolText()
{
    Cursor = new Cursor(GetType(), "Rectangle.cur");
}
public override void OnMouseDown(DrawArea drawArea, MouseEventArgs e)
{
    Point p = drawArea.BackTrackMouse(new Point(e.X, e.Y));
TextDialog td = new TextDialog();
td.Location = new Point(e.X, e.Y + drawArea.Top + td.Height);
    if (td.ShowDialog() ==
    DialogResult.OK)
    {
    string t = td.TheText;
    Color c = td.TheColor;
    Font f = td.TheFont;
    AddNewObject(drawArea, new DrawText(p.X, p.Y, t, f, c));
    }
}

Base the DrawText class off the DrawRectangle with a couple of properties, Text, Font, etc and for the Drawing Implementation:

public override void Draw(Graphics g)
{
    Pen pen = new Pen(Color);
    GraphicsPath gp = new GraphicsPath();
    StringFormat format = StringFormat.GenericDefault;
    gp.AddString(_theText, _font.FontFamily, (int)_font.Style, _font.SizeInPoints,
                    new PointF(Rectangle.X, Rectangle.Y), format);
    // Rotate the path about it's center if necessary
    if (Rotation != 0)
    {
        RectangleF pathBounds = gp.GetBounds();
        Matrix m = new Matrix();
        m.RotateAt(Rotation, new PointF(pathBounds.Left + (pathBounds.Width / 2), pathBounds.Top + (pathBounds.Height / 2)),
                    MatrixOrder.Append);
        gp.Transform(m);
    }
    g.DrawPath(pen, gp);
    rectangle.Size = g.MeasureString(_theText, _font).ToSize();
    pen.Dispose();
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thank you. I downloaded and noticed Draw Tools also using a text dialogue to get user text. I might go with this approach, shell give it a try soon. Thanks for reply – user969068 Aug 24 '13 at 22:52
  • Thank you all done, I used the idea to create a new form and ask userinput, I monitored click position on first form and inserted text from second Form on that position. Best for now untill we dont have any better option :) – user969068 Aug 25 '13 at 18:36
1

That's a very high-level functionality and it's not available as part of the .NET Framework. You'd have to implement that functionality using mouse event handlers and the DrawString() method. For example, when the user clicks the image, you can try creating a textbox with a transparent background (not sure if the transparency part is easy / possible) over the image and let the user type text. This wouldn't give you any formatting capabilities, though.

For full WYSIWYG editing, you'll have to either look for an existing component that does this or write the code from scratch.

Edit: Take a look at this similar questions:

transparent richTextBox

TextBox with a Transparent Background

According to these, transparency is not supported for TextBox controls.

Community
  • 1
  • 1
xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • I was actually already looking for this solution ( transpaernt textbox ) but it seems it is also not supported and it failed to transparent. But then i found that solution above you posted which I think only support with usercontrol which I dont have in my toolbox. http://stackoverflow.com/questions/5557365/textbox-with-a-transparent-background . Thanks for reply – user969068 Aug 24 '13 at 02:14