0

我正在尝试开发一个 Windows 窗体应用程序来捕获屏幕快照,并在捕获它们时动态显示它们,并且我还想提供删除捕获的图像的功能。现在,我已经完成了捕获并将它们显示为缩略图。单击特定缩略图时,我想显示图像的放大版本。我在链接捕获的图像和显示调整大小的版本时遇到问题。这是我到目前为止所做的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using HotKeys;

namespace Snapper
{
    public partial class SnapperForm : Form
    {
        static int imgCounter = 0;//keeps track of img for naming

        //create an instance of GlobalHotKey
        private GlobalHotKey ghk1; 
        private GlobalHotKey ghk2;

        public SnapperForm()
        {
            InitializeComponent();

            //pass the required key combination and form to the constructor
            ghk1 = new GlobalHotKey(Constants.CTRL + Constants.SHIFT, Keys.S, this);
            ghk2 = new GlobalHotKey(Constants.CTRL + Constants.SHIFT, Keys.W, this);
        }

        private Keys GetKey(IntPtr LParam)
        {
            return (Keys)((LParam.ToInt32()) >> 16);
        }
        //Perform the action required when HotKey is pressed
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
            {
                switch (GetKey(m.LParam))
                {
                    case Keys.S:
                        //take a snapshot of entire screen when CTRL + SHIFT + S is pressed
                        CaptureScreen();
                        break;
                    case Keys.W:
                        TestHotKeyCSW();
                        break;
                }                
            }
            base.WndProc(ref m);
        }


        private void TestHotKeyCSW()
        {
            MessageBox.Show("Hot key CTRL + SHIFT + W recived");
        }

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

            //working with ImageList
            CapturedImagesList.Images.Add(bmp);
            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;
        }
        private void Main_Load(object sender, EventArgs e)
        {
            if (!ghk1.Register() || !ghk2.Register())
            {
                MessageBox.Show("Failed to register", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void SnapperForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!ghk1.Unregister() || !ghk2.Unregister())
            {
                MessageBox.Show("Failed to UnRegister", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

我想将捕获的图像存储在图像列表中,并在动态生成的缩略图(图片框)之间同步,并在预览图片框中显示单击的图像的放大版本。当从flowlayoutpanel单击到previewpicturebox时,我能够显示图像,但只能再次显示缩略图的大小。我想放大图像。谁能指导我实现我的要求。如果这种设计是不可能的,任何人都可以指导我为这个应用程序使用适当的控件吗?提前致谢。

4

1 回答 1

1

由于使用 tempPictureBox_Click 方法,您的 PreviewPictureBox 将仅显示缩略图图像PreviewPictureBox.Image = ((PictureBox)sender).Image;

sender 参数是您在 CaptureScreen() 方法中创建的 tempPictureBox,它的 Image 属性返回您通过调用创建的缩略图bmp.GetThumbnailImage(...)

为了显示大图像,您必须存储 bmp 对象本身,而不仅仅是缩略图版本。一种方法是将其存储在tempPictureBox.Tag属性中。这些标记属性可以在大多数 Windows 窗体控件中找到,并用于存储与该控件关联的任意数据。

所以,总结一下:

添加tempPictureBox.Tag = bmp;CaptureScreen() 方法,

更改PreviewPictureBox.Image = ((PictureBox)sender).Image;PreviewPictureBox.Image = (Bitmap)((PictureBox)sender).Tag;.

可能仍然需要调整 PreviewPictureBox 的大小,但至少它不会以这种方式显示缩略图。

于 2013-02-03T10:47:46.550 回答