在图像上画圆 [英] Draw circle on the image

查看:90
本文介绍了在图像上画圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图用鼠标单击在图像上画一个小圆圈.但是,圆是从鼠标单击的点画出的.这是我的代码.

Am trying to draw a small circle on the image with a mouse click. But the circle is drawn away from the mouse clicked point. here is my code.

private void pictureBox1_MouseClick(object sender, MouseEventArgs e1)
        {
            string str = "O";
            if ((e1.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                Bitmap b = (Bitmap)pictureBox1.Image;
                pictureBox1.Image = b;
                Graphics g = Graphics.FromImage(b);
                p = new Point(e1.X, e1.Y);
                Pen p3 = new Pen(Color.Red, 1);
                this.Text = p.ToString();
                SizeF sizef = g.MeasureString(str, Font);
                f = new Font(new FontFamily("Times New Roman"), 10);
                g.DrawString(str, f, Brushes.Red, ((p.X)), ((p.Y)));
            }
            pictureBox1.Invalidate();
        }

推荐答案

您必须像已经测量过的那样测量字符串,但随后必须在鼠标单击坐标减去一半的位置绘制字符串如果要使字符串居中于鼠标单击的位置,则字符串的宽度应为左侧的宽度减去其高度的一半.

示例:

您单击x/y 100/200,并且字符串的宽度和高度为20像素.要在鼠标单击的坐标上居中放置字符串,请将其绘制到位置x:90和y:190


但是无论如何,正如其他人已经告诉您的那样,我宁愿自己画一个圆圈或十字架/X.


已修改:

试试这个:
You have to measure your string like you already did, but then you have to paint your string at the mouseclick''s coordinates minus half the string''s width to the left and minus half its height to the top, if you want to have the string centered to your mouseclick.

Example:

You click on x/y 100/200 and your string''s width and height is 20 pixels. To center the string on the mouseclick''s coordinates you have paint it to the location x:90 and y:190


But anyway, as the others told you already, I would prefer to paint a circle or the cross/X myself.


modified:

Try this:
f = new Font(new FontFamily("Times New Roman"), 10);
SizeF sizef = g.MeasureString(str, f);
g.DrawString(str, f, Brushes.Red, ((p.X-sizeF.widtdh/2)), ((p.Y-sizeF.heigth/2)));


您是否真的在画一个"O"字符串来画一个圆?

为什么不使用g.DrawEllipse()?
Are you actually drawing an "O" string to get a circle?

Why don''t you use g.DrawEllipse()?


您好,

您想让圆心到您在图片框上单击的位置吗?在这种情况下,使用字体会使问题复杂化,因为您不知道要绘制的字符串的高度和宽度.

确实有一些计算字体大小的方法(以像素为单位的高度和宽度),但是绘制圆形或十字形比较容易.您说绘制"X"很困难,但实际上并非如此,您所要做的就是用交换的坐标绘制2条线.

考虑一下,让我们知道您选择继续前进的方式,那么我们也许可以朝着代码示例的方向努力.

希望对您有所帮助:)问候
Hi there,

You want the center of the circle to the point where you clicked on the picture box right? In that case, using fonts is going to complicate your problem, since you do not know the height and width of a the character string you are going to draw.

There are indeed ways of calculating the font size (height and width in pixels) but it''s easier to draw a circle or a cross. You say drawing an "X" is hard, but actually it''s not, all you have to do is draw 2 lines with swapped co-ordinates.

Give it a thought and let us know what you choose to go forward with, then we might be able to work our way towards a code sample.

Hope this helps :) Regards


这篇关于在图像上画圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆