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