Excel Vba。将调整大小的图像保存到文件

时间:2016-07-13 12:32:33

标签: excel vba

我正在尝试保存以存档已调整大小的图片,其中包含LoadPicture。使用以下代码,我加载图像并调整其大小,但我现在明白Me.Image1.Width将图像调整为image box控件的大小仅用于显示目的。

如果我使用savepicture()保存图像,则保存的图像与加载的图像相同。

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

1 个答案:

答案 0 :(得分:2)

<强>解决。

根据建议,我使用了ImageMagick(以下内容适用于v7.0.2-4)。

  1. Download 动态版本('每个像素16位组件的Win32动态'或Win64)
  2. 安装时选择:
    • 将应用程序目录添加到系统路径
    • 为VBScript,VisualBasic和WSH安装ImageMagickObject OLE控件
  3. enter image description here

    以下代码打开一个对话框窗口以选择图像,调用ImageMagickObject OLE,调整图像大小并将其保存到新文件中:

    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
    

    其他ImageMagick resize options