如何从某个文件夹拖动图片并放在图片框上打开它 [英] How can I drag a picture from some folder and drop on picture box to open it

查看:126
本文介绍了如何从某个文件夹拖动图片并放在图片框上打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从桌面等任何文件夹中拖动图片,然后将此图片拖放到C#应用程序的图片框中,以便在图片框中显示。



例如当一些人从firefox拖动图片然后放到桌面上时他们只是复制到桌面。

我只是想做同样的事情。我还想从firefox中拖出一张图片并放到我在C#windows窗体应用程序中的图片框中。因此应该相应地显示。



谢谢。

解决方案



这是我最后开发的简单解决方案。




1.创建一个新项目 - >使用C#的Windows Forma应用程序

2.在表单上添加PictureBox

3.将Form1的AllowDrop属性设置为true。

4.添加DragEnter事件到form1并在那里写下面的代码

  private   void  Form1_DragEnter( object  sender,DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}



5.将DragDrop事件添加到Form1并在其中编写以下代码

  private   void  Form1_DragDrop( object  sender,DragEventArgs e)
{
数组a =(数组)e.Data.GetData(DataFormats.FileDrop);
if (a!= null
{
string s = a.GetValue( 0 )。ToString();
pictureBox1.ImageLocation = s;


}





这是最简单的解决方案,而不是MSDN的链接:D。我知道大书中有解决方案但他们需要很多时间:D


只需学习 System.Windows.Forms的拖放功能

http: //msdn.microsoft.com/en-us/library/ms171546%28v=vs.110%29.aspx [ ^ ]。



-SA

How can I drag a picture from any folder like Desktop then Drop this picture to my picture box in C# application to show it on picture box.

For example when some one drag picture from firefox then drop onto desktop they just copied to desktop.
I just want to do same thing using. I also want to drag a picture from firefox and drop onto my picure box in C# windows form application. So that it should be displayed accordingly.

Thanks.

解决方案


Here is it's simple solution developed by me at last.


1. Create a new Project -> Windows Forma Application using C#
2. Add a PictureBox on the form
3. Set the Form1's AllowDrop Property to true.
4. Add DragEnter Event to form1 and write the following code there

private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }


5. Add DragDrop Event to Form1 and write the following code there

private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
            if (a != null)
            {
                string s = a.GetValue(0).ToString();
                pictureBox1.ImageLocation = s;

               
            }



Thats the simplest solution rather then the links to the MSDN :D. I know there are solutions in the big books but they need a lot of time :D


Just learn the drag and drop functionality for System.Windows.Forms:
http://msdn.microsoft.com/en-us/library/ms171546%28v=vs.110%29.aspx[^].

—SA


这篇关于如何从某个文件夹拖动图片并放在图片框上打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆