Excel: Afficher une shape dans un controle Image

Résolu
loskiller62 Messages postés 135 Date d'inscription jeudi 30 janvier 2003 Statut Membre Dernière intervention 12 juillet 2006 - 20 févr. 2006 à 14:52
loskiller62 Messages postés 135 Date d'inscription jeudi 30 janvier 2003 Statut Membre Dernière intervention 12 juillet 2006 - 12 juil. 2006 à 14:04
Bonjour. Voici la description du probleme:

* J'ai une image dans une feuille Excel.
* J'ai une interface (form) avec un controle Image
--> J'aimerais afficher l'image de la feuille Excel dans mon controle

Je peux recuperer l'image de la feuille Excel en tant qu'objet shape, mais je ne trouve pas comment l'envoyer dans mon controle Image. L'ideal serait de convertir l'objet shape en objet Picture. Cela peut se eventuellement se faire via la methode myShape.CopyPicture (qui copy l'objet shape dans le Clipboard en tant que Picture), mais c'est moche et j'ai pas encore ete jusqu'au bout de cette possibilite.

Dim sh As Excel.Shape
set sh = ThisWorkbook.Sheets(1).Shapes(1)
me.Image1 = sh 'Ne fonctionne pas bien sur, mais c'est l'idee

Une idee?

ps: desole pour l'accentuation je suis en angleterre = clavier qwerty

Vous pensiez que votre souris ne servait à rien? > Le Projet Marmotte! Comparez la distance que vous faites avec les autres internautes :)

3 réponses

loskiller62 Messages postés 135 Date d'inscription jeudi 30 janvier 2003 Statut Membre Dernière intervention 12 juillet 2006 1
20 févr. 2006 à 19:05
Merci nanougat je sais pas ce que j'aurais fais sans toi....
Non sérieusement, je n'ai peut être pas été clair mais je crois qu'en relisant tu devrais arriver à comprendre ce que je veux. De toute façon j'ai abandonné et je passe par le Clipboard (on trouve quelques exemples deci delà).

De tete:
ThisWorkbook.Sheets(1).Shapes(1).CopyPicture Format:=xlBitmap 'Met l'image dans le clipboard
me.Image1.Picture = GetClipboardPicture(Format:=xlBitmap) 'Fonction qui recupere l'image dans le clipboard

Si j'y pense demain je balancerais le code de la fonction ici nommée GetClipboardPicture

Vous pensiez que votre souris ne servait à rien? > Le Projet Marmotte! Comparez la distance que vous faites avec les autres internautes :)
1
nanougat Messages postés 40 Date d'inscription samedi 6 novembre 2004 Statut Membre Dernière intervention 26 octobre 2007
20 févr. 2006 à 16:30
Bonjour,



Tu inserts ton image, .jpg par exemple, dans la proprité "Picture" et
tu mets à "1-fmpicturesizemodestrech" la propriété "picturesizemode"
0
loskiller62 Messages postés 135 Date d'inscription jeudi 30 janvier 2003 Statut Membre Dernière intervention 12 juillet 2006 1
12 juil. 2006 à 14:04
Code:





'= = PASTE PICTURE =====================================================================================
    'Declare a UDT to store a GUID for the IPicture OLE Interface
    Private Type GUID
        Data1 As Long
        Data2 As Integer
        Data3 As Integer
        Data4(0 To 7) As Byte
    End Type
   
    'Declare a UDT to store the bitmap information
    Private Type uPicDesc
        Size As Long
        Type As Long
        hPic As Long
        hPal As Long
    End Type
   
    'Does the clipboard contain a bitmap/metafile?
    Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
    'Open the clipboard to read
    Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
    'Get a pointer to the bitmap/metafile
    Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
    'Close the clipboard
    Private Declare Function CloseClipboard Lib "user32" () As Long
    'Convert the handle into an OLE IPicture interface.
    Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    'Create our own copy of the metafile, so it doesn't get wiped out by subsequent clipboard updates.
    Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long
    'Create our own copy of the bitmap, so it doesn't get wiped out by subsequent clipboard updates.
    Declare Function CopyImage Lib "user32" (ByVal handle As Long, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
   
    'The API format types we're interested in
    Const CF_BITMAP = 2
    Const CF_PALETTE = 9
    Const CF_ENHMETAFILE = 14
    Const IMAGE_BITMAP = 0
    Const LR_COPYRETURNORG = &H4
   
    'OLE Picture types
    Const PICTYPE_BITMAP = 1
    Const PICTYPE_ENHMETAFILE = 4'Function PastePicture(Optional lXlPicType As Long xlPicture) As IPicture
    Dim h As Long, hPicAvail As Long, hPtr As Long, hPal As Long, lPicType As Long, hCopy As Long
   
    'Convert the type of picture requested from the xl constant to the API constant    lPicType IIf(lXlPicType xlBitmap, CF_BITMAP, CF_ENHMETAFILE)
    'Check if the clipboard contains the required format
    hPicAvail = IsClipboardFormatAvailable(lPicType)
   
    If hPicAvail <> 0 Then
        h = OpenClipboard(0&)   'Get access to the clipboard
        If h > 0 Then
            hPtr = GetClipboardData(lPicType) 'Get a handle to the image data
           
            'Create our own copy of the image on the clipboard, in the appropriate format.
            If lPicType = CF_BITMAP Then
                hCopy = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
            Else
                hCopy = CopyEnhMetaFile(hPtr, vbNullString)
            End If
           
            h = CloseClipboard  'Release the clipboard to other programs
           
            'If we got a handle to the image, convert it into a Picture object and return it
            If hPtr <> 0 Then Set PastePicture = CreatePicture(hCopy, 0, lPicType)
        End If
    End If
End Function

Private Function CreatePicture(ByVal hPic As Long, ByVal hPal As Long, ByVal lPicType) As IPicture
    Dim r As Long, uPicInfo As uPicDesc, IID_IDispatch As GUID, IPic As IPicture
   
    ' Create the Interface GUID (for the IPicture interface)
    With IID_IDispatch
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With
   
    ' Fill uPicInfo with necessary parts.
    With uPicInfo
        .Size = Len(uPicInfo) ' Length of structure.        .Type IIf(lPicType CF_BITMAP, PICTYPE_BITMAP, PICTYPE_ENHMETAFILE) ' Type of Picture
        .hPic = hPic ' Handle to image.        .hPal IIf(lPicType CF_BITMAP, hPal, 0) ' Handle to palette (if bitmap).
    End With
   
    ' Create the Picture object.
    r = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, True, IPic)
    ' If an error occured, show the description
    If r <> 0 Then Debug.Print "Create Picture Error" ' & fnOLEError(r) 'Requires a reference to the "OLE Automation" type library
   
    Set CreatePicture = IPic    ' Return the new Picture object.
End Function





Utilisation:



    Dim s As Shape
    Set s = ThisWorkbook.Sheets("Sheet1").Shapes.Item("Shape1")
    s.CopyPicture Format:=xlBitmap
    ImgMap.Picture = PastePicture(xlBitmap)
    Set s = Nothing




[size=1]Vous pensiez que votre souris ne servait à rien? > ["http://www.marmotproject.net" Le Projet Marmotte]! Comparez la distance que vous faites avec les autres internautes :)/size=1
0
Rejoignez-nous