在C#中的某些图片框上画一条线

时间:2013-08-08 13:42:00

标签: c# line draw

我正在用C#开发一个新的应用程序,我需要一些帮助。

我想做到这一点:画一条线。更具体地说,有一些图片框我用代码生成它们,并且在应用程序开始时它们不可见。

这是一个例子,如果我想要的话:

Enter image description here

我想要的是这样的:

Enter image description here

我用于绘制第一行的代码是:

Graphics g;
Pen p;

void drawline(){
   g = this.CreateGraphics();
   p = new System.Drawing.Pen(Color.Red, 15);
   g.DrawLine(p, new Point(10, 150), new Point(900, 150));
}

PS:我很抱歉第二行(它是在Paint :)中绘制的)

2 个答案:

答案 0 :(得分:1)

您可以在绘制线条之前在图形上绘制图像,而不是在图像上放置图片框。

g.DrawImage(image, point)

也许这会导致什么?

    Bitmap _bitmap = new Bitmap(PictureBox1.Width, PictureBox1.Height);

    Dictionary<int, Image> _images = new Dictionary<int, Image>();
    System.Drawing.Pen _redPen = new System.Drawing.Pen(Color.Red, 15);
    bool _linePresent = false;

    int[] _imageIndex = { 1, 2, 1 };

    public void Init()
    {
        _images.Add(1, Image.FromFile("Melon.png"));
        _images.Add(2, Image.FromFile("Pineapple.png"));
        PictureBox1.Image = _bitmap;
    }

    public void Update()
    {
        Graphics graphics = Graphics.FromImage(_bitmap);
        graphics.Clear(Color.White);
        graphics.DrawImage(_images[_imageIndex[0]], 50, 100);
        graphics.DrawImage(_images[_imageIndex[1]], 350, 100);
        graphics.DrawImage(_images[_imageIndex[2]], 650, 100);

        if (_linePresent)
            graphics.DrawLine(_redPen, new Point(10, 150), new Point(900, 150));

    }

更新/刷新图片时可能会出现一些问题。但我没有测试它。

答案 1 :(得分:1)

您实际上可以使用位于图像顶部的矩形(红色标签)。比使用图形命令简单得多。