We help IT Professionals succeed at work.

How to save a bmp from a control?

Hello everybody!

I have the following issue,
I need to take a picture from a control and save it as BMP.
As you can see in the image attached, you can see that I have a windows form with some sort of controls. I need to take the picture from the chart.
Then, the picture I took must be shown in the upper right panel.

In advance, many thanks :D

PS: As you can see, I'm not being able to capture the exact area.
PS2: Attaching Code :D
Private Function CapturarPintura() As Bitmap

        Dim bm As Bitmap
        Dim gr As Graphics = Me.CreateGraphics
        Dim fSize As Size = Me.GB_Grafico.Size

        bm = New Bitmap(Me.GB_Grafico.Width, Me.GB_Grafico.Height, gr)
        Dim gr2 As Graphics = Graphics.FromImage(bm)
        Dim pt As New System.Drawing.Point
        pt = Me.GB_Grafico.Location

        gr2.CopyFromScreen(Me.Location.X + pt.X, Me.Location.Y + pt.Y, 0, 0, fSize)

        Return bm
    End Function

Open in new window

form.png
Comment
Watch Question

Shahan AyyubSenior Software Engineer

Commented:
Try this:

Since pt always greater than Me.Location (as seen from figure). So,

dim X as Integer = pt.X - Me.Location.X  
dim Y as Integer = pt.Y - Me.Location.Y

gr2.CopyFromScreen(Me.Location.X  + X, Me.Location.Y + Y, 0, 0, fSize)

The X and Y I calculated is so because you need the offset between the Form and your control and then you will add that offset into Me.Location. The X and Y are the offset.

I think this should do it.


High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Try this one:
(assuming the graph control is contained directly by the form)
    Private Function CapturarPintura() As Bitmap
        Dim bm As New Bitmap(Me.GB_Grafico.Width, Me.GB_Grafico.Height)
        Dim gr As Graphics = Graphics.FromImage(bm)
        Dim rc as Rectangle = Me.RectangleToScreen(New Rectangle(Me.GB_Grafico.Location, Me.GB_Grafico.Size))
        gr.CopyFromScreen(rc.Location.X, rc.Location.Y, 0, 0, Me.GB_Grafico.Size)
        gr.Dispose()
        Return bm
    End Function

Open in new window

No need to now the location of the screen you can use the following code,

       Try
            Dim b As New Bitmap(Button1.Size.Width, Button1.Size.Height)
            Button1.DrawToBitmap(b, New Rectangle(New Point(0, 0), Button1.Size))
            b.Save("temp.bmp")
            PictureBox1.Image = Image.FromFile("temp.bmp")
            System.IO.File.Delete("temp.bmp")
        Catch ex As Exception

        End Try




just replace the "Button1" with your control name.
and PictureBox1 With the picture box name.

:)

Try
            Dim b As New Bitmap(Button1.Size.Width, Button1.Size.Height)
            Button1.DrawToBitmap(b, New Rectangle(New Point(0, 0), Button1.Size))
            b.Save("temp.bmp")
            PictureBox1.Image = Image.FromFile("temp.bmp")
            System.IO.File.Delete("temp.bmp")
        Catch ex As Exception

        End Try

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
*That's assuming the "graph" control is a built-in .Net control that has exposed the native DrawToBitmap() function.  It doesn't appear to be a built-in control but I could be wrong...
Hi Idle Mind ,
Yes , but what if it inherits it ? :D
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Your approach may very well work...we don't know anything about the control.  =)

Author

Commented:
Ok,

I'm checking the possible solutions.

Many thanks!

Author

Commented:
It worked perfect!
thanks :D