Reputation: 141
I want to draw a text within an image in C# by using the method drawstring.But how can i obtain the co-ordinates of the clicked position and how can i relate this to the dimension of the image. Please Help.
Upvotes: 2
Views: 1439
Reputation: 81675
Working example:
private Bitmap _bmp = new Bitmap(250, 250);
public Form1()
{
InitializeComponent();
panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
panel1.Paint += new PaintEventHandler(panel1_Paint);
using (Graphics g = Graphics.FromImage(_bmp))
g.Clear(SystemColors.Window);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(_bmp, new Point(0, 0));
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
using (Graphics g = Graphics.FromImage(_bmp))
{
g.DrawString("Mouse Clicked Here!", panel1.Font, Brushes.Black, e.Location);
}
panel1.Invalidate();
}
You might want to use TextRenderer instead of DrawString. DrawString has issues.
TextRenderer.DrawText(g, "Mouse Clicked Here!", panel1.Font, e.Location, Color.Black);
Upvotes: 1
Reputation: 4702
If I was doing this, I'd follow this guy's tutorial;
http://www.emanueleferonato.com/2006/09/02/click-image-and-get-coordinates-with-javascript/
and in his function, generate an ajax post-back with the co-ordinates in the querystring. Then do whatever you need to do with them. You would be better off using client side scripting for this sort of thing though.
Upvotes: 0