0

我正在开发一个 Windows 窗体应用程序,如下所示。我想捕获屏幕截图,将它们显示为缩略图存储在图片框中(动态)并将其添加到添加控制按钮上方的 FlowLayoutPanel 中。我已经做到了。

在 FlowLayoutPanel 的顶部,我希望在单击相应的图片框控件时放大并显示缩略图。现在我意识到我不再可以访问动态生成的图片框了。

谁能帮我实现它?

在此处输入图像描述

在此处输入图像描述

    namespace Snapper
{
    public partial class Main : Form
    {
        static int imgCounter = 0;//keeps track of img for naming
        public Main()
        {
            InitializeComponent();
        }

        private void TestFlowButton_Click(object sender, EventArgs e)
        {            
            CaptureScreen();
        }

        private void CaptureScreen()
        { 
            /*This method captures a snapshot of screen and 
             * adds it to the ImageFlowLayoutPanel
             */             

            Rectangle bounds = Screen.GetBounds(Point.Empty);
            Bitmap bmp = new Bitmap(bounds.Width,bounds.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size); 
            imgCounter += 1;
            bmp.Save("snap" + imgCounter.ToString() + ".jpg", ImageFormat.Jpeg);

            //creating a picturebox control and add it to the flowlayoutpanel
            PictureBox tempPictureBox = new PictureBox();                

            //generates a thumbnail image of specified size
            tempPictureBox.Image = bmp.GetThumbnailImage(100,100,
                                   new Image.GetThumbnailImageAbort(ThumbnailCallback),
                                   IntPtr.Zero);
            tempPictureBox.Size = new System.Drawing.Size(100, 100);
            tempPictureBox.Click += new EventHandler(this.tempPictureBox_Click);                             
            ImageFlowLayoutPanel.Controls.Add(tempPictureBox);

        }

        //This click event will be used to display the enlarged images
        private void tempPictureBox_Click(object sender, EventArgs e)
        {            
            PreviewPictureBox.Image = ((PictureBox)sender).Image;
        }
        public bool ThumbnailCallback()
        {
            return true;
        }
    }
}
4

2 回答 2

2

Click如果为动态添加的图片框设置它,则可以在事件中访问它:

tempPictureBox.Click += new ...

然后在ClickVisual Studio 为您生成的方法中,您将拥有该object sender参数。您必须强制转换sender as PictureBox然后才能访问它。

于 2013-01-30T17:27:08.300 回答
2

好的,这是一些使用 PictureBoxes 的单击事件的更新代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static int imgCounter = 0;//keeps track of img for naming
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CaptureScreen();
        } 

        private void tempPictureBox_Click(object sender, EventArgs e)
        {
            //Put code here
        }

        private void CaptureScreen()
        {
            /*This method captures a snapshot of screen and 
             * adds it to the ImageFlowLayoutPanel
             */

            Rectangle bounds = Screen.GetBounds(Point.Empty);
            Bitmap bmp = new Bitmap(bounds.Width, bounds.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
            imgCounter += 1;
            bmp.Save("snap" + imgCounter.ToString() + ".jpg", ImageFormat.Jpeg);

            //creating a picturebox control and add it to the flowlayoutpanel
            PictureBox tempPictureBox = new PictureBox();

            //generates a thumbnail image of specified size
            tempPictureBox.Image = bmp.GetThumbnailImage(100, 100,
                               new Image.GetThumbnailImageAbort(ThumbnailCallback),
                               IntPtr.Zero);
            tempPictureBox.Size = new System.Drawing.Size(100, 100);
            tempPictureBox.Click += new System.EventHandler(this.tempPictureBox_Click);
            tempPictureBox.Cursor = System.Windows.Forms.Cursors.Hand;
            flowLayoutPanel1.Controls.Add(tempPictureBox);

        }
        public bool ThumbnailCallback()
        {
            return true;
        }
    }
}

请注意,我还获得了一些乐趣,并让图片框光标成为一只手。如果你愿意,你可以改变它。

所以总结一下,只要把放大事件放在这个里面:

private void tempPictureBox_Click(object sender, EventArgs e)
{
    //Put code here
}

祝你好运!

于 2013-01-30T18:03:28.207 回答