如何显示图像预览?更改图片框大小并将其移动到表单的中心

时间:2013-10-07 23:26:59

标签: c# winforms

这是Form1中的代码:

private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {

                pictureBox1.Load(file_array_satellite[file_indxs_satellite]);
                //label12.Visible = true;
                //label12.Text = "Satellite files date and time: " + File.GetCreationTime(file_array_satellite[file_indxs_satellite]);
                file_indxs_satellite = file_indxs_satellite - 1;
                //pictureBox1.Load(file_array[file_indexs]);
                //label10.Visible = true;
                //label10.Text = "Radar files date and time: " + File.GetCreationTime(file_array[file_indexs]);
                file_indxs_satellite = file_indxs_satellite - 1;
                if (file_indxs_satellite < 0)
                {
                    file_indxs_satellite = file_array_satellite.Length - 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*.*");
            for (int i = 0; i < file_array_satellite.Length; i++)
            {
                Image s = new Bitmap(file_array_satellite[i]);
                s = resizeImage(s, new Size(100, 100));
                s.Save(UrlsPath + "Changed" + i.ToString("D6") + ".jpg");
            }
            file_array_satellite = Directory.GetFiles(UrlsPath, "Changed*.*");
            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;
            }
        }

        public static Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {

        }

在form1的顶部:

string[] file_array_satellite;
int file_indxs_satellite;

在构造函数中:

localFilename = @"d:\localpath\";
UrlsPath = @"d:\localpath\Urls\";

我想要做的是当我将鼠标移到pictureBox1区域时,pictureBox将被调整为例如400x400,并将在图片框1中以调整大小显示图像400x400。

喜欢预览。当我用鼠标移动时,动画将继续移动/播放,但是pictureBox将位于窗体的中间,并且尺寸更大,如400x400。

我该怎么办?我该怎么做pictureBox1_MouseEnter事件?

编辑**

试过这个:

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

但是pictureBox不在中心,大小也没有改变。为什么? 在原来的pictureBox大小是100,100也硬盘上的文件我改为100,100,它的工作正常!

一旦我将鼠标移到pictureBox上,它就不会移动到中心而不会调整为500,500。

form1大小为800,600

我希望pictureBox显示在Form的中心,并且更大的尺寸为500,500或700,500

怎么了?

1 个答案:

答案 0 :(得分:2)

您应该按PictureBox.SizeMode property调整图片大小,以便图片填充PictureBox。然后使用MouseEnterMouseLeave事件来调整PictureBox的大小。它会根据里面的图片调整大小。