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

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

本文介绍了Excel Vba将调整大小的图像保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  Private Sub CommandButtonImage_Click()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.ButtonName =提交
.Title =选择图像
.Filters.AddImage,* .gif; * .jpg; *。 jpeg; * .png,1
如果.Show = -1然后
'文件已被选择

'适合图像到图像框
Me.Image1。 PictureSizeMode = fmPictureSizeModeZoom

'在图像控件中显示预览图像
Me.Image1.Picture = LoadPicture(.SelectedItems(1))

'调整大小图像
Me.Image1.Width = 50

Else
'something
End If
End With
End Sub


解决方案

SOLVED。



如所建议的,我使用






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

  Private Sub CommandButtonImage_Click()

Dim img
设置img = CreateObject(ImageMagickObject.MagickImage)

与Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.ButtonName =提交
.Title =Selezionare un'immagine
.Filters.AddImage,* .gif; * .JPG; * .JPEG; * .png,1
如果.Show = -1然后
'文件已被选择

'适合图像到图像框
Me.Image1.PictureSizeMode = fmPictureSizeModeZoom

'在图像控件中显示预览图像
Me.Image1.Picture = LoadPicture(.SelectedItems(1))

'这将调整所选图像的大小保持宽高比
',但调整大小将仅适用于给定
'('>'符号)的大小,并将图像名称设置为'resized.jpg'
img.Convert .SelectedItems(1),-resize,300x300>,c:\resized.jpg

Else
'something
End If
结束
End Sub

其他ImageMagick 。


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
解决方案

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

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.

这篇关于Excel Vba将调整大小的图像保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:16