Jump to content

Render Text


Gilbert

Recommended Posts

Hello!

I have a question about a plugin I am currently writing (in CodeLab).

Is there a way to render text to an image? Like: I want to write "50" at x 200px and y 300px.

I have a color bar (like in a FEM-Software) and want to lable it now from top to bottom

1.0

0.75

0.5

0.25

0.0

Thanks a lot!

Gilbert.

Link to comment
Share on other sites

There is an example on this page: http://www.boltbait.com/pdn/codelab/hel ... ments.html

// Author: Ego Eram Reputo
#region UICode
FontFamily Amount1 = new FontFamily("Arial"); // Font
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
  Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt(); 
  ColorBgra PC = (ColorBgra)EnvironmentParameters.PrimaryColor;
  // Reset the destination canvas
  for (int y = rect.Top; y    {
     for (int x = rect.Left; x       {
        dst[x,y] = src[x,y];
     }
  }
  // Create a brush and graphics surface to write on
  SolidBrush Brush1=new SolidBrush(
     Color.FromArgb(PC.A,PC.R,PC.G,PC.);
  Graphics g = new RenderArgs(dst).Graphics;
  g.SmoothingMode = 
     System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  g.Clip = new Region(rect);
  // Create a font from user selection
  Font SelectedFont=new Font(Amount1.Name, 16);
  // Write our text to the canvas
  g.DrawString("Paint.NET Rocks!", SelectedFont, 
     Brush1, selection.Left, selection.Top);
}

Link to comment
Share on other sites

If spacing is critical I'd recommend hard coding the font and size, but you'd need therefore to make the font a generic one so that you don't have to worry about it not being found on the users machine.

Google "web safe fonts" for a list :wink:

The location of the text is set by the last two variables in the g.DrawString call, selection.Top and selection.Left in the above example.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...