[Closed] Colored text with dotnet?Is it possible?
The code below is not mine. I get it from “info_strip” script, created by Vojtech Cada.
What I want to do is to make the str text colored. Something like this:
“Tris: 8945 | Length: 56,7 cm | Heigth: 192,4 cm | Width: 164,78 cm | UV verts: 6372”
Is it possible via dotnet and if it how to do it?
I think that I can do it by creating diferent bitmap for every part of the colored text, but…
(
local fName = getFiles (GetDir #preview+"\\*.bmp")
local img = openBitmap fName[1]
--
local fontForeColor = Yellow
local fontBackColor = black
local fontName = "Tahoma"
local fontSize = 14
--
local stripHeight = 20 -- the height of the stamp field
--
local str = "Tris: 8945 | Length: 56,7 cm | Heigth: 192,4 cm | Width: 164,78 cm | UV verts: 6372"
--
local dnFontStyle = dotNetClass "System.Drawing.FontStyle"
local dnColor = dotNetClass "System.Drawing.Color"
local fontStyle = dnFontStyle.Regular
local backgroundBrush = dotNetObject "System.Drawing.SolidBrush" (dnColor.FromArgb 0 0 0 125)
local textBrush = dotNetObject "System.Drawing.SolidBrush" (dnColor.FromArgb fontForeColor.r fontForeColor.g fontForeColor.b)
local dnFont = dotNetObject "System.Drawing.Font" fontName fontSize fontStyle
local fileName = (dotNetClass "System.IO.Path").ChangeExtension ((dotNetClass "System.IO.Path").GetTempFileName()) "png"
local drawingBitmap = dotNetObject "System.Drawing.Bitmap" img.width stripHeight
local overlayStrip = (dotNetClass "System.Drawing.Graphics").FromImage drawingBitmap
overlayStrip.FillRectangle backgroundBrush 0 0 img.width stripHeight
overlayStrip.TextRenderingHint = overlayStrip.TextRenderingHint.AntiAlias
overlayStrip.DrawString str dnFont textBrush 0 0
overlayStrip.DrawImage drawingBitmap (dotNetObject "System.Drawing.Rectangle" 0 0 img.width stripHeight)
overlayStrip.Dispose()
overlayStrip = undefined
drawingBitmap.Save fileName (dotNetClass "System.Drawing.Imaging.ImageFormat").png
drawingBitmap.Dispose()
drawingBitmap = undefined
local stripBitmap = openBitmap fileName
deleteFile fileName
local plainStrip = bitmap img.width stripHeight color:fontBackColor
pasteBitmap plainStrip img (Box2 0 0 img.width stripHeight) [0,img.height-stripHeight] type:#blend
pasteBitmap stripBitmap img (Box2 0 0 img.width stripHeight) [0,img.height-stripHeight] type:#blend
display img
freeSceneBitmaps()
gc()
)
Another question – in the orignal “info_strip” script blending work fine, but in my modification the blend does not work. Why?
try(form.close()) catch()
form = dotnetobject "MaxCustomControls.Maxform"
form.Text = ".NET RichTextBox"
form.StartPosition = form.StartPosition.Manual
form.Location = dotnetobject "System.Drawing.Point" 600 300
form.Size = dotnetobject "System.Drawing.Size" 500 100
tb = dotnetobject "RichTextBox"
tb.Height = 20
tb.Dock = tb.Dock.Top
form.Controls.add tb
form.showmodeless()
(
c = dotnetclass "System.Drawing.Color"
fn printColored text tb colors:#(c.Red, c.Green) tokens:" " =
(
tb.SuspendLayout()
txt = filterstring text tokens splitEmptyTokens:on
--tb.ForeColor = tb.ForeColor.Black
tb.Text = text
tb.SelectionStart = 0
for k=0 to txt.count-1 do
(
s = txt[k+1].count
tb.SelectionLength = s
tb.selectionColor = colors[(mod k colors.count) + 1]
tb.SelectionStart += s+1
)
tb.SelectionLength = 0
tb.ResumeLayout()
)
printColored "Tris: 8945 | Length: 56,7 cm | Heigth: 192,4 cm | Width: 164,78 cm | UV verts: 6372" tb colors:#(c.Blue, c.Orange) tokens:":|"
)
I’m not very familiar with dotnet, so for me is not very easy.
I think that these two lines create the new bitmap:
overlayStrip.DrawString str dnFont textBrush 0 0
overlayStrip.DrawImage drawingBitmap (dotNetObject "System.Drawing.Rectangle" 0 0 img.width stripHeight)
The “str” variable get its text from other part of the script, not from textbox or other control. So, if I create the string in RichTextBox(or paste it in the RichTextBox and then get it back colored) will the
“overlayStrip.DrawString str dnFont textBrush 0 0” draw colored text on “drawingBitmap”?
denisT, thank you very much.
Now I have to find a way to save the RichTextBox with colored text as a bitmap
I think the OP wants it for a frame stamp.
Try the .DrawToBitmap method
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.drawtobitmap.aspx
oh
that’s unfortunate
how about using the method suggested in this thread:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/1454d078-c312-4741-88df-aa7eb306fe51/
and by that, I mean putting the richTextbox in a topmost form, showing it, grabbing it, and then disposing the form
But then again, if you’re going to get that convoluted, you might as well draw the text yourself as in the original post.
The link that lo posted works, I’ve implemented that one myself some time ago
@lo is right. I want to create a bitmap that I will use for something like frame stamp. The “Info_strip” script do this, but I want to use the code from my first post for something a little different.