1

目前我有一个生成元文件的第三方控件。我可以毫无问题地将 .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 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

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

4

2 回答 2

0

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

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
于 2010-03-25T01:27:11.913 回答
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
于 2010-04-09T17:08:19.553 回答