picturebox可以显示多次拖拽不同的值

想实现将listview中的值拖拽到picturebox上后,如何实现呢?

那你不要写死坐标,要把鼠标放开时的坐标作为DrawString的坐标
Graphics g = Graphics.FromImage(bmp);这里也不对,你这里是将bmp作为底图,每次在bmp上画图
你想保留上次的值,要么需要有个全局变量bmp,去反复修改它,要么取pictureBox1.CreateGraphics,直接在pictureBox1上画图

该回答引用ChatGPT4,希望能对你有帮助,你试试看看能不能解决问题
要实现将多个值从 ListView 拖拽到 PictureBox 上并在上面显示,您需要在 pictureBox1_DragDrop 方法中对现有的图片进行修改,而不是创建一个全新的图片。这里有一个修改后的版本,应该能实现您所描述的功能:

private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
    ListViewItem item = (ListViewItem)e.Item;
    string itemName = item.Text;
    listView1.DoDragDrop(itemName, DragDropEffects.Copy);
}

private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
    string itemName = (string)e.Data.GetData(DataFormats.Text);

    // 使用现有的图片或创建一个新的图片
    Bitmap bmp;
    if (pictureBox1.Image != null)
    {
        bmp = new Bitmap(pictureBox1.Image);
    }
    else
    {
        bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    }

    Graphics g = Graphics.FromImage(bmp);

    // 计算文本的位置。这里我们使用已有文本的数量来计算 Y 坐标。
    int yOffset = bmp.GetPixel(0, 0).A == 0 ? 0 : bmp.Height;
    int lineHeight = new Font("Arial", 10).Height;
    int newY = yOffset + lineHeight;

    // 如果图片空间不足,创建一个更高的图片
    if (newY + lineHeight > bmp.Height)
    {
        Bitmap newBmp = new Bitmap(bmp.Width, bmp.Height + lineHeight);
        using (Graphics newG = Graphics.FromImage(newBmp))
        {
            newG.DrawImage(bmp, 0, 0);
        }
        bmp = newBmp;
        g = Graphics.FromImage(bmp);
    }

    g.DrawString(itemName, new Font("Arial", 10), Brushes.Red, new Point(10, newY));
    pictureBox1.Image = bmp;
}

这个修改后的方法首先检查 PictureBox 控件是否已经有一个图片。如果有,它会在现有的图片上修改。如果没有,它会创建一个新的图片。然后,我们计算文本的位置,确保每个新拖拽的文本在前一个文本的下方。如果图片空间不足以容纳新的文本,我们会创建一个更高的图片,并将现有的图片内容复制到新图片上。

这样,您就可以将多个值从 ListView 拖拽到 PictureBox 上,新拖拽的值会出现在前一个值的下方,而不是替换前一个值。

预留一个缓存数据,记录当前画布上的数据,当松开拖拽结束,缓存数据更新,画布根据缓存数据作画

如果需要插入到前边的行,或者数字中间,还需要计算松开位置是否有内容

该回答引用GPTᴼᴾᴱᴺᴬᴵ
您可以在拖放事件中使用图像列表而不是单个图像来显示所有拖放的值。在拖放事件中,将每个值添加到图像列表中,然后将该列表分配给pictureBox的Image属性。

以下是修改后的代码示例:

private List<string> draggedItems = new List<string>();

private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
ListViewItem item = (ListViewItem)e.Item;
string itemName = item.Text;
listView1.DoDragDrop(itemName, DragDropEffects.Copy);
}

private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
string itemName = (string)e.Data.GetData(DataFormats.Text);
draggedItems.Add(itemName);
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);

// Draw each item in the list
int yOffset = 10;
foreach (string item in draggedItems)
{
    g.DrawString(item, new Font("Arial", 10), Brushes.Red, new Point(10, yOffset));
    yOffset += 20;
}

pictureBox1.Image = bmp;
}

请注意,在此示例中,我们将字符串列表draggedItems用于存储拖放的值。在每个拖放操作中,我们将每个拖放的值添加到列表中,然后在pictureBox上使用该列表来绘制图像。每个项目的垂直偏移量是10,并且每个项目的高度为20。