2013-02-03 60 views
0

我試圖開發一個窗體應用程序,它捕獲碎片的快照,動態顯示它們,因爲它們被捕獲,我也想提供刪除捕獲的圖像的功能。現在,我已經完成,直到捕獲並以縮略圖形式顯示它們。點擊某個縮略圖時,我想顯示該圖像的放大版本。我無法鏈接捕獲的圖像並顯示重新調整大小的版本。 這是我迄今爲止所做的代碼。同步圖像列表,圖片框和調整大小圖片之間的圖像

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

由於tempPictureBox_Click方法中的PreviewPictureBox.Image = ((PictureBox)sender).Image;,因此您的PreviewPictureBox將只顯示縮略圖圖像。

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的大小,但至少它不會以這種方式顯示縮略圖。

+0

謝謝。你的回答幫助我向前邁進了很多。現在我正在嘗試調整圖像大小,使其適合previewpicturebox。 – kunaguvarun

+0

您可以在設計器或代碼中使用'PictureBox.SizeMode'屬性來自動調整PictureBox的大小以適應圖像。 'PictureBox.SizeMode = PictureBoxSizeMode.AutoSize'。 – Dirk

+0

謝謝,我調整了傳入的圖像以適合我的previewpicturebox,因爲圖像的大小超過了圖片框的大小 – kunaguvarun