I have a panel, how can I save a picture of what is visible in that panel to file as a bmp?
Printable View
I have a panel, how can I save a picture of what is visible in that panel to file as a bmp?
Try this:
vb.net Code:
Using bmp As New Bitmap(Me.Panel1.Width, Me.Panel1.Height) Me.Panel1.DrawToBitmap(bmp, Me.Panel1.ClientRectangle) bmp.Save("C:\test\panel.bmp", Imaging.ImageFormat.Bmp) End Using
close...
That just draws that panel. the panel has 1+ picture boxes in it, and the provided code only draws that panel, and not the picture boxes inside of it.
Help is appreciated!
ForumAccount's code actually does draw the child controls of the Panel. It's just that they are drawn off the visible area of the Bitmap because the second argument to DrawToBitmap is incorrect. The targetBounds is relative to the Bitmap being drawn on, not the form containing the control:vb.net Code:
Using bmp As New Bitmap(Me.Panel1.Width, Me.Panel1.Height) Me.Panel1.DrawToBitmap(bmp, New Rectangle(Point.Empty, bmp.Size)) bmp.Save("C:\test\panel.bmp", Imaging.ImageFormat.Bmp) End Using
OK! it works, but for some reason, when I put a picture box in, then place another on top of that one, it draws it reverse that...
For example:
Image A: 100x100
Image B: 200x200
If I first place "Image B" then paste "Image A" on top of that in the program it looks fine, but when I do the save, "Image B" is on top of "Image A" so "Image A" isn't visible.
If I first place "Image A" then paste "Image B" on top of that in the program it does not look fine, but when I do the save, "Image A" is on top of "Image B" so Now it looks like it did in the previous paragraph.
Why is it doing that?
What would be the best way to reverse them?
The order of the items in the Controls collection sets the z-order. You can use a loop and call SetChildIndex on the Controls collection or you can call BringToFront or SendToBack on the controls themselves. Just be aware that each time you make a change the order of the items in the collection will change, so you can't just loop as though they stay put. Think about it and experiment a bit and come up with some logic that will work. You don't even have to loop through the Controls collection itself. You can put all the controls into an array or collection first and loop through that. That way their positions in the list you're looping through won't change as they change in the Controls collection.
Alright, I got it!
If you have any solutions to this, please let me know!