Results 1 to 24 of 24

Thread: [RESOLVED] How can I change by EMF byte array to jpeg file?

Threaded View

  1. #9
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,673

    Re: How can I change by EMF byte array to jpeg file?

    Why would you go through StdPicture?

    Code:
    Public Sub EmfStreamToJpg(bEMF() As Byte, sSaveTo As String)
    'this doesn't have to be EMF by the way, you could load any image type
    'that GDI+ supports into the byte array without any other changes
    Dim pStrm As IUnknown
    Dim hEMF As Long
    
    Set pStrm = pvStreamFromArray(VarPtr(bEMF(0)), UBound(bEMF) + 1)
    Call GdipLoadImageFromStream(pStrm, hEMF)
    If hEMF Then
       Call gdipImgToFileJPG(hEMF, sSaveTo)
       GdipDisposeImage hEMF
    End If
    End Sub
    Public Function gdipImgToFileJPG(hImg As Long, sOut As String) As Long
    Dim encoderCLSID As CLSIDG
    Call GetEncoderClsid("image/jpeg", encoderCLSID) 'can also substitute any other supported codec, like image/png or image/bmp
    GdipSaveImageToFile hImg, StrConv(sOut, vbUnicode), encoderCLSID, ByVal 0&
    End Function
    
    '
    'generic support declares/funcs:
    
    Public Type CLSIDG
       Data1 As Long
       Data2 As Integer
       Data3 As Integer
       Data4(0 To 7) As Byte
    End Type
    Public Type ImageCodecInfo
       ClassID As CLSIDG
       FormatID As CLSIDG
       CodecName As Long      ' String Pointer; const WCHAR*
       DllName As Long        ' String Pointer; const WCHAR*
       FormatDescription As Long ' String Pointer; const WCHAR*
       FilenameExtension As Long ' String Pointer; const WCHAR*
       MimeType As Long       ' String Pointer; const WCHAR*
       Flags As Long 'ImageCodecFlags   ' Should be a Long equivalent
       Version As Long
       SigCount As Long
       SigSize As Long
       SigPattern As Long      ' Byte Array Pointer; BYTE*
       SigMask As Long         ' Byte Array Pointer; BYTE*
    End Type
    
    
    Public Function GetEncoderClsid(strMimeType As String, ClassID As CLSIDG)
       Dim num As Long, SIZE As Long, i As Long
       Dim ICI() As ImageCodecInfo
       Dim Buffer() As Byte
       On Error GoTo e0
       GetEncoderClsid = -1 'Failure flag
    
       ' Get the encoder array size
       Call GdipGetImageEncodersSize(num, SIZE)
       If SIZE = 0 Then Exit Function ' Failed!
    
       ' Allocate room for the arrays dynamically
       ReDim ICI(1 To num) As ImageCodecInfo
       ReDim Buffer(1 To SIZE) As Byte
    
       ' Get the array and string data
       Call GdipGetImageEncoders(num, SIZE, Buffer(1))
       ' Copy the class headers
       Call CopyMemory(ICI(1), Buffer(1), (Len(ICI(1)) * num))
    
       ' Loop through all the codecs
       For i = 1 To num
          ' Must convert the pointer into a usable string
          If StrComp(PtrToStrW(ICI(i).MimeType), strMimeType, vbTextCompare) = 0 Then
             ClassID = ICI(i).ClassID   ' Save the class id
             GetEncoderClsid = i        ' return the index number for success
             Exit For
          End If
       Next
       ' Free the memory
       Erase ICI
       Erase Buffer
       Exit Function
    e0:
       Debug.Print "GetEncoderCLSID.Error->" & Err.Description
    End Function
    Private Function PtrToStrW(ByVal lpsz As Long) As String
        Dim sOut As String
        Dim lLen As Long
    
        lLen = lstrlenW(lpsz)
    
        If (lLen > 0) Then
            sOut = StrConv(String$(lLen, vbNullChar), vbUnicode)
            Call CopyMemory(ByVal sOut, ByVal lpsz, lLen * 2)
            PtrToStrW = StrConv(sOut, vbFromUnicode)
        End If
    End Function
    ---------------
    Note: If you're a fan of my typelib and/or want to do more advanced stuff with the IStream, you can simply replace IUnknown in EmfStreamToJpg and pvStreamFromArray with oleexp3.IStream to get a real IStream object.


    ----
    PS- I can't find much in the way of sample files or programs that can convert; but doesn't EMF+ store the image as PNG or JPEG? Wouldn't that then support transparency, which would be lost with a normal StdPicture object?
    Last edited by fafalone; Nov 18th, 2016 at 12:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width