本文介绍了将图片在图片框中拖放到另一个图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一张带有2个图片盒的表格。在picturebox1中有一张照片。 picturebox2是空的。 使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用 System.Data; 使用 System.Drawing; 使用 System.Linq; 使用 System.Text; 使用 System.Threading.Tasks; 使用 System.Windows.Forms; 命名空间 project_challenge { public partial class Form1:Form { public Form1() { InitializeComponent(); } private void pictureBox1_MouseDown( object sender,MouseEventArgs e) { pictureBox1.DoDragDrop(pictureBox1.Image,DragDropEffects.All); } private void pictureBox2_DragEnter( object sender,DragEventArgs e) { e.Effect = DragDropEffects.All; } private void pictureBox2_DragDrop( object sender,DragEventArgs e) { pictureBox2.Image =(Image)e.Data.GetData(DataFormats.Bitmap); } private void Form1_Load( object sender,EventArgs e) { pictureBox2.AllowDrop = true ; } } } 当我将图片从picturebox1拖入picturebox2我看到两个盒子里的图片...... 我该怎么改变所以它只出现在第二个图片盒里? i不想要写picturebox2.image =什么都没有,因为当这个项目完成后,我将有10个使用拖放的图片框。我想写一个代码,清除图片框,我拖动图片... i希望你理解我的概念 greetings,Quinn 解决方案 你已经完成了解决问题所需的全部工作。将 Nothing 分配给 Image 确实是解决方案。你的我不想要......清楚地表明问题不是使用API​​而是你UI的设计。你10个图片盒与技术问题无关;你有这些图片框,因为你创建了它们。到目前为止,您似乎想要清除图片框,并且不想同时清除它。检查你的设计。如果您需要在这里获得一些帮助,请清楚地解释移动图像的目的。例如,我无法看到你在哪里可以合理地期待拖拽的好处。 -SA 代码位于 Control.DoDragDrop方法 [ ^ ]包含使用带有两个ListBox的DragDrop方法的示例。 正如我前面提到的,我不相信你可以使用PictureBoxes DragDrop。但你可以在mousedown事件中使用其他容器,例如Panel。 ,我将发件人放在一个变量中 String dragSource; 控制c =(发送者作为控制); if (c!= null ) dragSource = c.Name; 然后当我将图像拖到我的第二个图片框时,我可以在变量的帮助下清除第一个图片框 ((PictureBox)this.Controls [dragSource])。Image = null; 现在我只需要创建一个事件处理程序来使用每个代码图片框... hi,i have a form with 2 pictureboxes. in picturebox1 there is a picture. picturebox2 is empty. using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace project_challenge{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All); } private void pictureBox2_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.All; } private void pictureBox2_DragDrop(object sender, DragEventArgs e) { pictureBox2.Image = (Image)e.Data.GetData(DataFormats.Bitmap); } private void Form1_Load(object sender, EventArgs e) { pictureBox2.AllowDrop = true; } }}when i drag the picture from picturebox1 into picturebox2 i see the picture in both boxes...what should i change so it only appears in the second picturebox?i dont want to write picturebox2.image = nothing because when this project is done i will have like 10 pictureboxes that use drag and drop. i want to write a code that sais clear the picturebox where i dragged the picture from...i hope u understand my conceptgreetings, Quinn 解决方案 You already formulated all you needed to address the problem at all. Assigning Nothing to the Image is really the solution. Your "I don't want want…" clearly indicates that the problem is not the use of the API but the design of you UI. You "10 picture boxes" are irrelevant to the technical problem; you have those picture boxes because you created them. So far, it looks like you want to clear the picture box and don't want to clear it at the same time. Review your design. If you need to get some help here, please clearly explain the purpose of moving the image. I, for example, cannot see where you would reasonably expect some benefits of the drag-n-drop.—SAThe code found at Control.DoDragDrop Method [^] contains an example of using the DragDrop method with two ListBoxes.As I mentioned earlier, I do not belive that you can DragDrop using PictureBoxes. But you can with other containers such as Panel.in the mousedown event i have placed the sender in a variableString dragSource;Control c = (sender as Control); if (c != null) dragSource = c.Name;then when i drag the image to my second picturebox i can clear the first picturebox with help of the variable((PictureBox)this.Controls[dragSource]).Image=null;now i just have to create a event handler to use the code on every picturebox... 这篇关于将图片在图片框中拖放到另一个图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-27 11:05
查看更多