XL 2016 Userform - Comment réduite image en fonction du WebBrowser

webbacor

XLDnaute Nouveau
Bonjour,

je voudrai pouvoir réduire l'image insérer en fonction de la taille du webbrowse.

Private Sub lstImages_Click() WebBrowser1.Navigate (txtChemin & "" & lstImages.List(lstImages.ListIndex)) lblNomActuel = txtChemin & "\" & lstImages.List(lstImages.ListIndex) End Sub

Merci d'avance
 

fanch55

XLDnaute Barbatruc
Bonjour,
A tester:
VB:
Private Sub lstImages_Click()
Dim Img
    WebBrowser1.Navigate "about:blank"
    lblNomActuel = txtChemin & "" & lstImages.List(lstImages.ListIndex)
    Style = "display:block;margin-left:auto;margin-right:auto"
    WebBrowser1.Document.Write "<img id='MyImg' style=""" & Style & """ src='" & lblNomActuel & "'>"
    Set Img = WebBrowser1.Document.GetElementById("MyImg")
        If Img.Width > WebBrowser1.Width Then Img.Style.Width = "100%"
        If Img.Height > WebBrowser1.Height Then Img.Style.Height = "100%"
    Set Img = Nothing
End Sub
 

fanch55

XLDnaute Barbatruc
Bonsoir
a l'attention de @fanch55 voir les deux userforms
changer le chemin de l'image
Salut Patrick,
J'ai cherché à conserver les proportions :
1632850607724.png
 

patricktoulon

XLDnaute Barbatruc
ok bien reçu
faudrait essayer avec diverses images de formes différentes
comme le principe du ratio range/imagede ma fonction perso ca s'applique ici aussi

et puis il ne faut pas comparer le width du browser mais celui du body enfant de document

ps :je sais pas si tu a vu mais je l'ai monté en DOM et non codé en dur avec un write ;)
en dom pratiquement tout les membre de style sont applicables on supprime le tiret du code style en dur pour pointer le membre
 

fanch55

XLDnaute Barbatruc
ok bien reçu
faudrait essayer avec diverses images de formes différentes
comme le principe du ratio range/imagede ma fonction perso ca s'applique ici aussi

et puis il ne faut pas comparer le width du browser mais celui du body enfant de document

ps :je sais pas si tu a vu mais je l'ai monté en DOM et non codé en dur avec un write ;)
en dom pratiquement tout les membre de style sont applicables on supprime le tiret du code style en dur pour pointer le membre
En Dom, c'est bien plus propre effectivement .
Pour le width je suis fondamentalement d'accord, il vaut mieux utiliser
If Img.Width > WebBrowser1.Document.body.ClientWidth Then Img.Width = "100%"
If Img.Height > WebBrowser1.Document.body.ClientHeight Then Img.Height = "100%"
Le write m'évitait de faire un appendchild ...
 

webbacor

XLDnaute Nouveau
Bonjour et merci encore.
novice je n'arrive pas vraiment à suivre vos commentaires. Desolé
l'image est bien redimensionnée mais
Private Sub lstFichiers_Click() Dim Img Dim style WebBrowser1.Navigate "about:blank" lblNomActuel = txtChemin & "" & lstFichiers.List(lstFichiers.ListIndex) style = "display:block;margin-left:auto;margin-right:auto" WebBrowser1.Document.Write "<img id='MyImg' style=""" & style & """ src='" & lblNomActuel & "'>" Set Img = WebBrowser1.Document.GetElementById("MyImg") If Img.Width > WebBrowser1.Width Then Img.style.Width = "100%" If Img.Height > WebBrowser1.Height Then Img.style.Height = "100%" Set Img = Nothing End Sub
je n'arrive qu'a afficher qu'une seule photo de ma lstFichiers
 

patricktoulon

XLDnaute Barbatruc
ca c'est un autre probleme alors

@fanch55
reprends mon userform et met lui ce code





VB:
Private Sub lstImages_Click()
    Dim Img, WX#, HX#, Ratio#
    'lblNomActuel = txtChemin & "" & lstImages.List(lstImages.ListIndex)
    lblNomActuel = "C:\Users\Public\Pictures\Sample Pictures\aaaa.jpg"

    With WebBrowser1
        .Navigate "about:blank"
        Do While .ReadyState < 4: DoEvents: Loop
        Set body = .Document.getelementsbytagname("body")(0)
        With body.Style
            .margin = "0"
            .Width = "100%"
            .Height = "100%"
        End With
        Set Img = .Document.createelement("img")
        Img.src = lblNomActuel
        Img.ID = "MyImg"
        With Img.Style
            .display = "block"
            .MarginLeft = "auto"
            .MarginRight = "auto"
            .Position = "absolute"    ' sans ca on ne peut mettre une image ailleur qu'a gauche
            WebBrowser1.Document.body.appendchild (Img)

            ' c'est maintenant que l'on s'amuse
            ' on calcule le ratio width et height du  body/image
            WX = body.ClientWidth / Img.offsetwidth
            HX = body.ClientHeight / Img.offsetheight

            Ratio = Application.Min(WX, HX)    ' on prend le plus petit

            Img.Width = (Img.offsetwidth * Ratio) - 6    'on applique a l'image - le 6 car j'ai enlever la marge du body(plein webbrowser)
            Img.Height = (Img.offsetheight * Ratio) - 6    'pareil
            .Left = (body.ClientWidth - Img.Width - 6) / 2  'on la centre
        End With

        ' voir le code en bon et due forme d'une balise Img html
        TextBox1 = .Document.getelementsbytagname("body")(0).outerhtml
    End With
    Set Img = Nothing
End Sub

Private Sub UserForm_Click()

End Sub
1632854540241.png
 

patricktoulon

XLDnaute Barbatruc
re
et si au lieu d'utiliser les atribut width et height mais on resize le width et height dans le style on a plus besoins de la marge-6 c'est plus precis
VB:
Private Sub lstImages_Click()
    Dim Img, wx#, hx#, Ratio#
    'lblNomActuel = txtChemin & "" & lstImages.List(lstImages.ListIndex)
    lblNomActuel = "C:\Users\Public\Pictures\Sample Pictures\aaaa.jpg"

    With WebBrowser1
        .Navigate "about:blank"
        Do While .ReadyState < 4: DoEvents: Loop
        Set body = .Document.getelementsbytagname("body")(0)
        With body.Style
            .margin = "0"
            .Width = "100%"
            .Height = "100%"
        End With
        Set Img = .Document.createelement("img")
        Img.src = lblNomActuel
        Img.ID = "MyImg"
        With Img.Style
            .display = "block"
            .MarginLeft = "auto"
            .MarginRight = "auto"
            .Position = "absolute"    ' sans ca on ne peut mettre une image ailleur qu'a gauche
            WebBrowser1.Document.body.appendchild (Img)

            ' c'est maintenant que l'on s'amuse
            ' on calcule le ratio width et height du  body/image
            wx = (body.ClientWidth) / Img.offsetwidth
            hx = (body.ClientHeight) / Img.offsetheight
            TBwx = wx: TBhx = hx
            Ratio = Application.Min(wx, hx)    ' on prend le plus petit

            .Width = Round((Img.offsetwidth * Ratio)) & "px"   'on applique a l'image - le 6 car j'ai enlever la marge du body(plein webbrowser)
            .Height = "auto"    'Round((Img.offsetheight * Ratio)) & "px"     ' la même chose que lockaspectratio
            .Left = ((body.ClientWidth) - Img.Width) / 2     'on la centre en CSS
            .Top = ((body.ClientHeight) - Img.Height) / 2     'on la centre en CSS
        End With

        ' voir le code en bon et due forme d'une balise Img html
        TextBox1 = .Document.getelementsbytagname("body")(0).outerhtml
    End With
    Set Img = Nothing
End Sub
 

fanch55

XLDnaute Barbatruc
Bonjour et merci encore.
novice je n'arrive pas vraiment à suivre vos commentaires. Desolé
l'image est bien redimensionnée mais
je n'arrive qu'a afficher qu'une seule photo de ma lstFichiers
A tester ( c'est bien une image à la fois ? ) :
VB:
Private Sub lstImages_Click()
Dim Img
    With WebBrowser1
        .Navigate "about:blank"
        Do While .ReadyState < 4: DoEvents: Loop
        lblNomActuel = txtChemin & "" & lstFichiers.List(lstFichiers.ListIndex)
        Style = "display:block;margin-left:auto;margin-right:auto"
        .Document.Write "<img id='MyImg' style=""" & Style & """ src='" & lblNomActuel & "'>"
        Set Img = .Document.GetElementById("MyImg")
            If Img.Width > .Document.body.ClientWidth Then Img.Style.Width = "100%"
            If Img.Height > .Document.body.ClientHeight Then Img.Style.Height = "100%"
        Set Img = Nothing
    End With
End Sub
 

patricktoulon

XLDnaute Barbatruc
regarde @fanch55
ton dernier si on enlève la marge au document on ne peux le dimensionner a 100%
VB:
Private Sub lstImages_Click()
    Dim Img
    With WebBrowser1
        .Navigate "about:blank"
        Do While .ReadyState < 4: DoEvents: Loop
        'lblNomActuel = txtChemin & "" & lstFichiers.List(lstFichiers.ListIndex)
        lblNomActuel = "C:\Users\Public\Pictures\Sample Pictures\aaaa.jpg"
        style0 = "margin:0;width:100%;height:100%;"""
        style1 = "display:block;margin:0"
        .Document.Write "<body style=" & style0 & "><img id='MyImg' style=""" & style1 & """ src='" & lblNomActuel & "'></body>"
        Set Img = .Document.GetElementById("MyImg")
        If Img.Width > .Document.body.ClientWidth Then Img.Style.Width = "100%"
        If Img.Height > .Document.body.ClientHeight Then Img.Style.Height = "100%"
        Set Img = Nothing
        TextBox1 = .Document.getelementsbytagname("body")(0).outerhtml
    End With
End Sub
 

patricktoulon

XLDnaute Barbatruc
regarde en fait ce qui se passe avec tes deux tests
regarde bien le code obtenu dans le textbox
en aucun cas l'image est redimensionnée
1632858098405.png


autrement dis si l'image est plus petite elle reste comme tel
c'est pour ca que je disais tout a l'heure il faut faire comme ma fonction ratio range/image
car avec ça peut être supérieur ou inférieur
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 300
Messages
2 087 015
Membres
103 433
dernier inscrit
nicolaseuropa