将Bitmap的内容粘贴到PictureBox中 [英] Paste the contents of a Bitmap into a PictureBox

查看:588
本文介绍了将Bitmap的内容粘贴到PictureBox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个小油漆应用程序,用户可以在面板上绘制。我正在选择工具,并希望能够选择面板的某个区域,然后将这个选定的区域直接粘贴到我刚刚在面板右侧的PictureBox。

I'm currently writing a little paint application where the user is able to draw on a Panel. I am working on the select tool and want to be able to select a certain area of the Panel, and then paste this selected area directly into a PictureBox that I have just to the right of the Panel.

我的问题是,我的代码在这一刻是不能正常工作,当我尝试粘贴我从面板创建的Bitmap我得到一个大红色X在PictureBox而不是实际图像。我知道图像正在复制到Bitmap正确,因为我试图把一些代码围绕它保存到磁盘作为一个jpeg,然后看看图像,它都显示很好。

My problem is that my code at the moment is not working correctly, when I try to paste the Bitmap that I am creating from the panel I am getting a big red X in the PictureBox instead of the actual image. I know that the image is copying to the Bitmap correctly because I tried putting some code around it to save it to disk as a jpeg and then look at the image, and it is all displaying fine.

这是我的代码:

private void tbCopy_Click(object sender, EventArgs e)
{
    int width = selectList[0].getEnd().X - selectList[0].getInitial().X;
    int height = selectList[0].getEnd().Y - selectList[0].getInitial().Y;

    using (Bitmap bmp = new Bitmap(width, height))
    {
        pnlDraw.DrawToBitmap(bmp, new System.Drawing.Rectangle(
                                      selectList[0].getInitial().X,
                                      selectList[0].getInitial().Y, 
                                      width, height));
        pbPasteBox.Image = bmp;             
    }
}   

宽度和高度只是区域的尺寸我想要复制,selectList是一个包含一个对象的列表,其中包含我要复制的区域的坐标。

the width and height are just the dimensions of the area that I want to copy, and selectList is a List that contains one object which contains the coordinates of the area I want to copy.

任何帮助将非常感谢。 / p>

Any help would be greatly appreciated.

推荐答案

您的问题是 using(){}

只要删除大括号就可以使用 Bitmap bmp = new Bitmap(width,height)应该可以解决你的问题

Simply removing the brace to just have Bitmap bmp = new Bitmap(width, height) should solve your problem

这篇关于将Bitmap的内容粘贴到PictureBox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆