【问题标题】:I'm trying to show the current image in the pictureBox and stretch it but it's not showing anything why?我正在尝试在图片框中显示当前图像并拉伸它,但它没有显示任何内容,为什么?
【发布时间】:2013-10-18 13:53:29
【问题描述】:

在 For1 我有这个代码:

private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {     
                this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Load(file_array_satellite[file_indxs_satellite]);
                file_indxs_satellite = file_indxs_satellite - 1;
                if (file_indxs_satellite < 0)
                {
                    file_indxs_satellite = file_array_satellite.Length - 1;
                }
            }
            catch
            {
                timer1.Enabled = false;
            }
        }

        private void satellitesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            file_array_satellite = Directory.GetFiles(UrlsPath, "RainImage*.*");
            if (file_array_satellite.Length > 0)
            {
                DateTime[] creationTimes8 = new DateTime[file_array_satellite.Length];
                for (int i = 0; i < file_array_satellite.Length; i++)
                    creationTimes8[i] = new FileInfo(file_array_satellite[i]).CreationTime;
                Array.Sort(creationTimes8, file_array_satellite);
                file_indxs_satellite = 0;
                file_indxs_satellite = file_array_satellite.Length - 1;
                timer1.Enabled = true;
            }
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            this.pictureBox1.Size = new Size(500, 500);
            pictureBox1.Location = new Point(this.Bounds.Width / 2,
                            this.Bounds.Height / 2);
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.BringToFront();
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {

                this.pictureBox1.Size = new Size(100, 100);
                pictureBox1.Location = new Point(12,
                                27);

        }

在原版中,picturebox1 的大小为 100x100,我拉伸每个图像以适合 pictureBox。 当它是 100x100 时,一切正常,我在图片框中看到每个图像的动画。

现在我做了一个事件,当我用鼠标进入图片框区域时,它应该移动到窗体的中心,将大小调整为 500x500 拉伸图像并显示相同的动画。 当我离开图片框区域时,它应该恢复到原来的大小和位置。

当我用鼠标进入图片框1区域时,图片框消失了,一旦我离开图片框区域,我在任何地方都看不到它,我看到它的原始位置和大小为 100x100。

为什么当我用鼠标进入 pictureBox1 区域时,它消失了,我在 500x500 尺寸的表格中心看不到它?

file_array_satellite 是 string[] 并且 file_indxs_satellite 是 int。

RainImage*.* 是下载后硬盘上的文件名。

这个想法不是每次进入或离开时都转换/更改硬盘上的文件大小,所以我希望一旦我进入pictureBox1区域,它将拉伸pictureBox中的当前图像并显示它。它在 100x100 时有效,但在 500x500 时无效。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    我冒昧地猜测this.Bounds.Widththis.Bounds.Height 不是您所期望的,所以 PictureBox 并没有消失,您只是将它设置到屏幕外/表单外的某个位置。在调试模式下运行 Visual Studio 并在该行周围放置一个断点,然后查看 this.Bounds 等于什么。这可能会为您提供有关需要设置的正确位置的线索。

    【讨论】:

      【解决方案2】:

      当您将鼠标悬停在 PictureBox 上并将其移动到窗体的中心时,您将其从鼠标光标下方移出。这会导致 MouseLeave 事件立即触发,这会将其再次置于鼠标光标下,从而导致 MouseEnter 事件再次触发,等等。

      你可以这样做:

          bool suppressMouseLeave;
          private void pictureBox1_MouseEnter(object sender, EventArgs e)
          {
              suppressMouseLeave = true;
              this.pictureBox1.Size = new Size(500, 500);
              pictureBox1.Location = new Point(this.Bounds.Width / 2,
                              this.Bounds.Height / 2);
              this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
              pictureBox1.BringToFront();
              //point the cursor to the new Position so that it's still kept on the pictureBox1
              //This is important because it makes your idea acceptable.
              //Otherwise you have to move your mouse onto your pictureBox and leave the 
              //mouse from it then to restore the pictureBox
              Cursor.Position = PointToScreen(new Point(pictureBox1.Left + 250, pictureBox1.Top + 250));
              suppressMouseLeave = false;
          }
      
          private void pictureBox1_MouseLeave(object sender, EventArgs e)
          {
              if(suppressMouseLeave) return;
              this.pictureBox1.Size = new Size(100, 100);
              pictureBox1.Location = new Point(12, 27);
          }
      

      【讨论】:

      • Golden Dragon 你的想法是对的,如果我现在标记删除了鼠标离开事件,那么它正在工作,我在表单的中心看到它,但它永远不会回到原来的位置和大小。你能告诉我如何做离开事件代码吗?
      • 你可以这样做的一种方法是:在你的表单中创建一个新的 bool 并将其设置为 false。在 MouseEnter 函数中,如果图片框的宽度不是 500,请像现在这样调整图片框的大小。如果为 500,则将新布尔值设置为 true。然后在 MouseLeave 函数中,只有当 bool 为 true 时才执行其中的代码
      • @DoronMuzar 你可能要使用2个pictureBoxes,第一个是固定的,另一个用于放大图像并显示在中心。
      【解决方案3】:

      像这样“原地”缩放怎么样?

          private void pictureBox1_MouseEnter(object sender, EventArgs e)
          {
              Rectangle rc = pictureBox1.Bounds;
              rc.Inflate(200, 200);
              pictureBox1.Bounds = rc;
              pictureBox1.BringToFront();
          }
      
          private void pictureBox1_MouseLeave(object sender, EventArgs e)
          {
              Rectangle rc = pictureBox1.Bounds;
              rc.Inflate(-200, -200);
              pictureBox1.Bounds = rc;
          }
      

      【讨论】:

        猜你喜欢
        • 2020-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        相关资源
        最近更新 更多