修复图形绘制区域大小

发布于 2024-11-10 00:15:01 字数 1315 浏览 1 评论 0原文

我刚刚开始学习用于绘制线条、圆等的 GDI+ 系统。我创建了一个组件(扫描仪),它继承了要在其上绘制的面板(不确定面板或图片框是否最好)。

我目前在“扫描仪”上画了一个圆圈。该组件可以添加到 winform 中,并且使用停靠将在 winform 调整大小时调整大小。目前我正在获取组件的大小来计算圆的大小,但我想做的基本上是说无论组件的大小如何,“画布”总是 300 x 300 宽,所以我可以说该圆应位于 25,25 处,尺寸为 250x250。

正如您可能从“扫描仪”名称中猜到的那样,我想在其上绘制点,但这些点将从中心 (150,150) 位置计算。

下面是我基本上绘制圆圈的代码。

非常感谢您对此的任何帮助。

public partial class Scanner : Panel
{
    public Scanner() {
        InitializeComponent();
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Draw(g);
        base.OnPaint(e);
    }
    protected override void OnResize(EventArgs e) {
        Graphics g = this.CreateGraphics();
        Draw(g);
        base.OnResize(e);
    }

    private void Draw(Graphics g) {
        g.Clear(Color.Black);
        g.PageUnit = GraphicsUnit.Pixel;
        Pen green = new Pen(Color.Green);
        Font fnt = new Font("Arial", 10);
        SolidBrush sb = new SolidBrush(Color.Red);

        int pos = (this.Width < this.Height ? this.Width : this.Height) / 2;
        int size = (int)(pos * 1.9);
        pos -= ((int)size / 2);
        g.DrawEllipse(green, pos, pos, size, size);
        g.DrawString(this.Width.ToString(), fnt, sb, new Point(0, 0));
    }
}

I'm just starting to learn the GDI+ system for drawing lines, circles etc. I've created a component (scanner) that inherits a Panel to draw on (not sure if panel or picture box is best).

On the "Scanner" Im currently drawing a circle on it. the component can be added to a winform and using docking will resize when the winform resizes. At the moment I'm getting the size of the component to calculate the size of the circle but what I want to do is basically say no matter what size the component is the "canvas" is always 300 x 300 wide, so I can say the circle should be positioned at 25,25 with a size of 250x250.

As you might guess from the name "Scanner" I want to plot points on it, but these will be calculated from the center (150,150) location.

Below is the code I have that basically draws the circle.

Many thanks for any help on this.

public partial class Scanner : Panel
{
    public Scanner() {
        InitializeComponent();
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Draw(g);
        base.OnPaint(e);
    }
    protected override void OnResize(EventArgs e) {
        Graphics g = this.CreateGraphics();
        Draw(g);
        base.OnResize(e);
    }

    private void Draw(Graphics g) {
        g.Clear(Color.Black);
        g.PageUnit = GraphicsUnit.Pixel;
        Pen green = new Pen(Color.Green);
        Font fnt = new Font("Arial", 10);
        SolidBrush sb = new SolidBrush(Color.Red);

        int pos = (this.Width < this.Height ? this.Width : this.Height) / 2;
        int size = (int)(pos * 1.9);
        pos -= ((int)size / 2);
        g.DrawEllipse(green, pos, pos, size, size);
        g.DrawString(this.Width.ToString(), fnt, sb, new Point(0, 0));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2024-11-17 00:15:01

根据您最近的评论,我了解您希望在固定大小的画布上进行绘图,并在控件内绘制该画布,其大小与控件适合的大小相同。

尝试下面的代码:

public class Scanner : Panel
{
    private Image _scanner;

    public Scanner()
    {
        this.SetStyle(ControlStyles.ResizeRedraw, true);

        CreateScanner();
    }

    private void CreateScanner()
    {
        Bitmap scanner = new Bitmap(300, 300);
        Graphics g = Graphics.FromImage(scanner);

        g.DrawEllipse(Pens.Green, 25, 25, 250, 250);

        g.Dispose();
        _scanner = scanner;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        int shortestSide = Math.Min(this.Width, this.Height);

        if (null != _scanner)
            e.Graphics.DrawImage(_scanner, 0, 0, shortestSide, shortestSide);
    }

}

Based on your recent comment, I understand you want to do your drawing on a fixed-size canvas, and plot this canvas inside the control, as large as will fit in the control.

Try the code below:

public class Scanner : Panel
{
    private Image _scanner;

    public Scanner()
    {
        this.SetStyle(ControlStyles.ResizeRedraw, true);

        CreateScanner();
    }

    private void CreateScanner()
    {
        Bitmap scanner = new Bitmap(300, 300);
        Graphics g = Graphics.FromImage(scanner);

        g.DrawEllipse(Pens.Green, 25, 25, 250, 250);

        g.Dispose();
        _scanner = scanner;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        int shortestSide = Math.Min(this.Width, this.Height);

        if (null != _scanner)
            e.Graphics.DrawImage(_scanner, 0, 0, shortestSide, shortestSide);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文