Excel Vba。 将resize的图像保存到文件
我试图保存到一个resize的图像加载LoadPicture
。 使用下面的代码我加载图像,并调整它的大小,但我现在明白, Me.Image1.Width
resize图像到image box
控件仅用于显示目的。
如果我用savepicture()
保存图像, 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
解决了。
如所暗示的,我使用了ImageMagick (以下适用于v7.0.2-4)。
- 下载 dynamic版本(“Win32dynamic,每像素16位”或Win64)
- 安装时select:
- 将应用程序目录添加到系统path
- 为VBScript,VisualBasic和WSH安装ImageMagickObject OLE控件
下面的代码打开一个对话窗口来select图像,调用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 调整选项 。