+ Reply to Thread
Results 1 to 5 of 5

Loading an image into Userform.Image and loss of image quality

Hybrid View

  1. #1
    Registered User
    Join Date
    03-20-2020
    Location
    Poland
    MS-Off Ver
    2019
    Posts
    2

    Loading an image into Userform.Image and loss of image quality

    I have code that adds images from a selected folder to cells. Everything works, but not always - on two out of four computers it works, but on the other two it doesn't. What's going on with the non-working computer:

    When trying to rotate the image by 90?, it does not reload the rotated image into userform.image (it displays as if it were an empty shape) Additionally, is there a way to perform this operation without losing image quality, because later when I display the image in a Windows program by clicking on it in the cell, it is enlarged and the quality of the image is greatly reduced (hardly visible).

    There are two questions: How can I do this so that the image quality is better when displayed, and why doesn't this code work on all computers? sample file attached.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Loading an image into Userform.Image and loss of image quality

    Image quality will be relative to original. If you make it bigger it will deteriorate.
    You can maybe make your originals bigger with a free program called Irfanview and maintain good quality or just make sure you get high quality pictures to start with.

    Don't know the answer to the question why it does not work on some computers.
    The inherent weakness of the liberal society: a too rosy view of humanity.

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Loading an image into Userform.Image and loss of image quality


    Maybe 'cause IE was removed from the computers …

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,525

    Re: Loading an image into Userform.Image and loss of image quality

    dareksg, I've arranged for you to rotate the image on each computer (at least I hope so ) using a different method than copying the chart. But keep in mind that the rotation occurs only in the UserForm, into the sheet the image is inserted in its original form.

    Consider whether the clicked image needs to be opened in an external program. Since you're storing images in the workbook, maybe zooming the image can also happen in the workbook. When you open an image in an external program, especially small images can be distorted because that program can zoom in to fill the entire screen (or at least a large part of it). Then the original pixels are enlarged, which ends up blurring the image. And you are unlikely to be able to control the scale of this zooming.
    If you choose to enlarge the image in the sheet, then you can control the magnification size yourself. For example, you click on the image and it zooms in 2 or 3 times (depending on what magnification scale you choose). Another click on that image (enlarged) should return it to a small size.

    Attention. The code is adapted to the 32-bit version of MS Office. For the 64-bit version, the winAPI function declarations must be adjusted accordingly.

    Artik
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    03-20-2020
    Location
    Poland
    MS-Off Ver
    2019
    Posts
    2

    Re: Loading an image into Userform.Image and loss of image quality

    Thank you Artik, for the neat proposal with the clipboard. Although adapting it to 64-bit scared me, it seems like I managed to adjust the code to the 64-bit version, and it works. As for the pictures, they have to be displayed like that, but I won't reduce their size too much, so as not to lose quality.

    Below I am sending the code that works for me on 32 and 64.

    Dzięki

    Option Explicit
    
    #If VBA7 Then
        Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As LongPtr
        Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
        Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As LongPtr
        Private Declare PtrSafe Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As LongPtr, ByVal lpszFile As String) As LongPtr
        Private Declare PtrSafe Function CloseClipboard Lib "user32" () As LongPtr
        Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32.dll" (ByRef PicDesc As uPicDesc, ByRef RefIID As Any, ByVal fOwn As LongPtr, ByRef IPic As IPicture) As LongPtr
    #Else
        Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
        Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
        Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
        Private Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long
        Private Declare Function CloseClipboard Lib "user32" () As Long
        Private Declare Function OleCreatePictureIndirect Lib "oleaut32.dll" (ByRef PicDesc As uPicDesc, ByRef RefIID As Any, ByVal fOwn As Long, ByRef IPic As IPicture) As Long
    #End If
    
    'Requires a reference to the "OLE Automation" type library
    
    '----------------------------------------------------------------------------
    ' User-Defined Type for API Calls
    '----------------------------------------------------------------------------
    
    'Declare a Type 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 Type to store the bitmap information
    Private Type uPicDesc
        Size            As Long
        Type            As Long
        hPic            As LongPtr
        hPal            As LongPtr
    End Type
    
    Function PastePicture() As IPicture
    
        Const lMETAFILE As Long = 14
    
        Dim lPictureAvailable As LongPtr
        Dim lClipHandle As LongPtr
        Dim lPicHandle  As LongPtr
        Dim lCopyHandle As LongPtr
        Dim uInterGUID  As GUID
        Dim uPictureInfo As uPicDesc
        Dim lOLEHandle  As LongPtr
        Dim iTempPicture As IPicture
    
        'Check if the clipboard contains a picture file
        lPictureAvailable = IsClipboardFormatAvailable(lMETAFILE)
    
        If lPictureAvailable <> 0 Then
    
            'Get a Handle on the Clipboard
            lClipHandle = OpenClipboard(0&)
    
            If lClipHandle > 0 Then
    
                'Get a Handle on the Picture
                lPicHandle = GetClipboardData(lMETAFILE)
                'Make a local copy, in case the clipboard is changed
                lCopyHandle = CopyEnhMetaFile(lPicHandle, vbNullString)
    
                'Release Handle from Clipboard
                lClipHandle = CloseClipboard
    
                'Only Continue if we have a handle on the Picture
                If lPicHandle <> 0 Then
    
                    ' Create the Interface GUID (for the IPicture interface)
                    With uInterGUID
                        .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 UPictureInfo with necessary parts.
                    With uPictureInfo
                        .Size = Len(uPictureInfo)   ' Length of structure.
                        .Type = 4                   ' Type of Picture = Metafile
                        .hPic = lCopyHandle         ' Handle to image.
                        .hPal = 0                   ' Handle to palette.
                    End With
    
                    'Create the IPicture Object
                    lOLEHandle = OleCreatePictureIndirect(uPictureInfo, uInterGUID, True, iTempPicture)
    
                    If lOLEHandle = 0 Then
                        Set PastePicture = iTempPicture
                    End If
                End If
            End If
        End If
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Userform Image Control - Show specific region of Image
    By PrizeGotti in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-09-2021, 12:04 PM
  2. Image Pop Up on Mouse Hover over image on userform
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-31-2019, 04:43 PM
  3. Inserting image into userform label, text behind image?!?!
    By spyrule in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 0
    Last Post: 08-31-2016, 03:23 PM
  4. Replies: 1
    Last Post: 07-14-2016, 04:59 AM
  5. [SOLVED] Using a Userform to Browse image file then paste selected image to sheet?
    By matrixpom in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-05-2015, 05:42 PM
  6. [SOLVED] How to apply Image borders to an image that my excel vba userform pastes in a word doc?
    By CaptainCool in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-11-2014, 05:40 PM
  7. [SOLVED] VBA to transfer image FROM userform image control TO a worksheet cell
    By Zoediak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2014, 02:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1