VB.net将MetaFile / EMF保存为位图(.tiff)

时间:2010-03-24 22:07:13

标签: vb.net bitmap save metafile

目前,我有第三方控件可生成元文件。我可以将.wmf文件保存到磁盘而没有问题。问题是如何将Metafile呈现为Tiff文件。

目前我有以下代码来获取我的图元文件并保存它。

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



                        Dim enhMetafileHandle As IntPtr = mf.GetHenhmetafile()

                        Dim 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)

结束子

_         公共共享函数GetEnhMetaFileBits(ByVal hEMF As System.IntPtr,ByVal nSize As UInteger,ByVal lpData As IntPtr)As UInteger     结束功能

<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

我尝试了各种IMAGE和Graphic调用,但无法将元文件保存为.tiff。任何建议都会很棒。我甚至尝试创建一个新的位图并将元文件绘制到它上面。我总是最终抛出一个GDI异常。

2 个答案:

答案 0 :(得分:1)

您的代码难以破译。只需将元文件绘制到位图即可完成工作。例如:

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

答案 1 :(得分:0)

我的最终代码如下:

 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