转换图像

时间:2017-10-20 16:26:31

标签: vb.net image crystal-reports wmf

我试图将此代码转换为vb.net并添加一些代码来制作文件......但文件的分辨率变成了垃圾,我似乎无法将其插入到Crystal报表中,我做了吗?将其转换为vb.net时出了什么问题?提前谢谢:)

以下是原始链接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

1 个答案:

答案 0 :(得分: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