1

I'm trying to save to file a resized image loaded with LoadPicture. With the following code I load the image and resize it but I now understood that Me.Image1.Width resizes the image into the image box control for displaying purposes only.

If I save the image with savepicture() the image saved is the same as loaded image.

Private Sub CommandButtonImage_Click()
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Submit"
        .Title = "Select a image"
        .Filters.Add "Image", "*.gif; *.jpg; *.jpeg; *.png", 1
        If .Show = -1 Then
            ' file has been selected

            ' fit image into image box
            Me.Image1.PictureSizeMode = fmPictureSizeModeZoom

            ' display preview image in an image control
            Me.Image1.Picture = LoadPicture(.SelectedItems(1))

            ' resize image
            Me.Image1.Width = 50

        Else
            ' something    
        End If
    End With
End Sub
Nicero
  • 4,181
  • 6
  • 30
  • 52
  • Are you trying to resize an image by loading it into a command button?? Why? – Cody G Jul 13 '16 at 13:58
  • No no, the command button opens a dialog window (browse file) where the user selects an image. The image is then displayed into the `Me.Image1.Picture` image box. – Nicero Jul 13 '16 at 14:09
  • Well, LoadPicture has optional parameters x,y for requested height/width. What does that do for you? – Cody G Jul 13 '16 at 14:11
  • The `x y` parameters of `LoadPicture` are note related to setting the width or height of the image. I'm looking for a way to resize an image and then save it (resized) to a file. – Nicero Jul 13 '16 at 14:15
  • I can only think of importing the picture into a shape, resizing the shape, converting the shape to either clipboard or OLEObject data and then saving it. Seems overly complicated --- you could use a console script to process the image using something like imagemagick instead. Maybe see https://stackoverflow.com/questions/18232987/export-pictures-from-excel-file-into-jpg-using-vba – Cody G Jul 13 '16 at 14:44
  • Thank you for your time. Yes, it sounds really complicated. :-(( – Nicero Jul 13 '16 at 15:12
  • Add imagemagick as a COM+ object https://www.imagemagick.org/script/ImageMagickObject.php – Cody G Jul 13 '16 at 17:46

1 Answers1

3

SOLVED.

As suggested, I used ImageMagick (the following applies to v7.0.2-4).

  1. Download the dynamic version ('Win32 dynamic at 16 bits-per-pixel component' or Win64)
  2. When installing select:
    • Add application directory to your system path
    • Install ImageMagickObject OLE control for VBScript, VisualBasic and WSH

enter image description here


The following code opens a dialog window to select the image, calls ImageMagickObject OLE, resizes the image and saves it to a new file:

Private Sub CommandButtonImage_Click()
    
    Dim img
    Set img = CreateObject("ImageMagickObject.MagickImage")
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Submit"
        .Title = "Selezionare un'immagine"
        .Filters.Add "Image", "*.gif; *.jpg; *.jpeg; *.png", 1
        If .Show = -1 Then
            ' file has been selected
         
            ' fit image into image box
            Me.Image1.PictureSizeMode = fmPictureSizeModeZoom
            
            ' display preview image in an image control
            Me.Image1.Picture = LoadPicture(.SelectedItems(1))
            
            ' this will resize the selected image keeping the aspect ratio 
            ' but resizing will be done only to fit into the size given 
            ' ('>' sign) and it will set the image name to 'resized.jpg'   
            img.Convert .SelectedItems(1), "-resize", "300x300>", "c:\resized.jpg"
            
        Else
            ' something
        End If
    End With
End Sub

Other ImageMagick resize options.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Nicero
  • 4,181
  • 6
  • 30
  • 52
  • Cool! Glad to see you got it done. Your answer might help me too in the future when I refuse to do my general cmd/console output hackery. – Cody G Jul 21 '16 at 13:31