Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

inserer une image PNG dans une picturebox

3 réponses
Avatar
Superman
Bonjour,

J'aimerais savoir comment implant=E9 une image PNG dans un picturebox.
J'ai vu certaine chose avec GDI + mais je comprend pas trop...

Pourriez vous m'aidez ?

Merci !
Maxime

3 réponses

Avatar
Jacques93
Bonjour Superman,
Superman a écrit :
Bonjour,

J'aimerais savoir comment implanté une image PNG dans un picturebox.
J'ai vu certaine chose avec GDI + mais je comprend pas trop...

Pourriez vous m'aidez ?

Merci !
Maxime




Une solution possible : FreeImage, téléchargeable ici :

<http://sourceforge.net/project/showfiles.php?group_id504>

=> prendre FreeImage393Win32.zip

Note : il y a tous les sources, mais tu n'as besoin que de
FreeImage.dll, à placer dans le répertoire de l'application, ou
%windir%system32.

Il faut ensuite un wrapper déclarant les constantes et les Api's de la
dll. Il semble qu'il soit devenu payant, mais on peut encore le trouver
ici :

<http://www.koders.com/vb/fid9A78B689199142D7978E8319B397573C28D814D7.aspx>

tu en fais un copier / coller dans un module nommé 'modFreeImage.bas'

Note : Il y a quelques erreurs de transcription (* qui trainent (2),
mot-clé UNKNOWN devant des paramètres (2)), mais rien de bien difficile
à rectifier.


Ensuite le principe est simple : tu récupère un 'Handle' (la variable
dib) du ficher .png, tu l'enregistres au format .bmp (apparement la
conversion au format jpeg pose problème). puis tu charge ton image dans
le PictureBox avec LoadPicture :


Private Sub btnTest_Click()
Dim dib As Long
Dim bOK As Long

' Load a png image
dib = FreeImage_Load(FIF_PNG, "Image.png", 0)

' Save this image as BMP
bOK = FreeImage_Save(FIF_BMP, dib, "Image.bmp", 0)

' Unload the dib
FreeImage_Unload (dib)

' Charge le bmp dans le PictureBox
Set Me.Picture1.Picture = LoadPicture("Image.bmp")
End Sub

--
Cordialement,

Jacques.
Avatar
Loïc Carrère
Salut SuperMan.

Voici un bout de code qui fait ce que tu demandes.

Tu peux en entrée utiliser le format png, bmp, tif, gif wmf emf ico.

1) Démarre un nouveau projet vb.

2) Dessine sur ton formulaire un objet picturebox.

3) Copie/Colle le code ci-dessous.

4) rennome dans le code "c:test.jpg" par le chemin de ton image...

5) C'est tout.

Si tu as des question n'hésites pas.

Cordialement,

Loïc











Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal
Filename As String, image As Long) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal image As
Long) As Long
Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long,
inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0)
As Long
Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As
Long) As Long
Private Declare Function GdipCreateHBITMAPFromBitmap Lib "gdiplus"
(ByVal bitmap As Long, hbmReturn As Long, ByVal background As Long) As
Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll"
(PicDesc As PicBmp, RefIID As CLSID, ByVal fPictureOwnsHandle As Long,
IPic As Picture) As Long

Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type

Private Type PicBmp
Size As Long
type As Long
hBMP As Long
hPal As Long
Reserved As Long
End Type


Private Type CLSID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type


Private nGdiplusToken As Long



Private Sub Form_Load()
Dim GdiPlusSI As GdiplusStartupInput

GdiPlusSI.GdiplusVersion = 1
Call GdiplusStartup(nGdiplusToken, GdiPlusSI, ByVal 0&)

Call PngFileToPictureBox("c:test.jpg")
End Sub


Private Sub Form_Terminate()
Call GdiplusShutdown(nGdiplusToken)
End Sub




Private Sub PngFileToPictureBox(ByVal sFilePath As String)
Dim nImageID As Long

If GdipLoadImageFromFile(StrConv(sFilePath, vbUnicode), nImageID) = 0
Then
Set Picture1.Picture = ImageToPicture(nImageID)
End If
End Sub



Public Function ImageToPicture(ByVal ptImage As Long) As Picture
Dim oPicBmp As PicBmp

Dim IID_IDispatch As CLSID
Dim hPictureObject As Long


MsgBox GdipCreateHBITMAPFromBitmap(ptImage, hPictureObject, ByVal
&HFFFFFFFF)
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With

With oPicBmp
.Size = Len(oPicBmp)
.type = vbPicTypeBitmap
.hBMP = hPictureObject
.hPal = 0
End With


Call OleCreatePictureIndirect(oPicBmp, IID_IDispatch, 1,
ImageToPicture)
End Function















Le 21/06/2007, Superman a supposé :
Bonjour,

J'aimerais savoir comment implanté une image PNG dans un picturebox.
J'ai vu certaine chose avec GDI + mais je comprend pas trop...

Pourriez vous m'aidez ?

Merci !
Maxime



--
Loïc Carrère.

http://www.gdpicture.com
Avatar
Superman
Parfait je l'ai integré sans probleme !!!

Merci encore !