[Closed] DrawText with the correct alpha ?
here is a sample how to draw text to bitmap… it’s saves PNG file. But Alpha mask of the bitmap and Mask of the PNG file is not correct. Does anyone know the right way to get the alpha?
fn waterMark txt font: fore: =
(
temp = (dotNetClass "System.IO.Path").ChangeExtension ((dotNetClass "System.IO.Path").GetTempFileName()) "png"
-- setclipboardtext temp
if font == unsupplied do font = (dotnetclass "SystemInformation").MenuFont
tr = dotnetclass "TextRenderer"
sz = tr.MeasureText txt font
if fore == unsupplied do fore = (dotnetclass "System.Drawing.Color").Red
back = (dotnetclass "System.Drawing.Color").Transparent
bm = dotnetobject "System.Drawing.Bitmap" sz.width (font.GetHeight())
global gr = (dotnetclass "System.Drawing.Graphics").FromImage bm
-- gr.CompositingMode = gr.CompositingMode.SourceOver
-- gr.CompositingQuality = gr.CompositingQuality.HighQuality
-- gr.SmoothingMode = gr.SmoothingMode.HighQuality
-- gr.TextRenderingHint = gr.TextRenderingHint.SystemDefault
clip = dotnetobject "System.Drawing.Rectangle" 0 0 bm.width bm.height
-- gr.Clear back.white
tr.DrawText gr txt font clip fore back
gr.DrawImage bm clip
bm.Save temp (dotnetclass "System.Drawing.Imaging.ImageFormat").png
gr.Dispose()
bm.Dispose()
bb = openbitmap temp
-- deletefile temp
display bb
bb
)
font = dotnetobject "System.Drawing.Font" "Tahoma" 14 (dotnetclass "System.Drawing.FontStyle").Regular
waterMark "Hey, World!" font:font
ps. no post process please.
Wait for it… Wait for it… Denis has a question!!!
(sorry Denis couldn’t resist)
:bowdown:
as far as I know it’s a limitation of text rendering in gdi. There’s no 8-bit alpha value in the resulting image, only a 1-bit transparency value. What’s wrong with post processing?
i swear i don’t have an answer.
i can use GDI+ with Graphics.DrawString. but i have the same result. The mask is shifted 1 px left against the text.
What do you mean? You do get an 8-bit alpha but it’s not in the right position? I get a 1-bit alpha both with textrenderer and drawstring.
i can explain what i want to achieve.
you probably saw when some custom controls do dragging of a content with semi-transparent bitmap
(devx controls for example). But most of them do it with ugly 1-bit transparensy bitmap. I want to make it better!
i have some better result… almost OK
fn waterMark txt font: fore: =
(
temp = (dotNetClass "System.IO.Path").ChangeExtension ((dotNetClass "System.IO.Path").GetTempFileName()) "png"
setclipboardtext temp
if font == unsupplied do font = (dotnetclass "SystemInformation").MenuFont
bm = dotnetobject "System.Drawing.Bitmap" 1 1
global gr = (dotnetclass "System.Drawing.Graphics").FromImage bm
sz = (gr.MeasureString txt font).ToSize()
if fore == unsupplied do fore = (dotnetclass "System.Drawing.Color").Red
back = (dotnetclass "System.Drawing.Color").Transparent
bm = dotnetobject "System.Drawing.Bitmap" sz.width (font.GetHeight())
global gr = (dotnetclass "System.Drawing.Graphics").FromImage bm
-- gr.CompositingMode = gr.CompositingMode.SourceOver
-- gr.CompositingQuality = gr.CompositingQuality.HighQuality
-- gr.SmoothingMode = gr.SmoothingMode.HighQuality
gr.PixelOffsetMode = gr.PixelOffsetMode.HighQuality
gr.TextRenderingHint = gr.TextRenderingHint.AntiAliasGridFit --ClearTypeGridFit
clip = dotnetobject "System.Drawing.Rectangle" 0 0 bm.width bm.height
gr.Clear back
gr.DrawString txt font (dotnetobject "System.Drawing.SolidBrush" fore) 0 0
gr.DrawImage bm (dotnetobject "System.Drawing.Rectangle" 0 0 bm.width bm.height)
-- bm.MakeTransparent back
bm.Save temp (dotnetclass "System.Drawing.Imaging.ImageFormat").png
gr.Dispose()
bm.Dispose()
bb = openbitmap temp
-- deletefile temp
display bb
bb
)
font = dotnetobject "System.Drawing.Font" "Tahoma" 14 (dotnetclass "System.Drawing.FontStyle").Regular
waterMark "Hey, World!" font:font
cool… why almost? what’s wrong with it.
also, what is the purpose of
gr.DrawImage bm (dotnetobject "System.Drawing.Rectangle" 0 0 bm.width bm.height)
you are drawing the image on itself?
are you looking for any sense in every my action?
there is no purpose in this case. it’s atavism
Yes, always
In any case, what is wrong with the result you are getting? It looks ok to me
TextRenderingHint.ClearTypeGridFit is the highest quality setting. But it doesn’t work right for me…