Reputation: 2943
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
Upvotes: 1
Views: 851
Reputation: 65702
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();
}
Upvotes: 2
Reputation: 17346
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:
TextBox with a Transparent Background
According to these, transparency is not supported for TextBox
controls.
Upvotes: 1