0

我试图将此代码转换为 vb.net 并添加一些代码来制作文件......但文件的分辨率变得很糟糕,我似乎无法将其插入到水晶报表中,我在将其转换为 vb 时做错了什么。网?提前致谢 :)

这是原始链接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
4

1 回答 1

0

部分界面需要修改:

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
于 2017-11-30T19:56:52.377 回答