【问题标题】:Enlarging an Image when clicked - Windows Forms单击时放大图像 - Windows 窗体
【发布时间】:2013-01-30 17:21:23
【问题描述】:

我正在开发一个 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;
        }
    }
}

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    好的,下面是一些使用 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
    }
    

    祝你好运!

    【讨论】:

    • 哇。这正是我正在寻找的。我会在晚上试一试,一旦我做对了,就接受你的正确。我还想添加删除一些不需要的快照的功能。
    【解决方案2】:

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

    tempPictureBox.Click += new ...

    然后在 Visual Studio 将为您生成的 Click 方法中,您将拥有 object sender 参数。 您必须投射sender as PictureBox,然后才能访问它。

    【讨论】:

    • 谢谢,我决定加入一些额外的功能,比如删除一些不感兴趣的快照,并认为点击事件不能再被访问,因为每次新的图片框控件被覆盖时,tempPictureBox 都会被覆盖添加。 SuperPrograman 的以下答案就是您所解释的。谢谢你们俩。
    • 不客气,很高兴这对您有帮助,祝您余下的工作好运! (ps:您应该将@SuperPrograman 的答案标记为已解决,因为它似乎以明确的方式解决了您所回答的问题,与他相比,我缺乏细节)
    • 我已经根据您最重要的一点修改了代码作为图片框,现在我只能得到缩略图大小而不是图像的原始大小。我需要一种方法来显示原始图像而不是添加到 flowlayoutpanel 的图片框中的缩略图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 2012-10-20
    • 1970-01-01
    • 2013-04-04
    相关资源
    最近更新 更多