简体   繁体   English

如何显示图片预览? 若要更改pictureBox的大小并将其移动到窗体的中心

[英]How can i show an image preview ? To change a pictureBox size and move it to the center of the form

This is the code in Form1: 这是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)
        {

        }

In top of form1: 在form1顶部:

string[] file_array_satellite;
int file_indxs_satellite;

In constructor: 在构造函数中:

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

What i want to do is when i move the mouse over the pictureBox1 area the pictureBox will be resized to for example 400x400 and will show the image in the pictureBox1 in the resized size in this case 400x400. 我想做的是,当我将鼠标移到pictureBox1区域上时,pictureBox将被调整为例如400x400的大小,在这种情况下,将以400x400的大小显示pictureBox1中的图像。

Like a preview . 喜欢预览。 When i move over with the mouse the animation will keep moving/playing but the pictureBox will be in the middle of the form and a bigger size like 400x400 . 当我用鼠标移动时,动画将继续移动/播放,但pictureBox将位于窗体的中间,并且尺寸较大,如400x400。

How can i do it ? 我该怎么做 ? What should i do in the pictureBox1_MouseEnter event ? 在pictureBox1_MouseEnter事件中该怎么办?

Edit** 编辑**

Tried this: 试过这个:

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();
        }

But the pictureBox is not in the center and the size is not changed. 但是pictureBox不在中心,并且大小不变。 Why ? 为什么呢 In the original the pictureBox size is 100,100 also the files on hard disk i change them to 100,100 and it's working good ! 在原始情况下,pictureBox的大小为100,100,硬盘上的文件也将其更改为100,100,并且运行良好!

Once i move the mouse over the pictureBox it's not moving to the center and not resize to 500,500. 一旦我将鼠标移到pictureBox上,它就不会移动到中心并且不会调整为500,500。

The form1 size is 800,600 form1的大小是800,600

I want the pictureBox to show in the center of the Form and to be bigger size 500,500 or 700,500 我希望pictureBox显示在窗体的中心,并且尺寸更大,为500,500或700,500

What's wrong ? 怎么了 ?

you should resize image by PictureBox.SizeMode property , so the image will fill the PictureBox. 您应该通过PictureBox.SizeMode 属性来调整图像的大小,以便图像将填充PictureBox。 And then use MouseEnter and MouseLeave events to resize the PictureBox. 然后使用MouseEnterMouseLeave事件来调整PictureBox的大小。 It will be resized with the picture inside. 它将与里面的图片一起调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM