开发者

VB.net Saving an MetaFile / EMF as a bitmap ( .tiff)

Currently I have a third party control that generates a Metafile. I can save the .wmf file to disk with out issue. The problem is how do I render the Metafile as a Tiff file.

Currently I have the following code to get my metafile and save it.

 Dim mf As Metafile = page.GetImage(TXTextControl.Page.PageContent.All)



                        Dim enhMetafileHandle As IntPtr = mf.GetHenhmetafile()

                        Di开发者_开发百科m h As IntPtr
                        Dim bufferSize As UInteger = GetEnhMetaFileBits(enhMetafileHandle, 0, h)
                        Dim buffer(CInt(bufferSize)) As Byte

                        GetEnhMetaFileBits(enhMetafileHandle, bufferSize, buffer)

                        Dim msMetafileStream As New MemoryStream
                        msMetafileStream.Write(buffer, 0, CInt(bufferSize))


                        Dim baMetafileData() As Byte
                        baMetafileData = msMetafileStream.ToArray
                        Dim g As Graphics = Graphics.FromImage(mf)


                        mf.Dispose()



                        File.WriteAllBytes("c:\a.wmf", baMetafileData)

end sub

_ Public Shared Function GetEnhMetaFileBits( ByVal hEMF As System.IntPtr, ByVal nSize As UInteger, ByVal lpData As IntPtr) As UInteger End Function

<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll", EntryPoint:="GetEnhMetaFileBits")> _

Public Shared Function GetEnhMetaFileBits(<System.Runtime.InteropServices.InAttribute()> ByVal hEMF As System.IntPtr, ByVal nSize As UInteger, ByVal lpData() As Byte) As UInteger
End Function

I've tried all sort of IMAGE and Graphic calls and just can't save the meta file as a .tiff. Any suggestions would be great. I even tried to create a new bitmap and draw the metafile onto it. I always end up with a GDI exception being thrown.


Your code is hard to decipher. Simply drawing the metafile to a bitmap should get the job done. For example:

Dim mf As Metafile = page.GetImage(TXTextControl.Page.PageContent.All)
Using bmp As New Bitmap(mf.Width, mf.Height)
  Using gr As Graphics = Graphics.FromImage(bmp)
    gr.DrawImage(mf, 0, 0)
  End Using
  bmp.Save("c:\temp\test.tiff", ImageFormat.Tiff)
End Using


My final Code Looks like:

 Dim NewGraphic As Graphics = Nothing
        Dim BitonalImage As Bitmap = Nothing

        Using bmp As New Bitmap(3264, 4224)
            Try

                NewGraphic = Graphics.FromImage(bmp)
                NewGraphic.FillRectangle(New SolidBrush(Color.White), 0, 0, 3264, 4224)
                NewGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                NewGraphic.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                NewGraphic.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
                NewGraphic.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
                NewGraphic.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit

                NewGraphic.DrawImage(MetaFileToConvert, 0, 0, 3264, 4224)

                BitonalImage = CType(ConvertToBitonal(CType(bmp, Bitmap)), Bitmap)
                BitonalImage.SetResolution(385, 385)

                OutBMP = BitonalImage

            Catch ex As Exception
                Throw ex
            Finally
                NewGraphic.Dispose()
                BitonalImage.Dispose()
            End Try

        End Using
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜