понедельник, 5 октября 2009 г.

Get bytes from metafile (EMF)

Module Emf_Module
    '<DllImportAttribute("gdiplus.dll")> _
    'Private Function GdipEmfToWmfBits(ByVal _hEmf As IntPtr, ByVal _bufferSize As UInteger, ByVal _buffer As Byte(), ByVal _mappingMode As Integer, ByVal _flags As EmfToWmfBitsFlags) As UInteger
    'End Function
    <System.Runtime.InteropServices.DllImport("gdi32")> _
    Private Function GetEnhMetaFileBits(ByVal hemf As Integer, ByVal cbBuffer As Integer, ByVal lpbBuffer As Byte()) As Integer
    End Function

    Public Function TrySaveEmf(ByVal fileName As String, ByVal emfFile As Metafile) As Boolean
        Try
            Dim emfBytes As Byte() = GetBytes(emfFile)
            Dim fs As New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)
            Using bWriter As New BinaryWriter(fs)
                bWriter.BaseStream.Position = 0
                bWriter.BaseStream.Write(emfBytes, 0, emfBytes.Length)
                bWriter.BaseStream.Flush()
            End Using
            Return True
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Public Function TrySaveEmf")
            Return False
        End Try
    End Function

    Private Function GetBytes(ByVal img As Metafile) As Byte()
        Dim mf As Metafile = TryCast(img, Metafile)
        Dim enhMetafileHandle = mf.GetHenhmetafile().ToInt32()
        Dim bufferSize = GetEnhMetaFileBits(enhMetafileHandle, 0, Nothing)
        Dim buffer = New Byte(bufferSize - 1) {}
        GetEnhMetaFileBits(enhMetafileHandle, bufferSize, buffer)
        ' return bits
        Return buffer
    End Function
End Module

Комментариев нет:

Отправить комментарий