我正在 Delphi XE2 中开发一个程序,该程序需要能够将 Windows 增强型图元文件转换为位图。下面的代码用于执行转换:
procedure TForm1.Button8Click(Sender: TObject);
var
Bitmap : TBitmap;
Metafile : TMetafile;
begin
Metafile := TMetafile.Create();
Bitmap := TBitmap.Create;
try
Metafile.LoadFromFile(Edit1.Text);
Bitmap.Width := Metafile.Width;
Bitmap.height:= Metafile.Height;
Bitmap.Canvas.Draw(0,0,Metafile);
Bitmap.SaveToFile(ChangeFileExt(Edit1.Text, '.bmp'));
finally
Bitmap.Free();
Metafile.Free();
end;
end;
对于某些图像文件,原始图元文件中非常清晰的文本在最终位图中显得有些模糊。不幸的是,我无法在这里发布示例图像,因为我没有足够的声誉点,但是如果您比较以下问题中的两个图像,您可以看到我正在谈论的事情:
when rendering metafile, the texts are too large
我在两台机器上对此进行了测试(均为 Windows 7;一台 32 位,另一台 64 位)。该问题仅出现在64位机器上;在 32 位计算机上转换完全相同的图像文件会产生具有正常文本的位图。
到目前为止我尝试过的事情:
将 32 位计算机上存在但 64 位计算机上不存在的所有字体安装到 64 位计算机上。生成的位图中的文本仍然模糊。
尝试使用 SynGdiPlus 库而不是上述代码来执行转换。生成的位图中的文本仍然模糊。
尝试在 EMF Explorer 中打开原始图像文件。无论是否启用 GDI+,显示的文本都不会模糊。
有人对我如何解决这个问题有任何建议吗?
这是两张图片:
64位机上制作的版本:
32位机上制作的版本:
对于我正在处理的场景,我更喜欢在 32 位机器上制作的第二个图像。
最佳答案
{1} Edit: Since we've established that you're not the one creating the original meta after this answer has been posted, please refer to section Retrieving records from an existing MetaFile.
{2} Edit: Per your second problem with identifying font settings, please refer to second update section Retrieving font structure record.
ClearType真是一个泡菜。借助集成的 ClearType 调谐器,任何人都可以根据自己的意愿改变混合颜色的强度。因此,考虑到图像,您不能依赖每个单独系统的 ClearType 设置。
据我所知,唯一真正的解决方案是忽略自定义 ClearType 渲染并使用预配置的渲染。
<小时/>编辑 1:从现有元文件中检索记录
您可以通过 Enhanced Metafile Operations 修改现有图元文件更具体地说,通过 EnumEnhMetaFile具有回调函数的函数EnhMetaFileProc您可以使用它来处理记录。
使用PlayEnhMetaFileRecord函数一次解析并检查每条记录。 有关如何编辑和修改特定记录的更多信息,请参阅here 。
在接下来的某个时刻,您将必须使用下面的代码来修改现有的字体渲染。
<小时/>编辑2:检索字体结构记录
就像您可以通过 EMREXTTEXTOUTA 检索位置和文本一样结构中,您还可以通过 EMREXTCREATEFONTINDIRECTW 检索使用的字体设置结构。此结构将允许您获取 LOGFONT 定义类型的字体记录,其中包含有关字体的大部分信息,除了所使用的画笔之外。
如果您查看我原来的答案代码,您会发现字体的颜色是由所使用的画笔定义的。因此,同样,您必须使用不同的结构来获取该信息,即 EMRCREATEBRUSHINDIRECT结构。 LOGBRUSH32 类型成员包含有关所用画笔的颜色和样式的信息。
<小时/>原始答案
为了实现这一点,您必须求助于使用 GDI+,因为 Win32 增强型图元文件的 delphi 封装并不完整。使用Delphi GDI+ library .
uses
GDIPlus,GDIPlusHelpers
const
Deftext = 'Lorem ipsum dolor sit amet,'
+sLineBreak+'consectetur adipisicing elit, sed do eiusmod tempor incididunt'
+sLineBreak+'ut labore et dolore magna aliqua.';
procedure CreateEmF(const EmfFileName : TFileName);
var
Graphics : IGPGraphics;
xBrush: IGPBrush;
xFontFamily: IGPFontFamily;
xFont: IGPFont;
DC: HDC;
Metafile: IGPMetafile;
begin
xBrush := TGPSolidBrush.Create(TGPColor.Create(0, 0, 0));
xFontFamily := TGPFontFamily.Create('Segoe UI');
xFont := TGPFont.Create(xFontFamily, 12, FontStyleRegular, UnitPoint{UnitPixel});
DC := GetDC(0);
try
Metafile := TGPMetafile.Create(EmfFileName, DC);
Graphics := TGPGraphics.Create(Metafile);
{
Use Presets instead of the DefaultSystemRendering
TextRenderingHintAntiAliasGridFit - Preset ClearType Rendering
TextRenderingHintSingleBitPerPixelGridFit - Preset Normal Rendering
}
Graphics.TextRenderingHint := TextRenderingHintAntiAliasGridFit;
Graphics.DrawString(Deftext, xFont, TGPPointF.Create(50, 50), xBrush);
Graphics := nil;
finally
ReleaseDC(0, DC);
end;
end;
procedure ConvertEmf2Bmp(const EMFFileName, BMPFileName: TFileName) ;
var
MetaFile : TMetafile;
Bitmap : TBitmap;
begin
Metafile := TMetaFile.Create;
Bitmap := TBitmap.Create;
try
MetaFile.LoadFromFile(EMFFileName);
with Bitmap do
begin
SetSize(MetaFile.Width,MetaFile.Height);
Canvas.Draw(0, 0, MetaFile) ;
SaveToFile(BMPFileName) ;
end;
finally
Bitmap.Free;
MetaFile.Free;
end;
end;
关于delphi - 在 Delphi XE2 中将 Windows 图元文件转换为位图时文本模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16881268/