在图像列表,图片框和调整图片大小之间同步图像

时间:2013-02-03 10:10:27

标签: c# .net winforms

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

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点击图像时显示图像,但只能再次显示缩略图的大小。我想要放大图像。任何人都可以指导我实现我的要求。如果无法进行此设计,是否有人可以指导我为此应用程序使用适当的控件? 提前致谢。

1 个答案:

答案 0 :(得分:1)

你这样做的方式,你的PreviewPictureBox只会显示缩略图,因为tempPictureBox_Click方法中有PreviewPictureBox.Image = ((PictureBox)sender).Image;

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

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

所以,总结一下:

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

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

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