C # generate a grayscale image: Drag the image to the picturebox and Drag the picturebox image to the Resource Manager (Drag & drop)

Source: Internet
Author: User

It took two nights to generate a grayscale image and used the ColorMatrix class. We wanted to set a 5*5 parameter matrix. I didn't understand the matrix copied on MSDN, it takes a lot of time to understand errors in two places when dragging, as recorded here:

1. Drag in. The e. Data. GetData (DataFormats. FileDrop) parameter is initially considered as DataFormats. Bitmap, and the returned Data is considered as Bitmap Data. The correct code is as follows:

 

Private void flowLayoutPanel _ Original Image _ 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. Drag-out: Put the stored data into a DataObject and set the DataFormats. bitmap format to indicate the data type. In fact, it is also wrong. In fact, the data to be saved is saved as a file, and the path of the file is used as the data, and DataFormats. FileDrop is used as the parameter, so that the drag effect can be completed. The key is DoDragDrop (new DataObject (DataFormats. FileDrop, files ),

DragDropEffects. Move/* | DragDropEffects. Link */); note that files is a string [] array. It takes a long time to find the cause when a string is always used. The complete code is as follows:

 

 

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 */);
}
}

}

 

 

Paste a picture:

 

 

 

Code download

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.