为什么在pictureBox.Image上用鼠标绘制一个矩形时它没有绘制任何东西?

时间:2016-05-29 21:25:56

标签: c# .net winforms

pictureBoxSnap是winforms PictureBox的常规控件。 当它到达以下部分时:

pictureBoxSnap.Image = snap.Image;

它不会在pictureBoxSnap上显示/绘制一个矩形 但是,如果它到达IF的第二部分:

drawpicbox(this.listBox1.SelectedIndex);

它将绘制一个矩形。

我有这个选定的索引事件

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            WindowSnap snap = listBox1.Items[listBox1.SelectedIndex] as WindowSnap;
            if (snap.Text.Contains("emails") || snap.Text.Contains("Emails"))
            {
                pictureBoxSnap.Image = snap.Image;
            }
            else
            {
                img = new Bitmap(pictureBoxSnap.Width, pictureBoxSnap.Height);
                imgClone = new Bitmap(pictureBoxSnap.Width, pictureBoxSnap.Height);
                Graphics g;
                using (g = Graphics.FromImage(img))
                {
                    g.Clear(Color.White);
                }
                pictureBoxSnap.Image = img;
                drawpicbox(this.listBox1.SelectedIndex);
            }
        }

然后得到int(index)的drawpicbox方法:

private void drawpicbox(int index)
        {
            Rectangle[] test = rectangles;            
            Graphics g, g1;
            Size sz;
            WindowSnap snap = this.listBox1.SelectedItem as WindowSnap;
            Bitmap testBmp;
            if (snap.Image != null)
            {
                using (g = Graphics.FromImage(img))
                {
                    g.Clear(Color.White);
                    sz = calculateSizeAndPosition(snap.Image.Size);
                    if (isCropped[index] == true)
                    {
                        ConfirmRectangle.Enabled = false;
                        Reset.Enabled = true;
                        using (testBmp = new Bitmap(img.Width, img.Height))
                        {
                            using (g1 = Graphics.FromImage(testBmp))
                            {
                                g1.Clear(Color.White);
                                g1.DrawImage(snap.Image, (int)((pictureBoxSnap.Width - sz.Width) / 2.0), (int)((pictureBoxSnap.Height - sz.Height) / 2.0), sz.Width, sz.Height);
                            }
                            g.DrawImage(testBmp, rectangles[index], rectangles[index], GraphicsUnit.Pixel);
                            g.DrawRectangle(Pens.Firebrick, rectangles[index]);
                        }
                    }
                    else if (rectangles[index].Width != 0)
                    {
                        cm.MenuItems[0].Enabled = true;
                        cm.MenuItems[1].Enabled = true;
                        cm.MenuItems[2].Enabled = true;
                        cm.MenuItems[3].Enabled = true;
                        ConfirmRectangle.Enabled = true;
                        g.DrawImage(snap.Image, (int)((pictureBoxSnap.Width - sz.Width) / 2.0), (int)((pictureBoxSnap.Height - sz.Height) / 2.0), sz.Width, sz.Height);
                        g.DrawRectangle(Pens.Firebrick, rectangles[index]);
                        Reset.Enabled = true;
                    }
                    else
                    {
                        cm.MenuItems[0].Enabled = false;
                        cm.MenuItems[1].Enabled = false;
                        cm.MenuItems[2].Enabled = false;
                        cm.MenuItems[3].Enabled = false;
                        ConfirmRectangle.Enabled = false;
                        g.DrawImage(snap.Image, (int)((pictureBoxSnap.Width - sz.Width) / 2.0), (int)((pictureBoxSnap.Height - sz.Height) / 2.0), sz.Width, sz.Height);
                        Reset.Enabled = false;
                    }

                }

                pictureBoxSnap.Invalidate();
            }
        }

鼠标事件向下并移动我用来绘制矩形:

WindowSnap snap = null;
        Point screenCoordinates;
        Point pictureBoxSnapCoordinates;
        private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
        {
           screenCoordinates = this.PointToScreen(pictureBoxSnap.Location);


            Control control = (Control)sender;
            screenCoordinates = control.PointToScreen(new Point(e.X, e.Y));
            pictureBoxSnap.PointToScreen(Point.Empty);
            screenCoordinates = pictureBoxSnap.PointToScreen(e.Location);
            pictureBoxSnapCoordinates = e.Location;
            Graphics g;
            Size sz;

            if (isCropped[listBox1.SelectedIndex] == false && e.Button == MouseButtons.Left)
            {
                snap = this.listBox1.SelectedItem as WindowSnap;

                RectStartPoint = e.Location;

                sz = calculateSizeAndPosition(snap.Image.Size);

                using (g = Graphics.FromImage(imgClone))
                {
                    g.Clear(Color.White);
                    g.DrawImage(snap.Image, (int)((pictureBoxSnap.Width - sz.Width) / 2.0), (int)((pictureBoxSnap.Height - sz.Height) / 2.0), sz.Width, sz.Height);
                }
            }
        }

鼠标移动

private void pictureBoxSnap_MouseMove(object sender, MouseEventArgs e)
        {
            if (isCropped[listBox1.SelectedIndex] == false && e.Button == MouseButtons.Left && e.Location != RectStartPoint)
            {
                DrawRectangle(e.Location);
            }
        }

最后使用DrawRectangle方法

private void DrawRectangle(Point pnt)
        {
            Graphics g = Graphics.FromImage(img);
            g.DrawImage(imgClone, 0, 0);

            if (pnt.X == RectStartPoint.X || pnt.Y == RectStartPoint.Y)
            {
                g.DrawLine(Pens.Firebrick, RectStartPoint.X, RectStartPoint.Y, pnt.X, pnt.Y);
            }
            else if (pnt.X > RectStartPoint.X && pnt.Y > RectStartPoint.Y) 
            {
                rect.X = RectStartPoint.X; rect.Y = RectStartPoint.Y; rect.Width = pnt.X - RectStartPoint.X; rect.Height = pnt.Y - RectStartPoint.Y;
                g.DrawRectangle(Pens.Firebrick, rect);
            }
            else if (pnt.X > RectStartPoint.X && pnt.Y < RectStartPoint.Y) 
            {
                rect.X = RectStartPoint.X; rect.Y = pnt.Y; rect.Width = pnt.X - RectStartPoint.X; rect.Height = RectStartPoint.Y - pnt.Y;
                g.DrawRectangle(Pens.Firebrick, rect);
            }
            else if (pnt.X < RectStartPoint.X && pnt.Y > RectStartPoint.Y) 
            {
                rect.X = pnt.X; rect.Y = RectStartPoint.Y; rect.Width = RectStartPoint.X - pnt.X; rect.Height = pnt.Y - RectStartPoint.Y;
                g.DrawRectangle(Pens.Firebrick, rect);
            }
            else 
            {
                rect.X = pnt.X; rect.Y = pnt.Y; rect.Width = RectStartPoint.X - pnt.X; rect.Height = RectStartPoint.Y - pnt.Y;
                g.DrawRectangle(Pens.Firebrick, rect);
            }

            g.Dispose();

            pictureBoxSnap.Invalidate();
        }

现在我在所选索引事件中使用了一个断点,当它执行IF的一部分时:

pictureBoxSnap.Image = snap.Image;

或者

drawpicbox(this.listBox1.SelectedIndex);

最后,两个人都将DrawRectangle方法转到了这一行:

g.DrawLine(Pens.Firebrick, RectStartPoint.X, RectStartPoint.Y, pnt.X, pnt.Y);

但是当它pictureBoxSnap.Image = snap.Image;它没有画任何东西时,我看不到pictureBoxSnap上的任何矩形,但当它正在做drawpicbox(this.listBox1.SelectedIndex);时,我看到矩形在pictureBoxSnap。

不确定原因。

0 个答案:

没有答案