2010-01-05 10 views
2

.Net의 System.Drawing.Graphics.DrawString() 메소드를 사용하여 이미지 위에 텍스트를 그려서 새 파일에 저장합니다..Net의 이미지에 스트로크로 텍스트를 그립니다.

// define image file paths 
string sourceImagePath = HttpContext.Current.Server.MapPath("~/img/sourceImage.jpg"); 
string destinationImagePath = HttpContext.Current.Server.MapPath("~/img/") + "finalImage.jpg"; 

// create new graphic to draw to 
Bitmap bm = new Bitmap(200, 200); 
Graphics gr = Graphics.FromImage(bm); 

// open and draw image into new graphic 
System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(sourceImagePath, true); 
gr.DrawImage(sourceImage, 0, 0); 
sourceImage.Dispose(); 

// write "my text" on center of image 
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
StringFormat sf = new StringFormat(); 
sf.Alignment = StringAlignment.Center; 

PrivateFontCollection fontCollection = new PrivateFontCollection(); 
fontCollection.AddFontFile(HttpContext.Current.Server.MapPath("~/fonts/HelveticaNeueLTStd-BlkCn.ttf")); 
// prototype (1) 
gr.DrawString("my text", new Font(fontCollection.Families[0], 14, FontStyle.Bold), new SolidBrush(Color.FromArgb(255, 255, 255)), 100, 0, sf); 
fontCollection.Dispose(); 

// save new image 
if (File.Exists(destinationImagePath)) 
{ 
    File.Delete(destinationImagePath); 
} 
bm.Save(destinationImagePath); 
bm.Dispose(); 
gr.Dispose(); 

이미지에 텍스트를 추가하는 방법은 텍스트를 선을 추가하는 방법을 제공하지 않습니다. 예를 들어, 실제로해야 할 일은 이미지에 쓰여진 텍스트에 특정 색상의 2px 스트로크를 추가하는 것입니다.

.Net Framework < = v3.5로 어떻게 수행 할 수 있습니까?

답변

5

모든 텍스트는 strokes입니다. 뇌졸중에 의해 당신이 윤곽을 의미하는 경우

는, 당신은 GraphicsPath을 만들고 AddString를 사용하여 문자열을 추가, 당신은 다음 MSDN의 예에서 사용 된 DrawPath보다는 FillPath를 사용하여 경로를 간략하게 설명 할 수 있습니다. 당신이 strike을 의미 뇌졸중에 의한 경우

private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     // Create a GraphicsPath object. 
     GraphicsPath myPath = new GraphicsPath(); 

     // Set up all the string parameters. 
     string stringText = "Sample Text"; 
     FontFamily family = new FontFamily("Arial"); 
     int fontStyle = (int)FontStyle.Italic; 
     int emSize = 96; 
     Point origin = new Point(20, 20); 
     StringFormat format = StringFormat.GenericDefault; 

     // Add the string to the path. 
     myPath.AddString(stringText, 
      family, 
      fontStyle, 
      emSize, 
      origin, 
      format); 

     //Draw the path to the screen. 
     e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 
     e.Graphics.FillPath(Brushes.BlanchedAlmond, myPath); 
     e.Graphics.DrawPath(new Pen(Brushes.Azure, 2), myPath); 
    } 

는 다음 Strikeout 글꼴 스타일을 사용합니다.

뇌졸중이 serif 인 경우 다른 글꼴을 사용하십시오.

+0

매력처럼 작동합니다! 내가 선택한 한 가지는 먼저 DrawPath 호출을 수행 한 다음 FillPath를 수행하는 것입니다. 이렇게하면 드로우 된 패스가 실제 채워진 패스를 덮지 않고 더 많은 획을 긋습니다. 감사합니다. – Rafe

0

정확히 찾고자하는 것이 맞는지 모르지만 코드에 SolidBrush 대신 LinearGradientBrush을 사용해보세요.

이 질문에서 "획"이 무엇인지 확실하지 않지만 행 7과 8의 모든 픽셀을 빨간색으로 표시하려면 SolidBrush 대신 System.Drawing.TextureBrush을 사용할 수 있습니다.너비가 1이고 너비가 충분히 크고 (빨강이 될 행 7과 8을 제외한 모든 검정색) 글꼴을 만든 다음이 Bitmap을 매개 변수로 사용하는 TextureBrush의 생성자를 사용합니다.

0

해킹 비트이지만 다른 레이어의 상단에 두 세트의 텍스트를 겹쳐서 표시 할 수 있습니다. 하단 레이어는 약간만 확장됩니다. 모든 글꼴에 대해 완벽하게 작동하지 않을 수도 있지만 시도해 볼 가치가 있습니다.

관련 문제