2017-10-20 332 views
0

我試圖將此代碼轉換爲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 
+0

爲什麼你認爲它是用C#標籤來標記這是個好主意? – DavidG

+0

@DavidG我最好的猜測如果我是一個投注人,他是用C#標記的,因爲他從C#獲得代碼的鏈接。正如你已經提到的那樣,它在這裏沒有關係,並且已經編輯了該問題以移除違規標籤。 – Codexer

回答

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