1

我有一个 C# 应用程序,我希望当将图像放在表单上时,表单中的图片框会显示图像。我试过这个

    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;   
    }

    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        Graphics p = pictureBox1.CreateGraphics();
        p.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), new Point(0, 10));
    }

但它不起作用。

请问我做错了什么?

4

2 回答 2

1

我认为您正在从文件中拖动。简单的代码如下:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.AllowDrop = true;
        this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
        this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
    } 
    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy; 
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }

    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        string[] filex = (string[])e.Data.GetData(DataFormats.FileDrop);
        if (filex.Length > 0)
        { 
                pictureBox1.ImageLocation = filex[0]; 

        }
    }

}
于 2013-06-30T05:48:47.583 回答
0

i truly hope this is not the solution but you didn't gave much information so i'll start here and we'll go as you give us more information. when you toke this sample did you bind it to the proper events? as in:

this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);

also, on initializing, did you do:

AllowDrop = true;
于 2013-06-30T05:13:04.520 回答