0

I am currently trying to save a windows form as an image, and I have been able to using the following code:

Dim frm = Me
    Using bmp = New Bitmap(frm.Width, frm.Height)
        frm.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
        bmp.Save("D:\programs\files\image.png")
    End Using

However, the image includes the borders of the form. Is there any way to save the image without the borders?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
em3217
  • 18
  • 5
  • 1
    Take this: [How to print hidden and visible content of a Container with ScrollBars](https://stackoverflow.com/a/57309095/7444103), pass your Form instance. Read the notes (also those related to the Framework version). – Jimi Feb 14 '21 at 20:09
  • 1
    As an alternative, you can also try this one: [How can I make a screen shot of a window behind my form?](https://stackoverflow.com/a/65317047/7444103). This can print a Window even if it's hidden (completely or partially) by another. Full frame or ClientArea only. Read the notes :) – Jimi Feb 14 '21 at 20:24

1 Answers1

2

Can you change the border style to none before the picture, then add it back after you save the picture... something like...

Dim frm = Me
frm.FormBorderStyle = FormBorderStyle.None
Using bmp = New Bitmap(frm.Width, frm.Height)
  frm.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
  bmp.Save("D:\programs\files\image.png")
End Using
frm.FormBorderStyle = FormBorderStyle.FixedSingle
JohnG
  • 9,259
  • 2
  • 20
  • 29
  • 1
    You can also do something like: `dim rect = RectangleToScreen(ClientRectangle) rect.Offset(-Left, -Top)`, draw the Form's bounds to a Bitmap, then Clone() the part you want: `bmp = bmp.Clone(rect, PixelFormat.Format32bppArgb)`. Now the Bitmap only represents the ClientRectangle's content. -- To note that, as described in the Docs, `Control.DrawToBitmap()` doesn't draw the content of some Controls, as the RichTextBox and the content of nested containers is rendered in reverse order. That's why I've posted those methods in a comment :) – Jimi Feb 14 '21 at 22:28
  • @Jimi … I am sure you are correct; I did no testing of this using different controls. This was just the first thing that came to mind. – JohnG Feb 14 '21 at 22:58