Convert Image from VBA Using ImageMagick

ImageMagickObject is a Windows COM+ interface to ImageMagick. COM+, Visual Basic, and Delphi users should post to this discussion group.
Post Reply
tlewis3348
Posts: 3
Joined: 2015-09-01T05:40:44-07:00
Authentication code: 1151

Convert Image from VBA Using ImageMagick

Post by tlewis3348 »

I would like to convert images downloaded from the internet[1] to JPGs with ImageMagick in VBA. So far, I've attempted two methods that have both failed.

First, I tried using the ImageMagickObject 1.0 Type Library:

Code: Select all

Private Sub CommandButtonOkay_Click()
    Dim sURL As String, sNetFile As String, sLocalFile As String, _
        cmd As String, RetVal As Integer, img As Object
    Set img = New ImageMagickObject.MagickImage
    sURL = UserForm1.TextBoxImgURL
    sLocalFile = "C:\temp\" & UserForm1.TextBoxName
    DownloadFile sURL, sLocalFile ' Function to download image from a URL and save it to a local directory
    RetVal = img.Convert(sLocalFile, sLocalFile & ".jpg") '<-- This line produces the error
    UserForm1.Hide
End Sub
This ends up giving me the following error:

Image

The source file ("C:\temp\image") exists, but the file that was to be created ("C\temp\image.jpg") does not. This is very similar to the question posted here, but I have not been able to find a solution to that so far.

Second, I tried just calling ImageMagick using the Shell command:

Code: Select all

Private Sub CommandButtonOkay_Click()
    Dim sURL As String, sNetFile As String, sLocalFile As String, _
        cmd As String, RetVal As Integer
    sURL = UserForm1.TextBoxImgURL
    sLocalFile = "C:\temp\" & UserForm1.TextBoxName
    DownloadFile sURL, sLocalFile ' Function to download image from a URL and save it to a local directory
    RetVal = Shell("convert.exe """ & sLocalFile & """ """ & sLocalFile & ".jpg""")
    UserForm1.Hide
End Sub
When I run this, the image gets downloaded just fine, but the image isn't converted and no error is thrown. Furthermore, when I execute the command that the Shell command executes in a separate command window, the conversion happens exactly as I would expect.

So the question then seems to be why is the ImageMagick command working beautifully when it is operating in its own command prompt, but not working at all when operating from within VBA?

[1] I don't know if this is useful information or not, but I'm downloading the images from the internet programmatically, so I have no means of knowing what format I'm getting; however, the image I've been using to test this with is a PNG.
tlewis3348
Posts: 3
Joined: 2015-09-01T05:40:44-07:00
Authentication code: 1151

Re: Convert Image from VBA Using ImageMagick

Post by tlewis3348 »

Just realized that it looks like I was using the Shell command incorrectly. Using the following fixes that problem:

Code: Select all

RetVal = Shell("cmd.exe /c convert """ & sLocalFile & """ """ & sLocalFile & ".jpg""", vbMaximizedFocus)
If someone would like to explain how to resolve the other problem though, that would still be beneficial.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Convert Image from VBA Using ImageMagick

Post by dlemstra »

You are getting the message 'The specfied module could not be found' because you did not install the C++ redistributable for VS2013. You can find that here: https://www.microsoft.com/en-us/downloa ... x?id=40784
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
tlewis3348
Posts: 3
Joined: 2015-09-01T05:40:44-07:00
Authentication code: 1151

Re: Convert Image from VBA Using ImageMagick

Post by tlewis3348 »

Ah! That would make sense. Thanks!
Post Reply