单击时放大图像 - Windows窗体

时间:2013-01-30 17:21:24

标签: c# .net winforms

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

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

任何人都可以帮助我实现它吗?

enter image description here

enter image description here

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

2 个答案:

答案 0 :(得分:2)

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

tempPictureBox.Click += new ...

然后在visual studio将为您生成的Click方法中,您将拥有object sender参数。 您必须转换sender as PictureBox,然后才能访问它。

答案 1 :(得分: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
}
祝你好运!