找回密码
 加入
搜索
查看: 2105|回复: 5

[AU3基础] 【已解决】请教下,用GDI绘制文字,如何消除锯齿,让文字变的清晰

[复制链接]
发表于 2020-5-21 19:40:58 | 显示全部楼层 |阅读模式
本帖最后由 zghwelcome 于 2020-5-22 14:21 编辑



这是下面代码输出的结果,模糊而有锯齿


#include <GDIPlus.au3>

Local $sText = '文字模糊,有锯齿'

Local $iW = 250, $iH = 40, $iFontSize = 20

_GDIPlus_Startup()
Local $hImage = _GDIPlus_BitmapCreateFromScan0($iW, $iH, $GDIP_PXF32PARGB)
_GDIPlus_BitmapSetResolution($hImage, 96,96) ;//调整图像的水平、垂直DPI
Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
DllCall($__g_hGDIPDll, "int", "GdipSetTextRenderingHint", "hwnd", $hGraphic, "int", 4) ;1:无效,0:系统默认,1:最佳性能,2:最佳质量,3:不处理,4:抗锯齿


;背景色填充成白色
_GDIPlus_GraphicsClear($hGraphic, 0xffffffff)

;/ 图像绘制文字
$hBrush = _GDIPlus_BrushCreateSolid(0xFFff0000)
$hFormat = _GDIPlus_StringFormatCreate(0)
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, $iFontSize, 1) ;//字体大小
$tLayout = _GDIPlus_RectFCreate(3, 8, $iW, $iH)
_GDIPlus_GraphicsSetSmoothingMode($hGraphic, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
_GDIPlus_GraphicsDrawStringEx($hGraphic, $sText, $hFont, $tLayout, $hFormat, $hBrush)



;//输出文件
Local $giQuality = 100, $pParams
Local $tParams = _GDIPlus_ParamInit(1)
Local $tData = DllStructCreate('int Quality')
DllStructSetData($tData, 'Quality', $giQuality)
_GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, DllStructGetPtr($tData))
If IsDllStruct($tParams) Then $pParams = DllStructGetPtr($tParams)
Local $CLSID = _GDIPlus_EncodersGetCLSID('JPG')
 _GDIPlus_ImageSaveToFileEx($hImage, @ScriptDir & '\___Out.jpg', $CLSID, $pParams) 
ShellExecute(@ScriptDir & '\___Out.jpg')


;//销毁
$tData = 0
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()


;//调整图像的水平、垂直DPI
Func _GDIPlus_BitmapSetResolution($hBitmap, $nDpiX, $nDpiY)
        Local $aResult = DllCall($__g_hGDIPDll, "uint", "GdipBitmapSetResolution", "hwnd", $hBitmap, "float", $nDpiX, "float", $nDpiY)
        If @error Then Return SetError(@error, @extended, False)
        $GDIP_STATUS = $aResult[0]
        Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_BitmapSetResolution




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2020-5-21 20:24:31 | 显示全部楼层
GdipSetTextRenderingHint 没必要,_GDIPlus_GraphicsSetSmoothingMode 就行了
jpg 属于有损压缩,对文字表现劣势明显,如无必要,一般最好还是使用 bmp 或者 png 格式
 楼主| 发表于 2020-5-21 20:53:05 | 显示全部楼层
afan 发表于 2020-5-21 20:24
GdipSetTextRenderingHint 没必要,_GDIPlus_GraphicsSetSmoothingMode 就行了
jpg 属于有损压缩,对文字 ...

谢谢A版,感觉效果还是没达到期望值
发表于 2020-5-21 21:03:19 | 显示全部楼层
zghwelcome 发表于 2020-5-21 20:53
谢谢A版,感觉效果还是没达到期望值

你还是用的 jpg 格式吧,那肯定不行,上面说了~  就算你用100的质量仍旧劣势
上面说的 _GDIPlus_GraphicsSetSmoothingMode 就行了 是针对去锯齿。
你保存png试试
 楼主| 发表于 2020-5-21 22:16:28 | 显示全部楼层
afan 发表于 2020-5-21 21:03
你还是用的 jpg 格式吧,那肯定不行,上面说了~  就算你用100的质量仍旧劣势
上面说的 _GDIPlus_Graphic ...

我是改保存了PNG,效果确实比JPG好,但是还是和理想的有点差距
发表于 2020-5-21 23:04:38 | 显示全部楼层
zghwelcome 发表于 2020-5-21 22:16
我是改保存了PNG,效果确实比JPG好,但是还是和理想的有点差距

那就只能换字体试试了,你用的粗体,但是尺寸只有20,感觉不会达到你理想中的样子…
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-6-3 10:27 , Processed in 0.072741 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表