0

I Tried to convert this code into vb.net and add a little code to make a file... but the file's resolution became crap and i cant seem to insert it into the Crystal report, Did i do something wrong while converting it into vb.net? thanks in advance :)

Here is the original link Convert an image into WMF with .NET?

<Flags>
Private Enum EmfToWmfBitsFlags
    EmfToWmfBitsFlagsDefault = &H0
    EmfToWmfBitsFlagsEmbedEmf = &H1
    EmfToWmfBitsFlagsIncludePlaceable = &H2
    EmfToWmfBitsFlagsNoXORClip = &H4
End Enum

Private Shared MM_ISOTROPIC As Integer = 7
Private Shared MM_ANISOTROPIC As Integer = 8
<DllImport("gdiplus.dll")>
Private Shared Function GdipEmfToWmfBits(_hEmf As IntPtr, _bufferSize As UInteger, _buffer As Byte(), _mappingMode As Integer, _flags As EmfToWmfBitsFlags) As UInteger
End Function
<DllImport("gdi32.dll")>
Private Shared Function SetMetaFileBitsEx(_bufferSize As UInteger, _buffer As Byte()) As IntPtr
End Function
<DllImport("gdi32.dll")>
Private Shared Function CopyMetaFile(hWmf As IntPtr, filename As String) As IntPtr
End Function
<DllImport("gdi32.dll")>
Private Shared Function DeleteMetaFile(hWmf As IntPtr) As Boolean
End Function
<DllImport("gdi32.dll")>
Private Shared Function DeleteEnhMetaFile(hEmf As IntPtr) As Boolean
End Function


Private Function MakeMetafileStream(image As Bitmap) As Byte()
    Dim metafile As Metafile = Nothing
    Using g As Graphics = Graphics.FromImage(image)
        Dim hDC As IntPtr = g.GetHdc()
        metafile = New Metafile(hDC, EmfType.EmfOnly)
        g.ReleaseHdc(hDC)
    End Using

    Using g As Graphics = Graphics.FromImage(metafile)
        g.DrawImage(image, 0, 0)
    End Using
    Dim _hEmf As IntPtr = metafile.GetHenhmetafile()
    Dim _bufferSize As UInteger = GdipEmfToWmfBits(_hEmf, 0, Nothing, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
    Dim _buffer As Byte() = New Byte(_bufferSize - 1) {}
    GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
    Dim hmf As IntPtr = SetMetaFileBitsEx(_bufferSize, _buffer)
    Dim tempfile As String = Path.GetTempFileName()
    CopyMetaFile(hmf, tempfile)
    DeleteMetaFile(hmf)
    DeleteEnhMetaFile(_hEmf)

    Dim stream = New MemoryStream()
    Dim data As Byte() = File.ReadAllBytes(tempfile)

    Return data
End Function

Private Sub Convert()
    Dim src As New Bitmap("C:\Users\Sample\Desktop\Logos\LogoTransparent\Transparentlogo.png")
    Dim Msteam As Byte() = MakeMetafileStream(src)
    Dim newF As String = "C:\Users\Alvin Rodriguez\Desktop\Logos\new logo transparent\Transparentlogo.wmf"
    System.IO.File.WriteAllBytes(newF, Msteam)
    Msteam = Nothing
End Sub
Trevor
  • 7,777
  • 6
  • 31
  • 50
Orion Star
  • 1
  • 1
  • 5
  • Why did you think it was a good idea to tag this with C# tag? – DavidG Oct 20 '17 at 16:26
  • @DavidG my best guess if I was a betting man, is he tagged with C# because the link he got the code from was in C#. As you mentioned already, it is not relevant here and the question has been edited to remove the offending tag. – Trevor Oct 20 '17 at 18:23

1 Answers1

0

Some interface are to be modifed:

Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As IntPtr, ByVal lpszFile As String) As IntPtr

Declare Function DeleteEnhMetaFile Lib "gdi32" (ByVal hemf As IntPtr) As Integer

Private Declare Function GdipEmfToWmfBits Lib "gdiplus.dll" (ByVal hEmf As IntPtr,     ByVal bufferSize As UInteger, ByVal buffer() As Byte, ByVal mappingMode As Integer,     ByVal flags As EmfToWmfBitsFlags) As UInteger
cramopy
  • 3,459
  • 6
  • 28
  • 42
JPG
  • 1