2015-01-22 5 views
0

내 서식있는 텍스트 내가 특정 RECT 지역과 수직 정렬 센터에서 포맷 된 텍스트를 정렬해야합니다formattedtext의 텍스트 정렬을 수직으로 만드는 방법은 무엇입니까?

var formattedtext=new FormattedText("some string", CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, this.GetTypeface(), 12.0, Brushes.Black); 

예를 에 대한 C#을 에서 서식있는 텍스트에 수직 텍스트 정렬을 설정하는 것이 가능하다.

답변

0

당신은 이런 식으로 뭔가를 시도 할 수 있습니다 :

다음
// e is the PaintEventArgs, which is passed as a parameter 

// construct a new Rectangle . 
Rectangle displayRectangle = 
    new Rectangle (new Point(40, 40), new Size (80, 80)); 

// construct new StringFormat object 
var format = new StringFormat(StringFormatFlags.DirectionVertical); 

// set the LineAlignment and Alignment properties for 
format.LineAlignment = StringAlignment.Near; 
format.Alignment = StringAlignment.Center; 

// draw the bounding rectangle and a string for the StringFormat object. 
e.Graphics.DrawRectangle(Pens.Black, displayRectangle); 
e.Graphics.DrawString("Showing Format", this.Font, 
    Brushes.Red, (RectangleF)displayRectangle, format); 
+0

하지만 난 솔루션 위의 생각 WPF.i에 DrawingContext.DrawText (formattedtext, 새로운 포인트 (40, 40))를 사용할 필요가 내 요구에 수 없습니다 (WPF) . 내가 그림을 그어서는 안되기 때문에. – SharpGobi

+0

예, 질문 또는 WPF 특정 태그를 지정하지 않았으므로 대답은 WinForm 's에 대한 것입니다. 내가 아는 한 WPF에서는이 옵션이 없습니다. 대부분 사람들은 텍스트의 각 문자 다음에 개행 문자를 추가하여 세로 문자를 표시합니다. –

0

당신이 그것을 얻을 수있는 방법이다. 내 접근법은 텍스트가 오버 플로우 한도를 초과 할 경우 경계 상자 내부에 맞게 축소됩니다.

/// <summary> 
    /// Use to draw some text using font file location. 
    /// </summary> 
    /// <param name="font">Font file location</param> 
    /// <param name="someText"></param> 
    /// <param name="fontSize"></param> 
    /// <param name="width">bitmap width</param> 
    /// <param name="height">bitmap height</param> 
    /// <returns>new instance of RenderTargetBitmap</returns> 
    private static RenderTargetBitmap DrawText(string font, string someText, int fontSize, int width = 700, int height = 300) 
    { 
     var name = Path.GetFileName(font); 
     var glyphTypeface = new GlyphTypeface(new Uri(font)); 
     string family = String.Join(" ", glyphTypeface.FamilyNames.Values.ToArray<string>()); 
     var style = glyphTypeface.Style; 
     var weight = glyphTypeface.Weight; 
     var fontStretch = glyphTypeface.Stretch; 
     string fontUri = new Uri(font.Replace(name, ""), UriKind.RelativeOrAbsolute).AbsoluteUri + "/#" + family; 
     var fontFamily = new FontFamily(fontUri); 

     var typeface = new Typeface(fontFamily, style, weight, fontStretch); 
     var formattedText = new FormattedText(someText, System.Globalization.CultureInfo.InvariantCulture, 
      FlowDirection.LeftToRight, 
      typeface, fontSize, 
      Brushes.Black); 

     formattedText.TextAlignment = TextAlignment.Center; 

     var drawingVisual = new DrawingVisual(); 

     RenderOptions.SetBitmapScalingMode(drawingVisual, BitmapScalingMode.HighQuality); 
     RenderOptions.SetEdgeMode(drawingVisual, EdgeMode.Aliased); 
     TextOptions.SetTextRenderingMode(drawingVisual, TextRenderingMode.Aliased); 

     using (var drawingContext = drawingVisual.RenderOpen()) 
     { 
      drawingContext.DrawRectangle(Brushes.White, null, new Rect(0, 0, width, height)); 

      var geometry = formattedText.BuildGeometry(new Point(0, 0)); 

      var bounds = geometry.Bounds; 

      var sW = width/bounds.Width; 
      var sH = height/bounds.Height; 
      var ratio = sH <= sW ? sH : sW; 

      if (ratio > 1) ratio = 1; 

      var translateX = (width - bounds.Width * ratio)/2; 
      var translateY = (height - bounds.Height * ratio)/2; 
      var transformGroup = new TransformGroup(); 
      transformGroup.Children.Add(new ScaleTransform(ratio, ratio)); 
      transformGroup.Children.Add(new TranslateTransform(-bounds.X * ratio + translateX, -bounds.Y * ratio + translateY)); 

      geometry.Transform = transformGroup; 

      drawingContext.DrawGeometry(Brushes.Black, null, geometry); 
     } 

     var renderTargetBitmap = new RenderTargetBitmap(
      width, height, 
      0, 0, 
      PixelFormats.Pbgra32); 

     renderTargetBitmap.Render(drawingVisual); 

     return renderTargetBitmap; 
    } 

사용법 :

 //Setting System.Windows.Control.Image source 
     image.Source = DrawText(@"C:\Windows\Fonts\ariali.ttf", "Lorem ipsum dolor sit amet,\nconsectetur adipisicing elit,\nsed do eiusmod tempor", 50);