winform实现把外部文件拖入PictureBox的代码示例

来源:互联网 发布:mac os 11 yosemite 编辑:程序博客网 时间:2024/06/06 23:19

    private void Form1_Load(object sender, EventArgs e)  

        {    
            //这句代码不会报错,但是需要手动输入,.net编辑器无法自动识别AllowDrop    
            this.pictureBox1.AllowDrop = true;    
        }    
             
        private void pictureBox1_DragDrop(object sender, DragEventArgs e)    
        {    
            string fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();    
            this.pictureBox1.Image = Image.FromFile(fileName);    
        }    
        private void pictureBox1_DragEnter(object sender, DragEventArgs e)    
        {    
            if (e.Data.GetDataPresent(DataFormats.FileDrop))    
                e.Effect = DragDropEffects.Link;    
            else e.Effect = DragDropEffects.None;     
        }  

原创粉丝点击