2009年9月2日 星期三

拖放圖片




目前在研究圖片拖過去後,是否有辦法存到指定資料夾,大家加油@@
附程式碼供大家參考,或許可以在短一點,不過我目前只能想到這樣(不會用框架,可能有點佔版面,Sorry):

---------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 拖曳圖片
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
---------------------------------------開啟圖片-------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog opd = new OpenFileDialog();
opd.Filter = "JEPG|*jpg";
opd.ShowDialog();
pictureBox1.Image = Image.FromFile(opd.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
-------------------------------------------------------------------------------------------------
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.AllowDrop = true; //C#無此程式碼,需自己輸入。
pictureBox2.AllowDrop = true; //C#無此程式碼,需自己輸入。
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox pic = (PictureBox)(sender);
if (pic.Image != null)
{
pic.DoDragDrop(pic.Image, DragDropEffects.Move | DragDropEffects.Copy);
}
}
}

private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox pic = (PictureBox)(sender);
if (pic.Image != null)
{
pic.DoDragDrop(pic.Image, DragDropEffects.Move | DragDropEffects.Copy);
}
}
}

private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Move;
}

else
{
e.Effect = DragDropEffects.None;
}
}

private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Move;
}

else
{
e.Effect = DragDropEffects.None;
}
}

private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
pictureBox2.Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = null; //拖動圖片後,box1的圖是否繼續顯示。
}


}

------------------------------------------------------------------------------------------------

沒有留言: