【问题标题】:Display dropped image on Form在表单上显示丢弃的图像
【发布时间】:2013-06-30 04:00:42
【问题描述】:

我有一个 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));
    }

但它不起作用。

请问我做错了什么?

【问题讨论】:

  • 您用来拖动图像的示例应用是什么?但是,在您 Invalidate 您的图片框1 之前,您的代码无法正常工作,您应该使用 Image 属性,如下所示:pictureBox1.Image = (Bitmap) e.Data.GetData(DataFormats.Bitmap);
  • 我试过了,但是不行
  • 有异常吗?
  • @Precious1tj 从哪里拖入表单?

标签: c# winforms drag-and-drop


【解决方案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]; 

        }
    }

}

【讨论】:

    【解决方案2】:

    我真的希望这不是解决方案,但您没有提供太多信息,所以我会从这里开始,我们会在您提供更多信息时继续。 当您获取此样本时,您是否将其绑定到正确的事件? 如:

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

    另外,在初始化时,你做了吗:

    AllowDrop = true;
    

    【讨论】:

      猜你喜欢
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 2014-02-16
      • 2018-11-09
      • 2014-01-20
      • 2020-04-01
      相关资源
      最近更新 更多