zoukankan      html  css  js  c++  java
  • C#生成灰度图片:拖动图片到picturebox显示,拖动picturebox图片到资源管理器 (Drag & drop )

    用了两个晚上,生成灰度图片用到了ColorMatrix类,要设置一个5*5的参数矩阵,不懂那个在MSDN上抄了他的矩阵,在做拖动时有两个地方理解错误浪费了很多时间,记录在此:

    1.拖进,e.Data.GetData(DataFormats.FileDrop)的参数一开始认为是DataFormats.Bitmap,返回的Data认为是Bitmap的数据其实错了,正确的代码如下:

      

     private void flowLayoutPanel_原图片_DragDrop(object sender, DragEventArgs e)
            {
                
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    
    string[] fileList = (string[])(e.Data.GetData(DataFormats.Bitmap));
                    
    foreach (string fileName in fileList)
                    {
                        Image image 
    = Image.FromFile(fileName);
                        PictureBox pictureBox 
    = new PictureBox();
                        pictureBox.Size 
    = new Size(this.PicWidth, this.PicHeight);
                        pictureBox.SizeMode 
    = PictureBoxSizeMode.StretchImage;
                        pictureBox.Image 
    = image;
                        FileInfo info 
    = new FileInfo(fileName);
                        pictureBox.Tag 
    = info.Name;
                        
    this.flowLayoutPanel1.Controls.Add(pictureBox);
                    }
                }

            }

    2.拖出,认为拖出就是要把保存的数据放到一个DataObject中通过设置 DataFormats.bitmap格式表明数据类型,其实也错了。拖出实际上就是把要保存的数据保存成文件,然后将该文件的路径做为数据,使用DataFormats.FileDrop做为参数,这样就能完成拖出效果了。关键就是这一句     DoDragDrop(new DataObject(DataFormats.FileDrop, files),

    DragDropEffects.Move /* | DragDropEffects.Link */);,注意files是string[]数组,我用一个string总是出不来,用了好久才找到这个原因。完整代码如下:

        void pictureBox_MouseMove(object sender, MouseEventArgs e)
            {
                PictureBox pictureBox 
    = sender as PictureBox;
                
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
                {
                    
    if (dragBoxFromMouseDown != Rectangle.Empty &&
                        
    !dragBoxFromMouseDown.Contains(e.X, e.Y))
                    {
                        DataObject dataObject 
    = new DataObject();
                        
    string imageName = pictureBox.Tag.ToString().Substring(0, pictureBox.Tag.ToString().LastIndexOf(".")) + "_gray" + pictureBox.Tag.ToString().Substring(pictureBox.Tag.ToString().LastIndexOf("."));
                        
    string fileName = this.CreateTemporaryFileName(imageName, pictureBox.Image);
                        
    string[] files = new string[1] { fileName };

                        DoDragDrop(
    new DataObject(DataFormats.FileDrop, files), DragDropEffects.Move /* | DragDropEffects.Link */);
                    }
                }

            }

    贴个图吧:

     

     代码下载

  • 相关阅读:
    UVa 10088 (Pick定理) Trees on My Island
    LA 3295 (计数 容斥原理) Counting Triangles
    LA 5846 (计数) Neon Sign
    java是什么?软帝学院告诉你学Java能做什么?Java有什么特性?
    【软帝学院】一套好的java基础教学视频需要哪些有哪些内容
    推荐五个java基础学习网站,小白必备
    学习java设计模式有用吗?懂这六个原则,编程更轻松
    Java是什么?只需5分钟,了解java必须要知道的知识点
    软帝学院:自学java到底难不难?做好这几步,少走3年弯路
    软帝学院:java开发程序很难吗?学会这十步,5分钟搞定一个程序
  • 原文地址:https://www.cnblogs.com/zhaobl/p/1621476.html
Copyright © 2011-2022 走看看