简体   繁体   English

在表单上显示拖放的图像

[英]Display dropped image on Form

I have a C# app and i want it that when an Image is dropped on the form, A picturebox in the form displays the Image. 我有一个C#应用程序,我希望将图像放在窗体上时,窗体中的图片框会显示该图像。 I have tried this 我已经试过了

    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));
    }

But it doesn't work. 但这是行不通的。

Pls what did i do wrong? 请问我做错了什么?

I think you are dragging from file. 我认为您正在拖拽文件。 Simple code for this will be such: 这样的简单代码如下:

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]; 

        }
    }

}

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM