I’m trying to develop a windows forms application that captures snapshots of scree, displays them dynamically as they are captured and I would also like to provide the functionality to delete the captured images too. Right now, I’ve completed till capturing and displaying them as thumbnails. When a particular thumbnail is clicked, I would like to display an enlarged version of the image. I’m having trouble in linking the images captured and displaying the re-sized version.
Here is my code of what I’ve done so far.
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);
}
}
}
}
I thought to store captured images in an imagelist and sync between the dynamically generated thumbnails (picture boxes) and display the enlarged version of image clicked in the preview picture box. I’m able to display the image when it is clicked from the flowlayoutpanel to previewpicturebox, but only of the size of thumbnail again. I want the image to be enlarged. Can anyone guide me in achieving my requirement. If this design is not possible, can anyone guide me in using appropriate controls for this application?
Thanks in advance.
The way you do it your PreviewPictureBox will only show thumbnail images because of
PreviewPictureBox.Image = ((PictureBox)sender).Image;in the tempPictureBox_Click method.The sender parameter is the tempPictureBox which you created in the CaptureScreen() method and its Image property is returning the thumbnail which you created by calling
bmp.GetThumbnailImage(...).In order to show the large image you have to store the bmp object itself, not just the thumbnail version. One way to do this is to store it in the
tempPictureBox.Tagproperty. These Tag properties can be found in most Windows Forms controls and are used to store arbitrary data associated with that control.So, to summarize it:
add
tempPictureBox.Tag = bmp;in the CaptureScreen() method,change
PreviewPictureBox.Image = ((PictureBox)sender).Image;toPreviewPictureBox.Image = (Bitmap)((PictureBox)sender).Tag;.It might still be necessary to adjust the size of the PreviewPictureBox, but at least it won’t show the thumbnail picture that way.