首先要設定窗體的AllowDrop屬性為true。然後在窗體的DragEnter事件中添加如下代碼:調用自定義的顯示圖檔的方法。

#region "在用滑鼠将某項拖放到區域時事件"
private void Form1_DragEnter(object sender, DragEventArgs e)
{
// this.AllowDrop = true;
//在窗體中顯示拖放到窗體上的圖檔
SetDragImageToForm(this.pictureBox1, e);
}
#endregion

下面代碼是自定義的顯示圖檔的方法

#region "自定義在窗體背景中顯示被拖放的圖檔的方法"
private void SetDragImageToForm(PictureBox pb, DragEventArgs e)
e.Effect = DragDropEffects.Copy;//設定拖放操作中目标放置類型為複制
string[] str_Drop = (string[])e.Data.GetData(DataFormats.FileDrop, true);//檢索資料格式相管理的資料
string tempstr;
Bitmap bkImage;//定義Bitmap變量
tempstr = str_Drop[0];//擷取拖放檔案的目錄
try
bkImage = new Bitmap(tempstr);//存儲拖放的圖檔
pb.Size = new Size(bkImage.Width + 6, bkImage.Height + 33); //根據圖檔設定窗體的大小
pb.BackgroundImage = bkImage;//在窗體背景中顯示圖檔
catch (Exception ex)
MessageBox.Show(ex.Message.ToString());
