Writing text in bitmap


Simple question, what is the easiest way to write text in a bitmap at a given position and controling the font size? .NET , Post? Any example will be appreciated.



If you have access to Python you can use the PIL Imaging library. It’s really easy to learn, tons of tutorials / scripts etc. I’m loving it so far. It’s also compatible with newer versions of 3ds Max. :slight_smile:


Well thanks but don’t know Python :confused:


You could use the SVG engine that comes with the vector texturmap
See the MXS example at the bottom of this page, titled “Using SVG To Render Text To Bitmap” :wink:


this is how I do it

	fn DotNetFont fontname size style =
   		FontStyle = dotnetclass "System.Drawing.FontStyle";
   		fs = case style of
   			#regular: 	FontStyle.regular;
   			#bold:		FontStyle.bold;
   			#italic:	dotnetclass.italic;
   			#underline:	FontStyle.underline;
   			#strikeout:	FontStyle.strikeout;
   			default: 	FontStyle.regular;
   		dotnetobject "System.Drawing.Font" fontname size fs;
   fn DotNetPasteBitmap src_bitmap dest_bitmap dest_rect =
   		gr = (dotnetclass "System.Drawing.Graphics").FromImage dest_bitmap;
   		gr.DrawImage src_bitmap	dest_rect;
   fn DotNetOffsetRect pos width height = 
   		( dotnetobject "System.Drawing.Rectangle" (DotNetPoint pos) (DotNetSize width height); )
   fn DotNetColor mc = ((dotnetclass "System.Drawing.Color").FromArgb mc.r mc.g mc.b; )
   fn DotNetTextToBitmap txt font col =
   -- create temp context so we can measure the text width		
   		temp_bm = dotnetobject "System.Drawing.Bitmap" 1 1
   		temp_gr = (dotnetclass "System.Drawing.Graphics").FromImage temp_bm
   		sz = (temp_gr.MeasureString txt font).ToSize();
   -- create the actual draw context		
   		bm = dotnetobject "System.Drawing.Bitmap" sz.width (font.GetHeight())
   		gr = (dotnetclass "System.Drawing.Graphics").FromImage bm
   -- set render to best quality			
   		gr.PixelOffsetMode = gr.PixelOffsetMode.HighQuality
   		gr.TextRenderingHint = gr.TextRenderingHint.AntiAliasGridFit
   -- make the background transparent			
   		gr.Clear (dotnetclass "System.Drawing.Color").transparent
   -- render the string to the bitmap			
   		gr.DrawString txt font (dotnetobject "System.Drawing.SolidBrush" col) 0 0 
   -- release the drawing context			
   -- return the bitmap			

then call something like this where dest_bm is also a dot net bitmap/image

   darkbluegray = color 24 24 32;
   font = DotNetFont "Comic Sans Ms" 11 #bold;
   text_bm = DotNetTextToBitmap "Hello World"  font (DotNetColor darkbluegray) ;
   DotNetPasteBitmap text_bm dest_bm (DotNetOffsetRect pos width height);

you can also render directly to the image with a few changes to the above


Wow Thanks guys I have everything to complet my script!