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);
                
  • 相关阅读:
    JQuery之在线引用
    SpringBoot之durid连接池配置
    VueJs之事件处理器
    VueJs之样式绑定
    VueJs之判断与循环监听
    PTA 7-8 暴力小学(二年级篇)-求出4个数字 (10分)
    PTA 7-7 交替字符倒三角形 (10分)
    PTA 7-5 阶乘和 (10分)
    PTA 7-4 哥德巴赫猜想 (10分)
    PTA 7-3 可逆素数 (15分)
  • 原文地址:https://www.cnblogs.com/YYi_H/p/2121219.html
Copyright © 2011-2022 走看看