September 14, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Managed Extensions: Using GDI+ to Draw 3D Text

  • October 25, 2004
  • By Tom Archer
  • Send Email »
  • More Articles »

Steps for Rendering Embossed and Engraved Text

I explain both of these in the same section because engraved text is simply the inverse of embossed text. The embossed-text effect is usually accomplished by simply using the shadow-text technique with the depth set at one pixel and the foreground text color set to the same color as the background on which the text is being rendered. The shadow text is then some darker color, such as grey or black. The engraved effect is done in reverse; the offset for the shadow color is one pixel up and left from the foreground text:

 

// Assumes a PictureBox on the form named picText
// with this code being the picText object's 
// Paint method

private: 
System::Void picText_Paint(
  System::Object *  sender, 
  System::Windows::Forms::PaintEventArgs *  e)
{
  // Test string
  String* textToDisplay = S"Test string";

  // Get drawing surface for PictureBox and clear background
  Graphics* g = e->Graphics;

  // Create a Font object
  System::Drawing::Font* font = new System::Drawing::Font("Times New Roman", Convert::ToSingle(25), FontStyle::Regular);

  // Obtain the size of the text to be rendered
  SizeF textSize = g->MeasureString(textToDisplay, font);

  // Text will be centered on Picture Box control
  Single x = (picText->Width - textSize.Width) / 2;
  Single y = (picText->Height - textSize.Height) / 2;

  // Clear background
  g->Clear(Color::White);
  
  // Change the isEmossed value to switch
  // between embossed and engraved
  bool isEmbossed = false;
  g->DrawString(textToDisplay, 
                   font, 
                   SystemBrushes::ControlText,
                   x + Convert::ToSingle( (isEmbossed? 1 : -1)),
                   y + Convert::ToSingle( (isEmbossed ? 1 : -1)));

  // Draw the foreground text
  g->DrawString(textToDisplay, font, new SolidBrush(Color::White), x, y);
  
}

Sample Application

I've attached a demo application that allows you to play around with the various effects you can accomplish with GDI+ and 3D text. The following image is a screen capture of that demo application:

About the Author

The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel