VBAでJPEGファイルのサイズ変換

Option Explicit
Private Enum GDIPlusStatusConstants
    Ok = 0
    GenericError = 1
    InvalidParameter = 2
    OutOfMemory = 3
    ObjectBusy = 4
    InsufficientBuffer = 5
    NotImplemented = 6
    Win32Error = 7
    WrongState = 8
    Aborted = 9
    FileNotFound = 10
    ValueOverflow = 11
    AccessDenied = 12
    UnknownImageFormat = 13
    FontFamilyNotFound = 14
    FontStyleNotFound = 15
    NotTrueTypeFont = 16
    UnsupportedGdiplusVersion = 17
    GdiplusNotInitialized = 18
    PropertyNotFound = 19
    PropertyNotSupported = 20
    ProfileNotFound = 21
End Enum
Private Type UUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type
Private Type GdiplusStartupInput
    GdiplusVersion As Long
    DebugEventCallback As Long
    SuppressBackgroundThread As Long
    SuppressExternalCodecs As Long
End Type
Private Type EncoderParameter
    Guid As UUID
    NumberOfValues As Long
    Type As Long
    Value As Long
End Type
Private Type EncoderParameters
    Count As Long
    Parameter(15) As EncoderParameter
End Type
Private Declare Function GdiplusStartup Lib "GDIPLUS.dll" _
   (ByRef token As Long, _
    ByRef inputBuf As GdiplusStartupInput, _
    ByVal outputBuf As Long) As Long
Private Declare Sub GdiplusShutdown Lib "GDIPLUS.dll" _
    (ByVal token As Long)
Private Declare Function GdipDisposeImage Lib "GDIPLUS.dll" _
    (ByVal image As Long) As Long
Private Declare Function GdipSaveImageToFile Lib "GDIPLUS.dll" _
    (ByVal image As Long, _
    ByVal fileName As Long, _
    ByRef clsidEncoder As UUID, _
    ByVal encoderParams As Long) As Long
Private Declare Function CLSIDFromString Lib "ole32" _
    (ByVal lpszCLSID As Long, _
     ByRef pclsid As UUID) As Long
Private Declare Function GdipLoadImageFromFile Lib "GDIPLUS.dll" _
    (ByVal fileName As Long, _
    image As Long) As Long
Private Declare Function GdipGetImageThumbnail Lib "GDIPLUS.dll" _
    (ByVal baseImage As Long, _
    ByVal thumbWidth As Long, _
    ByVal thumbHeight As Long, _
    ByRef thumbImage As Long, _
    ByVal callback As Long, _
    ByVal callbackData As Long) As Long
'┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
'┃ 名称 :ResizeImage
'┃ 説明 :指定されたピクチャーを指定したサイズで保存する
'┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Private Function ResizeImage( _
    ByVal strInName As String, _
    ByVal strOutName As String, _
    lngWidth As Long, lngHeight As Long, _
    Optional ByVal Quality As Long = 100) As Boolean
    Dim lngResult As Long
    Dim lngGDIPToken As Long
    Dim lngBaseImage As Long
    Dim lngThumbImage As Long
    Dim udtEncParam As EncoderParameters
    Dim udtGdiPlus As GdiplusStartupInput

    'Const CLSID_PNG = "{557CF406-1A04-11D3-9A73-0000F81EF32E}"
    Const CLSID_JPEG = "{557CF401-1A04-11D3-9A73-0000F81EF32E}"
    Const CLSID_QUALITY = "{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"

    On Error GoTo ResizeImage_Err:

    If Quality > 100 Then Quality = 100
    If Quality < 25 Then Quality = 25
    If Dir(strInName) = "" Then
        Exit Function
    End If
    udtGdiPlus.GdiplusVersion = 1
    If GdiplusStartup(lngGDIPToken, udtGdiPlus, 0&) <> 0 Then
        Exit Function
    End If
    If GdipLoadImageFromFile(StrPtr(strInName), lngBaseImage) <> 0 Then
        Exit Function
    End If

    lngResult = GdipGetImageThumbnail(lngBaseImage, _
                                      lngWidth, _
                                      lngHeight, _
                                      lngThumbImage, _
                                      0&, 0&)
    If lngResult = GDIPlusStatusConstants.Ok Then
        udtEncParam.Count = 1
        With udtEncParam.Parameter(0)
            .Guid = GetCLSID(CLSID_QUALITY)
            .NumberOfValues = 1
            .Type = 4
            .Value = VarPtr(Quality)
        End With
        ' JPG変換で保存
        ResizeImage = GdipSaveImageToFile(lngThumbImage, _
                                          StrPtr(strOutName), _
                                          GetCLSID(CLSID_JPEG), _
                                          VarPtr(udtEncParam))
        lngResult = GdipDisposeImage(lngThumbImage)
    End If
    lngResult = GdipDisposeImage(lngBaseImage)
    Call GdiplusShutdown(lngGDIPToken)
    ResizeImage = True
    Exit Function

ResizeImage_Err:
    MsgBox Err.Description
End Function
'┌───────────────────────────────────────
'│ 名称 :GetCLSID
'│ 説明 :クラスID文字列から128BitクラスID値に変換する
'└───────────────────────────────────────
Private Function GetCLSID(ByVal strGuid As String) As UUID
Dim lngResult As Long
    lngResult = CLSIDFromString(StrPtr(strGuid), GetCLSID)
End Function
使い方は
Public Sub CreateImg2VGA(ByVal strInName As String, ByVal strOutName As String)
    If ResizeImage(strInName, strOutName, 640&, 480&) = False Then
        MsgBox "失敗しました。"
    End If
End Sub

なんでもVBAに頼るのはそろそろやめよう。

------------------------------------

お恥ずかしい

%CD%でした。(2008.12.19追記)

------------------------------------

.BATファイルでカレントディレクトリを指定したい場合


%0が自ファイル名(フルパス)なので

%~dp0

と指定する(右端には\付き)


CURDIR.BAT

------------------

echo %~dp0

pause

------------------


カレントディレクトリが表示される。


参考『コマンドプロンプトを使ってみよう:バッチパラメータ・修飾子』
http://ykr414.com/dos/bat.html#05