zoukankan      html  css  js  c++  java
  • How to: Rotating text 90 degrees in GDI+

    转载:http://www.c-sharpcorner.com/Blogs/580/

    One of the issues I ran into when using GDI+ is how to rotate a string and place it on the Y Axis of a Graph. Here is how I solved it.


    You can use the StringFormatFlags.DirectionVertical in the DrawString command to rotate text.  Unfortunately, when drawing the graph it draws the text mirror-imaged the wrong way.  So much for StringFormat.

    In order to rotate text 90 degrees,  you'll need to draw the text to a separate bitmap in memory and then draw the resulting image to the screen.

    In your paint event handler code add the following:

    SizeF labelSize = g.MeasureString(myLabel, GraphFont);
    Bitmap stringmap = new Bitmap((int)labelSize.Height + 1, (int)labelSize.Width + 1);
    Graphics gbitmap = Graphics.FromImage(stringmap);
    gbitmap.SmoothingMode = SmoothingMode.AntiAlias;
    // g.DrawString(m_LabelY, GraphFont, Brushes.Blue, new PointF(ClientRectangle.Left , ClientRectangle.Top + 100), theFormat);
    gbitmap.TranslateTransform(0, labelSize.Width);
    gbitmap.RotateTransform(-90);
    gbitmap.DrawString(myLabel, GraphFont, Brushes.Blue, new PointF(0 , 0), new StringFormat());
    g.DrawImage(stringmap, (float)ClientRectangle.Left, (float)ClientRectangle.Top + 100);
    //And don't forget to dispose of your bitmap and graphics objects at the end of onpaint
    gbitmap.Dispose();
    stringmap.Dispose();
    

      

    方法二:不过这个方法绘制出来的字符串是从上到下,方向固定的
    // Create string to draw.
    String drawString = "Sample Text";
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    // Create point for upper-left corner of drawing.
    float x = 150.0F;float y = 50.0F;
    // Set format of string.
    StringFormat drawFormat = new StringFormat();
    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
    // Draw string to screen.            
    e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
                
  • 相关阅读:
    CF1539 VP 记录
    CF1529 VP 记录
    CF875C National Property 题解
    CF1545 比赛记录
    CF 1550 比赛记录
    CF1539E Game with Cards 题解
    CF1202F You Are Given Some Letters... 题解
    vmware Linux虚拟机挂载共享文件夹
    利用SOLR搭建企业搜索平台 之九(solr的查询语法)
    利用SOLR搭建企业搜索平台 之四(MultiCore)
  • 原文地址:https://www.cnblogs.com/YYi_H/p/2121219.html
Copyright © 2011-2022 走看看