title: How to stroke text when drawing it with DrawString function in GDI+?
body: <p>There's a GDI's <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd145124(v=vs.85).aspx" rel="nofollow noreferrer">StrokePath</a> API that can allow to simulate "stroking" of text using <a href="http://ntcoder.com/bab/2008/03/21/drawing-outlined-text/" rel="nofollow noreferrer">this method</a>. I'll copy it here:</p>
  
  <pre><code>void CCanvas::DrawOutlineText( CDC&amp; dc, const CString&amp; Text )
  {
    const int RestorePoint = dc.SaveDC();
  
    // Create new font
    CFont NewFont;
    NewFont.CreatePointFont( 700, TEXT( "Verdana" ), &amp;dc );
  
    // Use this font
    dc.SelectObject( &amp;NewFont );
  
    // Brush for pen
    LOGBRUSH lBrushForPen = { 0 };
    lBrushForPen.lbColor = RGB( 200, 150, 100 );
    lBrushForPen.lbHatch = HS_CROSS;
    lBrushForPen.lbStyle = BS_SOLID;
  
    // New pen for drawing outline text
    CPen OutlinePen;
    OutlinePen.CreatePen( PS_GEOMETRIC | PS_SOLID, 2, &amp;lBrushForPen, 0, 0 );
  
    // Use this pen
    dc.SelectObject( &amp;OutlinePen );
  
    dc.SetBkMode( TRANSPARENT );
  
    dc.BeginPath();
    // This text is not drawn on screen, but instead each action is being
    // recorded and stored internally as a path, since we called BeginPath
    dc.TextOut( 20, 20, Text );
    // Stop path
    dc.EndPath();
  
    // Now draw outline text
    dc.StrokePath();
  
    dc.RestoreDC( RestorePoint );
  }
  </code></pre>
  
  <p>In my case I'm using <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms535991(v=vs.85).aspx" rel="nofollow noreferrer">DrawString</a> function from GDI+ to draw text.</p>
  
  <p>Does anyone know if there's an alternative to <code>BeginPath</code>, <code>EndPath</code> and <code>StrokePath</code> in GDI+ to simulate text stroking?</p>
  
  <p><strong>EDIT:</strong> <em>Following the advice by <code>jschroedl</code> below, I tried the following:</em></p>
  
  <pre><code>CString str = L"Text";
  
  Graphics grpx(dc.GetSafeHdc());
  
  SolidBrush gdiBrush(Color(0xFF, 0xFF, 0, 0));
  
  StringFormat gdiSF;
  gdiSF.SetAlignment(StringAlignmentNear);
  gdiSF.SetFormatFlags(StringFormatFlagsNoWrap | StringFormatFlagsNoFitBlackBox | 
      StringFormatFlagsNoFontFallback | StringFormatFlagsNoClip);
  gdiSF.SetHotkeyPrefix(HotkeyPrefixNone);
  gdiSF.SetTrimming(StringTrimmingNone);
  
  grpx.SetTextRenderingHint(TextRenderingHintAntiAlias);
  grpx.SetPixelOffsetMode(PixelOffsetModeNone);
  grpx.SetInterpolationMode(InterpolationModeHighQualityBicubic);
  
  GraphicsPath dd;
  FontFamily gdiFF(L"Segoe UI");
  const PointF pntF(0, 0);
  dd.AddString(str, str.GetLength(), &amp;gdiFF, FontStyleRegular, 58.0f, pntF, &amp;gdiSF);
  
  Pen penFF(Color(0xFF, 0xFF, 0, 0), 1.0f);
  grpx.DrawPath(&amp;penFF, &amp;dd);
  </code></pre>
  
  <p><em>that produced quite a jagged outline (enlarged screenshot):</em></p>
  
  <p><a href="https://i.stack.imgur.com/ljYqt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ljYqt.png" alt="enter image description here"></a></p>
  
  <p>Any idea how to make it render with anti-aliasing?</p>