【问题标题】:Drawing a line on a bitmap在位图上画一条线
【发布时间】:2025-12-07 03:30:01
【问题描述】:

我在 matlab 中做了一个人脸检测器,我正在将它翻译成 c# 代码,我一切都完成了。我主要使用

 System.Drawing.Bitmap b = new
        System.Drawing.Bitmap("C:*Location of file on computer*");

首先获得图像,在最后的步骤中我有这个代码

public static void ratio(System.Drawing.Bitmap b, Dictionary<int, List<int>> map)
    {
        double height=0;
        double width=0;


        foreach (KeyValuePair<int, List<int>> place in map)
        {
            height = place.Value[2] - place.Value[3];
            width = place.Value[0] - place.Value[1];

            if( ((height/width) >= 1) && ((height/width) <=  2 ) )
                draw(b, place, map);
        }
    }

    public static void draw(System.Drawing.Bitmap bmp, KeyValuePair<int, List<int>> place, Dictionary<int, List<int>> map)
    {
        // Create pen.
        Pen blackPen = new Pen(Color.Black, 3);
        // Create coordinates of points that define line.

        int x1 = place.Value[1];   //topleft to topright
        int y1 = place.Value[3];
        int x2 = place.Value[0];
        int y2 = place.Value[3];

        int x3 = place.Value[0];   //topright to bottomright
        int y3 = place.Value[3];
        int x4 = place.Value[0];
        int y4 = place.Value[2];

        int x5 = place.Value[0];   //bottomright to bottomleft
        int y5 = place.Value[2];
        int x6 = place.Value[1];
        int y6 = place.Value[2];

        int x7 = place.Value[1];   //bottomleft to topleft
        int y7 = place.Value[2];
        int x8 = place.Value[1];
        int y8 = place.Value[3];

        // Draw line to screen.
        using (var graphics = Graphics.FromImage(bmp))
        {
            graphics.DrawLine(blackPen, x1, y1, x2, y2);
        }

        using (var graphics = Graphics.FromImage(bmp))
        {
            graphics.DrawLine(blackPen, x3, y3, x4, y4);
        }

        using (var graphics = Graphics.FromImage(bmp))
        {
            graphics.DrawLine(blackPen, x5, y5, x6, y6);
        }

        using (var graphics = Graphics.FromImage(bmp))
        {
            graphics.DrawLine(blackPen, x7, y7, x8, y8);
        }

    }

在脸部周围绘制一个框。比率使用从连接组件标签获得的标签的边界来找到人脸的正确比率(我的数字只是编出来的)地图是包含标签编号的字典,以及 xmax、xmin、ymax 和 ymin 作为值.一切编译都没有错误,但是,我现在要做的是在脸部周围显示带有绘制框的所述图像,我不确定该怎么做

【问题讨论】:

    标签: c# bitmap drawing


    【解决方案1】:

    假设这是一个 Windows.Forms 应用程序,您只需将 PictureBox 控件从工具箱拖放到窗体上,将其 Dock 属性设置为 Fill,然后在代码中设置其 Image 属性:

    PictureBox1.Image = b;
    

    【讨论】:

    • 这是在一个新功能中还是我可以将此代码放在绘图功能中?
    • 您可以将代码插入到任何可以看到 PictureBox 和 Bitmap 变量的地方。不过,看起来你的代码目前都是静态的,这让我想知道你把它放在哪里以及你正在编写什么样的应用程序。
    • 好吧,我使用的是 windows 窗体,但我添加了一个名为 main 的新 source.cs,我的所有代码都是,我应该将我的代码复制到名为 form1.cs 的源中吗,因为我做了我按照你的建议做了,但它说图片框在当前上下文中不存在
    • 我收到错误“非静态字段、方法或属性“项目名称”.Form1.pictureBox1 需要对象引用”(为了清楚起见而更改)
    • 新建一个表单项目,在设计器中打开Form1并双击。这将带您进入表单的加载事件,您应该在其中放置问题中的代码。然后您将能够引用图片框
    【解决方案2】:

    在表单设计器中,在表单上放置一个PictureBox 控件,然后按您喜欢的方式定位和调整大小。如果您愿意(或有必要),您可以通过编程方式添加一个。

    然后,为表单的Load 事件添加一个事件处理程序,并在该方法中应用以下代码:

    System.Drawing.Bitmap b = new System.Drawing.Bitmap("C:*Location of file on computer*");
    pictureBox1.Image = b;
    

    那么,你的绘图方法可以变成:

    public static void draw(KeyValuePair<int, List<int>> place, Dictionary<int, List<int>> map)
    {
        // Create pen.
        Pen blackPen = new Pen(Color.Black, 3);
    
        // Create coordinates of points that define line.
        int x1 = place.Value[1];   //topleft to topright
        int y1 = place.Value[3];
        int x2 = place.Value[0];
        int y2 = place.Value[3];
    
        int x3 = place.Value[0];   //topright to bottomright
        int y3 = place.Value[3];
        int x4 = place.Value[0];
        int y4 = place.Value[2];
    
        int x5 = place.Value[0];   //bottomright to bottomleft
        int y5 = place.Value[2];
        int x6 = place.Value[1];
        int y6 = place.Value[2];
    
        int x7 = place.Value[1];   //bottomleft to topleft
        int y7 = place.Value[2];
        int x8 = place.Value[1];
        int y8 = place.Value[3];
    
        // Draw line to screen.
        using (Graphics g = Graphics.FromHwnd(pictureBox1.Handle))
        {
            g.DrawLine(blackPen, x1, y1, x2, y2);
            g.DrawLine(blackPen, x3, y3, x4, y4);
            g.DrawLine(blackPen, x5, y5, x6, y6);
            g.DrawLine(blackPen, x7, y7, x8, y8);
        }
    
        pictureBox1.Invalidate();
    }
    

    【讨论】:

    • 好的,我也有人脸检测的代码,我是把代码和这段代码放在form.cs中,还是放在实际的c#源文件中?
    • 两者都可以。如果你在谈论应该在它自己的类中的东西,那么把它做成一个单独的 *.cs 文件,然后从表单中调用类方法。
    • 我对表单不太熟悉,所以我不确定如何初始化这个“加载”和“偶数处理程序”