Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Create Image from Panel Control

Create Image from Panel Control

Scheduled Pinned Locked Moved Visual Basic
csharptutorialquestion
16 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gwittlock
    wrote on last edited by
    #1

    I thought I seen an example up here before But I will be dang if I can find it now. I have a panel control which has geometry created on it from a third party dll. What I would like to do is that and create an image (preferably with transparent background) of what the user is seeing on that screen. can anyone point me in the right direction? Oh I guess I should mention that I am using VS2013 vb.net Thanks

    L 1 Reply Last reply
    0
    • G gwittlock

      I thought I seen an example up here before But I will be dang if I can find it now. I have a panel control which has geometry created on it from a third party dll. What I would like to do is that and create an image (preferably with transparent background) of what the user is seeing on that screen. can anyone point me in the right direction? Oh I guess I should mention that I am using VS2013 vb.net Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      A panel is a control, and those can be drawn to a bitmap. See MSDN[^] :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      G 1 Reply Last reply
      0
      • L Lost User

        A panel is a control, and those can be drawn to a bitmap. See MSDN[^] :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        G Offline
        G Offline
        gwittlock
        wrote on last edited by
        #3

        Thanks for the advice but that does not create a transparent bitmap and in most cases will not work if the actual drawing to the panel is done through a 3rd party dll. I think what I really need is more of a screen capture of the panel and then convert its size and transparentcy

        G 1 Reply Last reply
        0
        • G gwittlock

          Thanks for the advice but that does not create a transparent bitmap and in most cases will not work if the actual drawing to the panel is done through a 3rd party dll. I think what I really need is more of a screen capture of the panel and then convert its size and transparentcy

          G Offline
          G Offline
          gwittlock
          wrote on last edited by
          #4

          I found this bit of code to work with 2 exceptions. 1) how to re-size it to a smaller image (So I can display a thumbnail type image and 2) how to make the background transparent. I want item 2 because the image is displayed in a panel control with a black background. That is what the user needs to see. But when I create the image I do not want the black background

          Private Sub my_capture(ctrl As Control, fileName As String)
          Dim bounds As Rectangle = ctrl.Bounds
          Dim pt As Point = ctrl.PointToScreen(bounds.Location)
          Dim bitmap As New Bitmap(bounds.Width, bounds.Height)
          Using g As Graphics = Graphics.FromImage(bitmap)
          g.CopyFromScreen(New Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size)
          End Using

              bitmap.Save(fileName, ImageFormat.Png)
          End Sub
          
          L 1 Reply Last reply
          0
          • G gwittlock

            I found this bit of code to work with 2 exceptions. 1) how to re-size it to a smaller image (So I can display a thumbnail type image and 2) how to make the background transparent. I want item 2 because the image is displayed in a panel control with a black background. That is what the user needs to see. But when I create the image I do not want the black background

            Private Sub my_capture(ctrl As Control, fileName As String)
            Dim bounds As Rectangle = ctrl.Bounds
            Dim pt As Point = ctrl.PointToScreen(bounds.Location)
            Dim bitmap As New Bitmap(bounds.Width, bounds.Height)
            Using g As Graphics = Graphics.FromImage(bitmap)
            g.CopyFromScreen(New Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size)
            End Using

                bitmap.Save(fileName, ImageFormat.Png)
            End Sub
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You'd have to specify which color should become transparent[^].

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            G 1 Reply Last reply
            0
            • L Lost User

              You'd have to specify which color should become transparent[^].

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              G Offline
              G Offline
              gwittlock
              wrote on last edited by
              #6

              I changed my code to this: But it had no effect. I still get the image with a black background. I am not sure how to include images with the reply otherwise I would show you before and after images

              Dim bounds As Rectangle = ctrl.Bounds
              Dim pt As Point = ctrl.PointToScreen(bounds.Location)
              Dim myBitmap As New Bitmap(bounds.Width, bounds.Height)

                  myBitmap.MakeTransparent(ctrl.BackColor)
              
                  Using m\_g As Graphics = Graphics.FromImage(myBitmap)
                      m\_g.CopyFromScreen(New Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size)
                      m\_g.Dispose()
                  End Using
              
                  myBitmap.Save(fileName, ImageFormat.Png)
                  myBitmap.Dispose()
              
              D L 2 Replies Last reply
              0
              • G gwittlock

                I changed my code to this: But it had no effect. I still get the image with a black background. I am not sure how to include images with the reply otherwise I would show you before and after images

                Dim bounds As Rectangle = ctrl.Bounds
                Dim pt As Point = ctrl.PointToScreen(bounds.Location)
                Dim myBitmap As New Bitmap(bounds.Width, bounds.Height)

                    myBitmap.MakeTransparent(ctrl.BackColor)
                
                    Using m\_g As Graphics = Graphics.FromImage(myBitmap)
                        m\_g.CopyFromScreen(New Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size)
                        m\_g.Dispose()
                    End Using
                
                    myBitmap.Save(fileName, ImageFormat.Png)
                    myBitmap.Dispose()
                
                D Offline
                D Offline
                drago11
                wrote on last edited by
                #7

                Hi Imports System.Drawing.Imaging Imports System.Drawing.Printing Private Sub PrintDocument1_PrintPage(ByVal Sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Static page As Integer = 1 Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height Static maxPages As Integer = 0 If page = 1 Then For Each ctrl As Control In Me.pRight19.Controls If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Or TypeOf ctrl Is RichTextBox Then ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1 If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag) End If Next End If For Each ctrl As Control In Me.pRight19.Controls If CInt(ctrl.Tag) = page Then If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is RichTextBox Then Dim sf As New System.Drawing.StringFormat If TypeOf ctrl Is TextBox Then If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then sf.Alignment = StringAlignment.Far Else sf.Alignment = StringAlignment.Near End If ElseIf TypeOf ctrl Is Label Then If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then sf.Alignment = StringAlignment.Far End If ElseIf TypeOf ctrl Is RichTextBox Then If DirectCast(ctrl, RichTextBox).SelectionAlignment = ContentAlignment.TopLeft Then sf.Alignment = StringAlignment.Near End If End If sf.FormatFlags = StringFormatFlags.NoClip e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New Rectangle(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf) ElseIf TypeOf ctrl Is PictureBox Then e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition)) End If End If Next page += 1

                G L 2 Replies Last reply
                0
                • G gwittlock

                  I changed my code to this: But it had no effect. I still get the image with a black background. I am not sure how to include images with the reply otherwise I would show you before and after images

                  Dim bounds As Rectangle = ctrl.Bounds
                  Dim pt As Point = ctrl.PointToScreen(bounds.Location)
                  Dim myBitmap As New Bitmap(bounds.Width, bounds.Height)

                      myBitmap.MakeTransparent(ctrl.BackColor)
                  
                      Using m\_g As Graphics = Graphics.FromImage(myBitmap)
                          m\_g.CopyFromScreen(New Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size)
                          m\_g.Dispose()
                      End Using
                  
                      myBitmap.Save(fileName, ImageFormat.Png)
                      myBitmap.Dispose()
                  
                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Set the transparency AFTER you capture the screen.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  G 1 Reply Last reply
                  0
                  • L Lost User

                    Set the transparency AFTER you capture the screen.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                    G Offline
                    G Offline
                    gwittlock
                    wrote on last edited by
                    #9

                    Eddy yep that is what the issue was. Setting it after did the trick. Now that I have that working(thanks to you) and I see the results, I think I need to save the image as B/W or greyScale. Is that difficult?

                    L 1 Reply Last reply
                    0
                    • D drago11

                      Hi Imports System.Drawing.Imaging Imports System.Drawing.Printing Private Sub PrintDocument1_PrintPage(ByVal Sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Static page As Integer = 1 Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height Static maxPages As Integer = 0 If page = 1 Then For Each ctrl As Control In Me.pRight19.Controls If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Or TypeOf ctrl Is RichTextBox Then ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1 If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag) End If Next End If For Each ctrl As Control In Me.pRight19.Controls If CInt(ctrl.Tag) = page Then If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is RichTextBox Then Dim sf As New System.Drawing.StringFormat If TypeOf ctrl Is TextBox Then If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then sf.Alignment = StringAlignment.Far Else sf.Alignment = StringAlignment.Near End If ElseIf TypeOf ctrl Is Label Then If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then sf.Alignment = StringAlignment.Far End If ElseIf TypeOf ctrl Is RichTextBox Then If DirectCast(ctrl, RichTextBox).SelectionAlignment = ContentAlignment.TopLeft Then sf.Alignment = StringAlignment.Near End If End If sf.FormatFlags = StringFormatFlags.NoClip e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New Rectangle(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf) ElseIf TypeOf ctrl Is PictureBox Then e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition)) End If End If Next page += 1

                      G Offline
                      G Offline
                      gwittlock
                      wrote on last edited by
                      #10

                      Drago11 I am not sure what all the printing code has to do with my question. Maybe you just pasted it in to the wrong question. Am I missing something?

                      1 Reply Last reply
                      0
                      • G gwittlock

                        Eddy yep that is what the issue was. Setting it after did the trick. Now that I have that working(thanks to you) and I see the results, I think I need to save the image as B/W or greyScale. Is that difficult?

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Not very hard, but I reckon it will need to remain a 32bit argb format for transparency to work. There's a howto[^] on MSDN :)

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                        G 2 Replies Last reply
                        0
                        • D drago11

                          Hi Imports System.Drawing.Imaging Imports System.Drawing.Printing Private Sub PrintDocument1_PrintPage(ByVal Sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Static page As Integer = 1 Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height Static maxPages As Integer = 0 If page = 1 Then For Each ctrl As Control In Me.pRight19.Controls If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Or TypeOf ctrl Is RichTextBox Then ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1 If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag) End If Next End If For Each ctrl As Control In Me.pRight19.Controls If CInt(ctrl.Tag) = page Then If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is RichTextBox Then Dim sf As New System.Drawing.StringFormat If TypeOf ctrl Is TextBox Then If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then sf.Alignment = StringAlignment.Far Else sf.Alignment = StringAlignment.Near End If ElseIf TypeOf ctrl Is Label Then If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then sf.Alignment = StringAlignment.Far End If ElseIf TypeOf ctrl Is RichTextBox Then If DirectCast(ctrl, RichTextBox).SelectionAlignment = ContentAlignment.TopLeft Then sf.Alignment = StringAlignment.Near End If End If sf.FormatFlags = StringFormatFlags.NoClip e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New Rectangle(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf) ElseIf TypeOf ctrl Is PictureBox Then e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition)) End If End If Next page += 1

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          That's the good answer, but to the wrong question :laugh:

                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                          1 Reply Last reply
                          0
                          • L Lost User

                            Not very hard, but I reckon it will need to remain a 32bit argb format for transparency to work. There's a howto[^] on MSDN :)

                            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                            G Offline
                            G Offline
                            gwittlock
                            wrote on last edited by
                            #13

                            Hmmm that use the Imaging class I have seen that used in medical applications I believe, Maybe I will try a different approach. You would think that Microsoft would of made saving an image a whole lot easier wouln't you? :laugh:

                            1 Reply Last reply
                            0
                            • L Lost User

                              Not very hard, but I reckon it will need to remain a 32bit argb format for transparency to work. There's a howto[^] on MSDN :)

                              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                              G Offline
                              G Offline
                              gwittlock
                              wrote on last edited by
                              #14

                              I am using the this sub and on the following line of code I get an error

                              pixColor = myBitmap.GetPixel(x, y)

                              the error states "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Drawing.dll Additional information: Parameter must be positive and < Width." Would anyone know why?

                              Private Sub my_capture(ctrl As Control, fileName As String)
                              Dim bounds As Rectangle = ctrl.Bounds
                              Dim pt As Point = ctrl.PointToScreen(bounds.Location)
                              Dim myBitmap As New Bitmap(bounds.Width, bounds.Height)
                              Dim BW_Bitmap As New Bitmap(bounds.Width, bounds.Height)
                              Dim x As Integer
                              Dim y As Integer
                              Dim pixColor As Color
                              Dim luma As Integer

                                  Using m\_g As Graphics = Graphics.FromImage(myBitmap)
                              
                                      m\_g.CopyFromScreen(New Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size)
                              
                                      m\_g.Dispose()
                              
                                  End Using
                              
                                  For y = 0 To BW\_Bitmap.Height
                              
                                      For x = 0 To BW\_Bitmap.Width
                              
                                          pixColor = myBitmap.GetPixel(x, y)
                              
                                          luma = CInt(pixColor.R \* 0.3 + pixColor.G \* 0.59 + pixColor.B \* 0.11)
                              
                                          BW\_Bitmap.SetPixel(x, y, Color.FromArgb(luma, luma, luma))
                              
                                      Next
                              
                                  Next
                              
                                  BW\_Bitmap.Save(fileName, ImageFormat.Png)
                              
                                  BW\_Bitmap.Dispose()
                              
                                  myBitmap.Dispose()
                              
                              End Sub
                              
                              L 1 Reply Last reply
                              0
                              • G gwittlock

                                I am using the this sub and on the following line of code I get an error

                                pixColor = myBitmap.GetPixel(x, y)

                                the error states "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Drawing.dll Additional information: Parameter must be positive and < Width." Would anyone know why?

                                Private Sub my_capture(ctrl As Control, fileName As String)
                                Dim bounds As Rectangle = ctrl.Bounds
                                Dim pt As Point = ctrl.PointToScreen(bounds.Location)
                                Dim myBitmap As New Bitmap(bounds.Width, bounds.Height)
                                Dim BW_Bitmap As New Bitmap(bounds.Width, bounds.Height)
                                Dim x As Integer
                                Dim y As Integer
                                Dim pixColor As Color
                                Dim luma As Integer

                                    Using m\_g As Graphics = Graphics.FromImage(myBitmap)
                                
                                        m\_g.CopyFromScreen(New Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size)
                                
                                        m\_g.Dispose()
                                
                                    End Using
                                
                                    For y = 0 To BW\_Bitmap.Height
                                
                                        For x = 0 To BW\_Bitmap.Width
                                
                                            pixColor = myBitmap.GetPixel(x, y)
                                
                                            luma = CInt(pixColor.R \* 0.3 + pixColor.G \* 0.59 + pixColor.B \* 0.11)
                                
                                            BW\_Bitmap.SetPixel(x, y, Color.FromArgb(luma, luma, luma))
                                
                                        Next
                                
                                    Next
                                
                                    BW\_Bitmap.Save(fileName, ImageFormat.Png)
                                
                                    BW\_Bitmap.Dispose()
                                
                                    myBitmap.Dispose()
                                
                                End Sub
                                
                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                ..from 0 to 640, is 641 steps, not 640. That's one more step than there is pixels, causing it to go out of range.

                                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                G 1 Reply Last reply
                                0
                                • L Lost User

                                  ..from 0 to 640, is 641 steps, not 640. That's one more step than there is pixels, causing it to go out of range.

                                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                  G Offline
                                  G Offline
                                  gwittlock
                                  wrote on last edited by
                                  #16

                                  Well :doh: ! Boy do I feel stupid. Thanks

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups