ahmed khames ahmed khames - 25 days ago 9
HTML Question

How to fit image size on excel WebBrowser control

I have WebBrowser control named

WebBrowser1
.

I use it to display images by its URL using this code.

WebBrowser1.Navigate ("https://cdn2.iconfinder.com/data/icons/modern-latin-alphabet-lowercase-and-uppercase-lett/154/alphabet-uppercase-letter-z-512.png")


It works great but the image is bigger than WebBrowser control.

So I need to fit the image size to fit in the WebBrowser control size.

Answer

Try this code to fit image width:

Sub Test()

    UserForm1.Show
    UserForm1.WebBrowser1.Navigate "about:blank"
    UserForm1.WebBrowser1.Document.Write "<img style=""width:100%;"" src=""https://cdn2.iconfinder.com/data/icons/modern-latin-alphabet-lowercase-and-uppercase-lett/154/alphabet-uppercase-letter-z-512.png"">"

End Sub

You can even embed base64 encoded image into the code (use any of the online Base64 Image Encoders):

Sub TestBase64Image()

    Dim sBase64Image As String

    sBase64Image = "data:image/png;base64," & _
        "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAACXBIWXMAAA7DAAAOwwHHb6hkAAADoklE" & _
        "QVR4nO3dwW2DQBBAUYjSaWSKikytTgMcgmS8K/57BaA5fc2BgXV5PF8LkPQ1egBgHAGAMAGAMAGAMAGA" & _
        "MAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGA" & _
        "MAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAMAGAsO/RA/AZr9+f0SPcyrrto0d4CxsAhAkA" & _
        "hAkAhAkAhAkAhAkAhAkAhAkAhAkAhAkAhAkAhLkF4NDV77qfvU2YbZ67sAFAmABAmABAmABAmABAmABA" & _
        "mABAmABAmABAmABAmABAmABAmABAmABAmABAmABAmABAmABAmABAmABAmABAmABAmABAmP8CcGi27+TP" & _
        "Ns9d2AAgTAAgTAAgTAAgTAAgTAAgTAAgTAAgTAAgTAAgTAAgzC1AxLrto0f4KLcD/2MDgDABgDABgDAB" & _
        "gDABgDABgDABgDABgDABgDABgDABgLDb3AJ49/vearcMn2IDgDABgDABgDABgDABgDABgDABgDABgDAB" & _
        "gDABgDABgLDb3AKcdfW75WdvE8zDCDYACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMA" & _
        "CBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACMv+F2C279KbhxFsABAmABAmABAmABAmABAmABAm" & _
        "ABAmABAmABAmABAmABCWvQVYt/3S5599l36meWaahWvZACBMACBMACBMACBMACBMACBMACBMACBMACBM" & _
        "ACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACAs+1+A2b5NP9M8M83CtWwAECYA" & _
        "ECYAECYAECYAECYAECYAECYAECYAECYAECYAEJa9BZjNuu2XPt/7/RyxAUCYAECYAECYAECYAECYAECY" & _
        "AECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECYAECY" & _
        "AECYAECYAECYAECYAECYAECYAECYAECYAEDYujyer9FDAGPYACBMACBMACBMACBMACBMACBMACBMACBM" & _
        "ACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBMACBM" & _
        "ACBMACBMACBMACBMACBMACBMACBMACBMACDsD0NPOfMjSKzzAAAAAElFTkSuQmCC"

    UserForm1.WebBrowser1.Navigate "about:blank"
    UserForm1.WebBrowser1.Document.Write "<body style='background-color: buttonface; overflow: hidden;'><img style='width:100%;' src='" & sBase64Image & "'></body>"

End Sub