Paste the contents of the bitmap into the PictureBox

I am currently writing a small drawing application where the user can draw on the panel. I am working on a selection tool and want to be able to select a specific area of ​​the panel, and then paste this selected area directly into the PictureBox, which I have only to the right of the panel.

My problem is that my code currently does not work correctly, when I try to insert the Bitmap that I create from the panel, I get a big red X in the PictureBox instead of the actual image. I know that the image is copied to Bitmap correctly, because I tried to put some code around it to save it to disk as jpeg, and then look at the image, and it all looks fine.

Here is my code:

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; } } 

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

Any help would be greatly appreciated.

0
source share
1 answer

Your problem is that using(){} , when the code inside the braces is completed, the object inside () is deleted, since it is believed that it is no longer needed.

Just remove the bracket to just Bitmap bmp = new Bitmap(width, height) solve your problem

+5
source

All Articles