C#剪切板编程操作图片、音乐、txt==文件(神样的代码,找了好久)

来源1:http://www.cnblogs.com/webcyz/archive/2011/06/29/2092946.html

来源2:http://blog.csdn.net/hc1104/article/details/8130831

.....

 

 

*****

采纳来源:

http://www.360doc.com/content/14/0314/13/16280577_360525415.shtml

*** 可用于图片、音乐、txt==文件的复制、剪切 操作。代码只是获取了文件对应的路径

一.set图片到系统剪切板

传入图片路径:如:"C:\\Users\\Administrator\\Pictures\\aaa\\NWP_130731_0000.jpg"

        /// <summary>
        /// 复制或剪切文件至剪切板
        /// </summary>
        /// <param name="files">文件路径的数组</param>
        /// <param name="cut">是否剪切;true:剪切false:复制</param>
        private void CopyToClipboard(string[] files, bool cut)
        {
            //files字段必须是定义为string[] ,要不然会出现右键有粘贴但是粘贴不成功的问题
            //"C:\\Users\\Administrator\\Pictures\\aaa\\NWP_130731_0000.jpg"
            if (files == null)
            {
                return;
            }

            IDataObject data = new DataObject(DataFormats.FileDrop, files);
            MemoryStream memo = new MemoryStream(4);
            byte[] bytes = new byte[] { (byte)(cut ? 2 : 5), 0, 0, 0 };
            memo.Write(bytes, 0, bytes.Length);
            data.SetData("Preferred DropEffect", memo);
            Clipboard.SetDataObject(data);

        }


 

二.从系统剪切板获取图片(获得图片路径:"C:\\Users\\Administrator\\Pictures\\aaa\\NWP_130731_0000.jpg")

 (1)检索剪切板中的文件名的集合方式

string[] files = new string[Clipboard.GetFileDropList().Count];
            Clipboard.GetFileDropList().CopyTo(files, 0);
            foreach (string file in files)
            {}


示例:

经典代码

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                //获得目录信息
                DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
                //获得目录文件列表
                FileInfo[] files = dir.GetFiles("*.*");
                string[] fileNames = new string[files.Length];
                int i = 0;
                foreach (FileInfo fileInfo in files)
                {
                    fileNames[i] = folderBrowserDialog1.SelectedPath + "\\" + fileInfo.Name;
                    i++;
                }
                this.listBox1.DataSource = (object)fileNames;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //获得目录信息
            string[] fileNames = new string[1];
            fileNames[0] = listBox1.SelectedItem.ToString();
            CopyToClipboard(fileNames, true);
        }

        /// <summary>
        /// 复制或剪切文件至剪切板
        /// </summary>
        /// <param name="files">文件路径的数组</param>
        /// <param name="cut">是否剪切;true:剪切false:复制</param>
        private void CopyToClipboard(string[] files, bool cut)
        {
            //files字段必须是定义为string[] ,要不然会出现右键有粘贴但是粘贴不成功的问题
            //"C:\\Users\\Administrator\\Pictures\\aaa\\NWP_130731_0000.jpg"
            if (files == null)
            {
                return;
            }

            IDataObject data = new DataObject(DataFormats.FileDrop, files);
            MemoryStream memo = new MemoryStream(4);
            byte[] bytes = new byte[] { (byte)(cut ? 2 : 5), 0, 0, 0 };
            memo.Write(bytes, 0, bytes.Length);
            data.SetData("Preferred DropEffect", memo);
            Clipboard.SetDataObject(data);

        }
    }

界面:

 

(2)检索剪切板中的数据方式

 

 

 

                //将剪贴板中的图片放到richTextBox1里
                IDataObject iData = Clipboard.GetDataObject();
                DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);
                if (iData.GetDataPresent(DataFormats.Bitmap))
                {
                    richTextBox1.Paste(myFormat);
                    //Clipboard.Clear();//清除剪贴板中的对象
                }

 

 

 

 


 

阅读更多
换一批

没有更多推荐了,返回首页