我試圖開發一個窗體應用程序,它捕獲碎片的快照,動態顯示它們,因爲它們被捕獲,我也想提供刪除捕獲的圖像的功能。現在,我已經完成,直到捕獲並以縮略圖形式顯示它們。點擊某個縮略圖時,我想顯示該圖像的放大版本。我無法鏈接捕獲的圖像並顯示重新調整大小的版本。 這是我迄今爲止所做的代碼。同步圖像列表,圖片框和調整大小圖片之間的圖像
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時顯示,但只能再次顯示縮略圖的大小。我想要放大圖像。任何人都可以指導我達到我的要求。如果這種設計是不可能的,任何人都可以引導我使用適當的控制這個應用程序? 在此先感謝。
謝謝。你的回答幫助我向前邁進了很多。現在我正在嘗試調整圖像大小,使其適合previewpicturebox。 – kunaguvarun
您可以在設計器或代碼中使用'PictureBox.SizeMode'屬性來自動調整PictureBox的大小以適應圖像。 'PictureBox.SizeMode = PictureBoxSizeMode.AutoSize'。 – Dirk
謝謝,我調整了傳入的圖像以適合我的previewpicturebox,因爲圖像的大小超過了圖片框的大小 – kunaguvarun