c#编程,如何实现拖动一个图片在picturebox中打开

c#编程,如何实拖动一个图像现在picturebox中打开,不是在两个pictruebox中拖动,就是平日里磁盘中存在的图片,用鼠标拖动到(自己编写的一个小软件)picturebox中打开,请附上相应代码,分数是大大的有的!!!
我在窗体中拖放一个picturebox,怎么就没发现allowdrop属性呢,像panel和form都有,
分数吗,等验证以后,自然还会加分的。
新加问题,下面那位仁兄的代码我已经验证过了,可以实现,不过我还有一个问题没解决,再帮我解决一下捕获鼠标事件怎么样,比如说如何捕获鼠标事件在picturebox中绘制一个矩形什么的,如何获得鼠标的当前坐标等等。
此问题一解决,再奉上30分。

private void pictureBox1_DragOver(object sender, DragEventArgs e)
{
if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
{
e.Effect = DragDropEffects.Link;
}
}

private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
string[] items = (object)e.Data.GetData("FileNameW") as string[];
if (items.Length == 1)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(items[0]);
this.pictureBox1.Image = img;
}
}

在窗体构造函数或者是Load事件里写下面三行
this.pictureBox1.DragOver+=new DragEventHandler(pictureBox1_DragOver);
this.pictureBox1.DragDrop+=new DragEventHandler(pictureBox1_DragDrop);
this.pictureBox1.AllowDrop = true;

代码已经经过测试,没有问题

PictureBox的AllowDrop属性确实感知不出来,但是PictureBox类中含有对这个属性的定义,你可以Go To Definition看一下
你这样写是不会出问题的,因为这个属性在定义的地方加了两个Attribute:
// Summary:
// Overrides the System.Windows.Forms.Control.AllowDrop property.
[EditorBrowsable(1)]
[Browsable(false)]
public override bool AllowDrop { get; set; }
它们规定这个属性是否可见,详情可以查询MSDN

还要谢谢楼主啊,我是好久之前遇到这个情况了,当时没查出来,今天你这么一提查出来了,长了点知识,多谢多谢!

----------------------------------------------
你是想做个画图板了....
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p2 = new Point(e.X, e.Y);
Rectangle rec = new Rectangle();
Graphics g1= this.pictureBox1.CreateGraphics();
g1.DrawRectangle(new Pen(Brushes.Black), new Rectangle(p1, new Size(p2.X - p1.X, p2.Y - p1.Y)));
}
}

void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p1 = new Point(e.X, e.Y);
}
}

p1,p2是两个Point类型的类成员,这个无所谓,你声明一个Rectangle的也可以。
怎么注册事件就不用我教了吧。
这个是画方形,其他图形也可以画,那就是需要设置一个标志位,记录鼠标当前状态,然后画的时候判断和计算就可以了。具体代码我不写了

yloverong:thx~!没关系的,这个分也不能打粮食吃~没啥。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-29
//你把我的代码复制过去用就可以的了~

实在不懂我用户名就是Q号

主菜 Form from1一个
配菜 PictureBox pictureBox1 一个
香料 如下代码一堆

//呵~想加赏多少分?哈`

public partial class Form1 : Form
{
string fileName = string.Empty;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}

void Form1_Load(object sender, EventArgs e)
{
this.AllowDrop = true;
}

void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.All;

else e.Effect = DragDropEffects.None;
}

void Form1_DragDrop(object sender, DragEventArgs e)
{
//获取第一个文件名
fileName = (e.Data.GetData(DataFormats.FileDrop, false) as String[])[0];
try
{
this.pictureBox1.ImageLocation = fileName;
}
catch (Exception) { MessageBox.Show("文件格式不对"); }
}
}

//当前鼠标坐标
//MousePosition.X //MousePosition.Y
//你可以在MouseDonw和MouseUp分别记录一下坐标值就可以了

//关于绘制
Graphics gc = this.CreateGraphics();
//得到当前的gc
gc.DrawLine(new Pen(Color.Red, 10), 100, 100, 200, 200);
//画一条宽20的红线,位置在 100,100开始画到200,200
Graphics还有很多其它画圆啊~画框啊很多方法的~
可以去MSDN查Graphice类得到你想要的资料

//不加多点分对不起党啊~~~本回答被提问者采纳
第2个回答  2008-06-01
想要绘图的话,要调用GDI绘图操作。 建议你把分给楼上的,然后重新提个问题,这样一题多问,我觉得不太厚道。
第3个回答  2008-05-30
接收拖放的窗体需要设置AllowDrop为true;重写DragDrop,DragEnter,DragOver等几个方法。

手边没vs,代码没法提供
第4个回答  2015-10-12
正需要这个,留个脚印
相似回答