How to convert TIF to JPG using VBA ExportRasterFile?

I wrote a VBA code to convert TIF to JPG, but it does not work. I get run time error 5 when I run my code. Can someone help me? I'm using Microstation v8i. My code is below one.

RasterManager.ExportRasterFile sourcefilename:=fpath & "\" & fname, destinationfilename:=fpath & "\" & fso.GetBaseName(fname) & ".jpg", NewFormatSpecifier:=RasterManager.RasterFormats.Find("JPEG(J)")

  • I read MicrostationVBA.chm and tried some ways, then I could save JPEG from TIFF using ExportRasterfile. The string 'JPEG(J)' is not wrong but I have to add some arguments to my code.

    RasterManager.ExportRasterFile sourcefilename:=fname, _
    destinationfilename:=outname, _
    NewFormatSpecifier:=oRF, _
    NewColorModeSpecifier:=oColor, _
    NewCompressionModeSpecifier:=oCOMPRESS

    I got RasterFormat from another JPEG raster. And I got ColorMode and CompressionMode from it, then I set them as the arguments for ExortRasterFile.

    Then I excuted my improved code, it made JPEG from TIFF.

    Answer Verified By: hirotaka nisimura 

  • Thank you Jon. I checked the filename. In the case that I set 'JPEG' for Raster, the new file was saved. But it was TIFF, not JPEG format.

    And I chekced RasterFormats class using your code. As the result, The string of RasterFormat for JPEG is 'JPEG(J)' so I tried again. But it don't work.

  • I get run time error 5

    Add some Debug statements to help find the problem.

    Const Backslash As String = "\"
    Dim source As String
    source = fpath & Backslash & fName
    Debug.Print "Source=" & source
    Dim raster As String
    raster = fpath & Backslash & fso.GetBaseName(fname) & ".jpg"
    Debug.Print "Raster=" & raster

    The Debug.Print statement prints to the VBA Immediate window.  Are those valid file names?

    The RasterFormats class is not well-documented.  It's not clear whether JPEG(J) is a valid search criterion.  The sole example I can find is...

     Set oFormat = RasterManager.RasterFormats.Find("TI~FF")
    

    This always fails...

    Sub TestRaster()
        Dim rm As RasterManager
        Set rm = RasterManager
        Dim oFormat As raster
        Dim formatName As String
        formatName = "JPEG(J)"
        Set oFormat = rm.RasterFormats.Find(formatName)
        If oFormat Is Nothing Then
            Debug.Print "Invalid format name " & formatName
        Else
            Debug.Print "Format name " & formatName & " is valid"
        End If
    End Sub
    

    However, "JPEG" is valid. 

    Here's how to list all raster formats...

    Sub TraceRasterFormats()
        Dim oFormat As RasterFormat
        Dim oFormats As RasterFormats
        Dim nFormat As Long
        nFormat = 1
        Dim msg     As String
        Set oFormats = RasterManager.RasterFormats
        For Each oFormat In oFormats
            msg = "Raster Format [" & CStr(nFormat) & "] = '" & oFormat.ToString() & "'"
            ShowMessage msg, msg, msdMessageCenterPriorityDebug
            Debug.Print msg
            nFormat = 1 + nFormat
        Next
    End Sub
    

     
    Regards, Jon Summers
    LA Solutions