English 中文(简体)
图像框图形错误
原标题:ImageBox graphics mistake

我正在试图将多边形列表绘制到图像框中。 目前我只得到了屏幕左上角的对角黑线。 这些点如下:

20,20 0,20 20,0 40,40

200,100 0,20 20,0 20,20

100, 200 0, 200 200,0 250, 450

150,130 1,20 20,0 10,20

    private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
    {
        int jobIndex = 0;
        int trussIndex = 0;

        Graphics g = e.Graphics;
        foreach (Member m in jobArray[jobIndex].trusses[trussIndex].members)
        {
            Point[] pointArray = new Point[m.poly.Points.Count()];
            //m.poly.Points.CopyTo(pointArray, 0);
            int index =0;
            foreach(System.Windows.Point p in m.poly.Points)
            {
                pointArray[index].X = (int)p.X;
                pointArray[index].Y = (int)p.Y;
                index++;
            }
            Pen myPen = new Pen(Color.Black);
            SolidBrush myBrush = new SolidBrush(m.color);

            g.DrawPolygon(myPen, pointArray);
            g.FillPolygon(myBrush, pointArray);

        }
    }

Any Ideas as to why the polygons do not appear on the imagebox? The colors are no the issue nor the size of the box

最佳回答

似乎您使用“ 绘画事件” 来调用这个方法, 确保您在调用该方法时拥有可用的数据。 油漆事件不总是被调用, 所以如果您要添加数据来绘制的话, 您需要调用“ 图片Box1. Invalifate () ” 的方法来强制绘制事件 。

我敢肯定,你迟早会看到这一点,但你应该创建一个图像(Bitmap)对象,并将其指定给 PhotoBox 的图像属性。然后使用图形。从图像(_image) 获得您的图形对象。如果您不这样做,那么如果窗口被覆盖,您的图像将不再停留,然后返回焦点,或者将其拖离屏幕,然后将它拖离屏幕。

private Bitmap _bitMap;
private Graphics _graphic;
Pen myPen;

public Constructor()
{
    _bitMap = new Bitmap(pictureBox1.Width,pictureBox1.Height);
    _graphic = Graphics.FromImage(_bitMap);
    pictureBox1.Image = _bitMap;
    myPen = new Pen(Color.Black);
}
private void DataAdded()
{
    int jobIndex = 0;
    int trussIndex = 0;

    foreach (Member m in jobArray[jobIndex].trusses[trussIndex].members)
    {
        //Pen myPen = new Pen(Color.Black); //Don t instantiate in a loop
        SolidBrush myBrush = new SolidBrush(m.color);
        _graphic.DrawPolygon(myPen, m.poly.Points.ToArray());
        _graphic.FillPolygon(myBrush, m.poly.Points.ToArray());
    }
    pictureBox1.Image = _bitMap; //Don t think you need this, but I don t remember
}

希望这有帮助

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签