Link to home
Start Free TrialLog in
Avatar of karelog
karelogFlag for Chile

asked on

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
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

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.


ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KingFlash
KingFlash

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

*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
Your approach may very well work...we don't know anything about the control.  =)
Avatar of karelog

ASKER

Ok,

I'm checking the possible solutions.

Many thanks!
Avatar of karelog

ASKER

It worked perfect!
thanks :D