把一个PictureBox的图片拖到另一个PictureBox里面显示

moyumoyu 2009-07-29 11:11:54
从桌面上可以拖动到PictureBox
从PictureBox1到PictureBox2却不行??
什么问题呢?

主要代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Pic_Drop
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
pictureBox1.AllowDrop=true;
pictureBox2.AllowDrop=true;
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

void PictureBox1DragDrop(object sender, DragEventArgs e)
{
string path =((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
pictureBox1.Image=new Bitmap(path);
pictureBox1.Tag=path;
}

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

void PictureBox1MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox1.Image!=null) {
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Link|DragDropEffects.Copy);
}

}

void PictureBox2DragDrop(object sender, DragEventArgs e)
{
string path=((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
pictureBox2.Image=new Bitmap(path);
}

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

void PictureBox2MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox2.Image!=null) {
pictureBox2.DoDragDrop(pictureBox2.Image, DragDropEffects.Copy);
}

}

void Button1DragDrop(object sender, DragEventArgs e)
{
string s=e.Data.GetData(DataFormats.FileDrop,true).ToString();

}

void Button1DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect=DragDropEffects.Copy;
}
else
{
e.Effect=DragDropEffects.None;
}
}
}
}
...全文
276 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chaozi_249 2009-07-30
  • 打赏
  • 举报
回复
实现了?有copy
xkx2003 2009-07-30
  • 打赏
  • 举报
回复
学习
moyumoyu 2009-07-30
  • 打赏
  • 举报
回复
好了,
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Pic_Drop
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
pictureBox1.AllowDrop=true;
pictureBox2.AllowDrop=true;
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

void PictureBox1DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
string path =((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
pictureBox1.Image=new Bitmap(path);
}
if (e.Data.GetDataPresent(DataFormats.Bitmap)) {
pictureBox1.Image=(Bitmap)e.Data.GetData(DataFormats.Bitmap);
}

}

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

void PictureBox1MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox1.Image!=null) {
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
}

}

void PictureBox2DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
string path=((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
pictureBox2.Image=new Bitmap(path);
}
if (e.Data.GetDataPresent(DataFormats.Bitmap)) {
pictureBox2.Image=(Bitmap)e.Data.GetData(DataFormats.Bitmap);
}

}

void PictureBox2DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect=DragDropEffects.Copy;
}
if (e.Data.GetDataPresent(DataFormats.Bitmap)) {
e.Effect= DragDropEffects.Copy;
}
}

void PictureBox2MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox2.Image!=null) {
pictureBox2.DoDragDrop(pictureBox2.Image, DragDropEffects.Copy);
}

}


}
}
zhangyanyang 2009-07-30
  • 打赏
  • 举报
回复
没接触过,顶起
zgke 2009-07-30
  • 打赏
  • 举报
回复
注意 文件拖放和PICTUREBOX的拖放数据不一样的

void PictureBox2DragDrop(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
pictureBox2.Image = new Bitmap(path);
}
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
pictureBox2.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
}
}

void PictureBox2DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}

110,526

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧