C# 在位图上绘制直线

C# 在位图上绘制直线,c#,bitmap,drawing,C#,Bitmap,Drawing,我在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<

我在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);
        }

    }
公共静态空隙率(System.Drawing.Bitmap b,字典图)
{
双倍高度=0;
双倍宽度=0;
foreach(KeyValuePair在地图中的位置)
{
高度=位置值[2]-位置值[3];
宽度=位置值[0]-位置值[1];

如果(((高度/宽度)>=1)&&((高度/宽度)假设这是一个Windows.Forms应用程序,您只需从工具箱中将PictureBox控件放到窗体上,将其Dock属性设置为Fill,并在代码中设置其Image属性:

PictureBox1.Image = b;

在表单设计器中,在表单上放置一个
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();
}
publicstaticvoiddraw(KeyValuePair位置,字典映射)
{
//创建钢笔。
钢笔黑色钢笔=新钢笔(颜色:黑色,3);
//创建定义直线的点的坐标。
int x1=place.Value[1];//从左上到右上
int y1=位置值[3];
int x2=位置值[0];
int y2=位置值[3];
int x3=place.Value[0];//从右上到右下
int y3=位置值[3];
int x4=place.Value[0];
int y4=位置值[2];
int x5=place.Value[0];//从右下角到左下角
int y5=位置值[2];
int x6=place.Value[1];
int y6=位置值[2];
int x7=place.Value[1];//从左下到左上
int y7=位置值[2];
int x8=place.Value[1];
int y8=位置值[3];
//在屏幕上画线。
使用(Graphics g=Graphics.FromHwnd(pictureBox1.Handle))
{
g、 抽绳(黑笔,x1,y1,x2,y2);
g、 抽绳(黑笔、x3、y3、x4、y4);
g、 抽绳(黑笔、x5、y5、x6、y6);
g、 抽绳(黑笔、x7、y7、x8、y8);
}
pictureBox1.Invalidate();
}

这是在一个新函数中,还是我可以将此代码放在draw函数中?您可以将代码插入任何具有PictureBox和Bitmap变量可视性的位置。不过,目前您的代码似乎都是静态的,这让我想知道您将其放在何处以及您正在编写什么样的应用程序。好吧,我有点不知所措ows表单,但我添加了一个名为main的新source.cs,如果我所有的代码都是,我是否应该将我的代码复制到名为form1.cs的源代码中,因为我做了一个修改,我按照你的建议做了,但它说picturebox在当前上下文中不存在我得到错误“项目名称的非静态字段、方法或属性需要对象引用”“.Form1.pictureBox1”(仅为清晰起见对其进行了更改)创建一个新的表单项目,在designer中打开Form1并双击它。这将带您进入表单的加载事件,您应该在其中放置问题中的代码。然后您将能够引用图片框OK,这样我也有人脸检测的代码,我是将该代码和该代码放在form.cs中,还是将其放在a中实际的c#源文件?两者都可以。如果你说的是应该在它自己的类中的东西,那么把它变成一个单独的*.cs文件,然后从表单中调用类方法。我对表单不太熟悉,所以我不确定如何初始化这个“加载”和“甚至处理程序”