Notifications
Clear all

[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.

15 Replies

Wait for it… Wait for it… Denis has a question!!!

(sorry Denis couldn’t resist)
:bowdown:

1 Reply
 lo1
(@lo1)
Joined: 11 months ago

Posts: 0

I bet he already knows the answer and just wants to see if someone else knows

 lo1

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?

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

the only way that i know is post-processing. but i hope there is some smarter way

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.

 lo1

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.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

yes. i can make 8-bit alpha with drawstring… but the mask is 1-px off

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


 lo1

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?

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

are you looking for any sense in every my action?
there is no purpose in this case. it’s atavism

 lo1

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…

Page 1 / 2