Exporter Graphe vers Bitmap

diegosaure Messages postés 27 Date d'inscription mardi 27 mars 2007 Statut Membre Dernière intervention 18 juin 2008 - 6 févr. 2008 à 19:50
debutant VB Messages postés 93 Date d'inscription lundi 3 juillet 2006 Statut Membre Dernière intervention 25 juin 2010 - 9 févr. 2008 à 14:08
Bonjour à tous,

J'ai un objet MSChart que je voudrai capturer afin de le coller dans un fichier bitmap tout simplement.
Merci de me dire comment m'y prendre.

Julien

2 réponses

debutant VB Messages postés 93 Date d'inscription lundi 3 juillet 2006 Statut Membre Dernière intervention 25 juin 2010
7 févr. 2008 à 21:06
J'ai trouvé une façon de le faire (qui n'est sans doute pas la meilleure mais bon ...)


Voici le code :   "


Option Explicit


Private Declare Function BitBlt Lib "gdi32.dll" ( _
        ByVal hDestDC As Long, _
        ByVal x As Long, _
        ByVal y As Long, _
        ByVal nWidth As Long, _
        ByVal nHeight As Long, _
        ByVal hSrcDC As Long, _
        ByVal xSrc As Long, _
        ByVal ySrc As Long, _
        ByVal dwRop As Long) As Long


Private Declare Function GetDesktopWindow Lib _
        "user32.dll" () As Long


Private Declare Function GetDC Lib "user32.dll" ( _
        ByVal hWnd As Long) As Long


Private Const SRCCOPY As Long = &HCC0020


Public Sub SaveMSChart(ByRef Pic As PictureBox, ByRef PicD As PictureBox, _
        ByRef mform As Form, ByVal mChart As MSChart, _
        ByVal mFileName As String)
    Pic.Visible = False
    Pic.AutoRedraw = True
    Pic.Width = Screen.Width
    Pic.Height = Screen.Height
    PicD.Visible = False
    ScreenShot Pic
    mform.ScaleMode = vbTwips
    Dim XU As Double, YU As Double
    Dim XD As Double, YD As Double
    XU = mform.Left + (mform.Width - mform.ScaleWidth) + mChart.Left
    YU = mform.Top + (mform.Height - mform.ScaleHeight) + mChart.Top
    XD = XU + mChart.Width
    YD = YU + mChart.Height
    Pic.ScaleMode = vbPixels
    PicD.AutoRedraw = True
    PicD.ScaleMode = vbPixels
    PicD.Width = (XD - XU)
    PicD.Height = (YD - YU)
    XU = XU / Screen.TwipsPerPixelX
    YU = YU / Screen.TwipsPerPixelY
    XD = XD / Screen.TwipsPerPixelX
    YD = YD / Screen.TwipsPerPixelY
    Dim numX As Integer, numY As Integer
    For numY = 0 To CInt(YD - YU)
        For numX = 0 To CInt(XD - XU)
            PicD.PSet (numX - 1, numY - 1), _
                    Pic.Point(numX + CInt(XU) - 1, numY + CInt(YU) - 1)
            DoEvents
        Next numX
    Next numY
    SavePicture PicD.Image, mFileName
End Sub


Private Sub ScreenShot(Pic As PictureBox)
    BitBlt Pic.hDC, 0&, 0&, Screen.Width, _
            Screen.Height, GetDC(GetDesktopWindow()), _
            0&, 0&, SRCCOPY
End Sub

"

En fait, la fonction prend une capture d'écran,
et localise l'objet par rapport au bord.
On peut sûrement remplacer les lignes :
For numY = 0 To CInt(YD - YU)
        For numX = 0 To CInt(XD - XU)
            PicD.PSet (numX - 1, numY - 1), _
                    Pic.Point(numX + CInt(XU) - 1, numY + CInt(YU) - 1)
            DoEvents
        Next numX
    Next numY
par la méthode Render pour aller plus vite,
mais je ne sais pas bien m'en servir.


Pic et PicD servent juste pour la fonction.
par exemple :


Module.SaveMSChart Me.Picture1, Me.Picture2, _
            Me, Me.MSChart1, "C:\monfichier.bmp"

Voilà, j'espère que cela te conviendra.

- Keyboard not found, Press any key to continue -
0
debutant VB Messages postés 93 Date d'inscription lundi 3 juillet 2006 Statut Membre Dernière intervention 25 juin 2010
9 févr. 2008 à 14:08
J'y ai apporté quelques modifications :
http://www.vbfrance.com/codes/ENREGISTRER-IMAGE-CONTROLE_45682.aspx

- Keyboard not found, Press any key to continue -
0
Rejoignez-nous