VB.net保存一个元文件/ EMF作为位图(.TIFF)
题
目前我有产生图元文件第三方控制。我可以保存.WMF文件到磁盘中出的问题。问题是我怎么渲染图元文件为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作为System.IntPtr,BYVAL n大小作为UInteger,BYVAL的lpData作为IntPtr的)作为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
我已经尝试过各种图像和图形调用的,只是不能保存的元文件作为.TIFF。任何建议将是巨大的。我甚至尝试创建一个新的位图和绘制图元文件到它。我总是最后被抛出一个异常GDI。
解决方案 2
我的最终的代码如下所示:
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
其他提示
您代码难以破译。简单绘制图元文件的位图应该把工作做好。例如:
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
不隶属于 StackOverflow