我使用以下代码绘制图元文件: {
<DllImport("gdi32.dll", EntryPoint:="CopyEnhMetaFileA", BestFitMapping:=False, ThrowOnUnmappableChar:=True, _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function CopyEnhMetaFile(ByVal hemfSrc As IntPtr, ByVal lpszFile As String) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteEnhMetaFile", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function DeleteEnhMetaFile(ByVal hemfSrc As IntPtr) As Boolean
End Function
Private Sub DrawArialTest()
Dim TestString = "11111111111112"
Dim metafileStream = New MemoryStream()
Dim offScreenDC = Graphics.FromHwndInternal(IntPtr.Zero)
Dim myImagePointer = offScreenDC.GetHdc()
Dim Image = New Metafile(metafileStream, myImagePointer, EmfType.EmfPlusDual)
' Create the graphics object
Using g = Walker.GraphicsUtilities.GdiGraphics.FromImage(Image)
SetGraphicsSettings(g)
Try
' Draw in the metafile
Dim sFormat = New StringFormat
sFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces
Dim Y = 0
For intI As Single = 0 To 30
Using font = New Font("Arial", 6 + intI, FontStyle.Regular)
Dim size = g.MeasureString(TestString, font, PointF.Empty, sFormat)
Dim rec = New RectangleF(0, Y, size.Width, size.Height)
g.DrawString(TestString, font, System.Drawing.Brushes.Black, rec, sFormat)
g.DrawRectangles(Pens.Red, New RectangleF() {rec})
Y += rec.Height
End Using
Next
sFormat.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
SaveEnhMetafile(Image, "c:\temp\test.emf")
End Sub
Public Shared Function SaveEnhMetafile(ByVal sourceMetafile As Metafile, ByVal fileName As String) As Boolean
Dim hEMF As IntPtr = DirectCast(sourceMetafile.Clone, Metafile).GetHenhmetafile()
If Not hEMF.Equals(New IntPtr(0)) Then
If System.IO.File.Exists(fileName) Then Kill(fileName)
DeleteEnhMetaFile(CopyEnhMetaFile(hEMF, fileName))
DeleteEnhMetaFile(hEMF)
Return True
End If
Return False
End Function
Sub SetGraphicsSettings(ByVal g As IGraphics)
g.SmoothingMode = SmoothingMode.AntiAlias
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.CompositingQuality = CompositingQuality.GammaCorrected
'g.TextRenderingHint = Text.TextRenderingHint.ClearTypeGridFit
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit
g.PixelOffsetMode = PixelOffsetMode.HighQuality
End Sub
}
如果我从这个测试中打开图元文件,我会看到以下内容:
Win8.1 64位
Win7 64bit
除了Arial,我无法使用任何其他字体重现此错误。任何帮助将不胜感激。
谢谢!
答案 0 :(得分:0)
如果其他人对此感兴趣,我从微软获得的是:
Microsoft于2015年5月22日下午1:39发布 你好!感谢您的反馈意见。这是GDIPlus API GdipMeasureString中的错误,而不是Graphics.MeasureString中的错误。对不起,你被误导到了这个论坛。我为GDI +团队打开了一个内部错误,他们将对其进行调查和分类。由于这个错误是Dot Net Framework的外部错误,我正在解决它。 谢谢, Windows窗体小组