如何让汉字旋转90度
admin 发表于 2010-08-28 | 来源:互联网 | 阅读:

我想生成一幅图片,上面的汉字是旋转90度的,请问如何实现?

已经有31 个评论
  1. wtaxum 说:

    本来想用字体来解决,可是搜遍了也没找到…请高手帮忙呀

  2. 546914408 说:

    参考C# code

    public enum Orientation
    {
    Circle,
    Arc,
    Rotate
    }

    public enum Direction
    {
    Clockwise,
    AntiClockwise
    }

    #endregion

    public class OrientedTextLabel : System.Windows.Forms.Label
    {

    #region Variables

    private double rotationAngle;
    private string text;
    private Orientation textOrientation;
    private Direction textDirection;

    #endregion

    #region Constructor

    public OrientedTextLabel()
    {
    //Setting the initial condition.
    rotationAngle = 0d;
    textOrientation = Orientation.Rotate;
    this.Size = new Size(105,12);
    }

    #endregion

    #region Properties

    [Description("Rotation Angle"),Category("Appearance")]
    public double RotationAngle
    {
    get
    {
    return rotationAngle;
    }
    set
    {

    rotationAngle = value;
    this.Invalidate();
    }
    }

    [Description("Kind of Text Orientation"),Category("Appearance")]
    public Orientation TextOrientation
    {
    get
    {
    return textOrientation;
    }
    set
    {

    textOrientation = value;
    this.Invalidate();
    }
    }

    [Description("Direction of the Text"),Category("Appearance")]
    public Direction TextDirection
    {
    get
    {
    return textDirection;
    }
    set
    {

    textDirection = value;
    this.Invalidate();
    }
    }

    [Description("Display Text"),Category("Appearance")]
    public override string Text
    {
    get
    {
    return text;
    }
    set
    {
    text = value;
    this.Invalidate();
    }
    }

    #endregion

    #region Method

    protected override void OnPaint(PaintEventArgs e)
    {
    Graphics graphics = e.Graphics;

    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    stringFormat.Trimming = StringTrimming.None;

    Brush textBrush = new SolidBrush(this.ForeColor);

    //Getting the width and height of the text, which we are going to write
    float width = graphics.MeasureString(text,this.Font).Width;
    float height = graphics.MeasureString(text,this.Font).Height;

    //The radius is set to 0.9 of the width or height, b’cos not to
    //hide and part of the text at any stage
    float radius = 0f;
    if(ClientRectangle.Width<ClientRectangle.Height)
    {
    radius = ClientRectangle.Width *0.9f/2;
    }
    else
    {
    radius = ClientRectangle.Height *0.9f/2;
    }

    //Setting the text according to the selection
    switch(textOrientation)
    {
    case Orientation.Arc :
    {
    //Arc angle must be get from the length of the text.
    float arcAngle = (2*width/radius)/text.Length;
    if(textDirection == Direction.Clockwise)
    {
    for(int i=0;i<text.Length;i++)
    {

    graphics.TranslateTransform(
    (float)(radius*(1 – Math.Cos(arcAngle*i + rotationAngle/180 * Math.PI))),
    (float)(radius*(1 – Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
    graphics.RotateTransform((-90 + (float)rotationAngle + 180*arcAngle*i/(float)Math.PI));
    graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
    graphics.ResetTransform();
    }
    }
    else
    {
    for(int i=0;i<text.Length;i++)
    {

    graphics.TranslateTransform(
    (float)(radius*(1 – Math.Cos(arcAngle*i + rotationAngle/180*Math.PI))),
    (float)(radius*(1 + Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
    graphics.RotateTransform((-90 – (float)rotationAngle – 180*arcAngle*i/(float)Math.PI));
    graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
    graphics.ResetTransform();

    }
    }
    break;
    }
    case Orientation.Circle :
    {
    if(textDirection == Direction.Clockwise)
    {
    for(int i=0;i<text.Length;i++)
    {
    graphics.TranslateTransform(
    (float)(radius*(1 – Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
    (float)(radius*(1 – Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
    graphics.RotateTransform(-90 + (float)rotationAngle + (360/text.Length)*i);
    graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
    graphics.ResetTransform();
    }
    }
    else
    {
    for(int i=0;i<text.Length;i++)
    {
    graphics.TranslateTransform(
    (float)(radius*(1 – Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
    (float)(radius*(1 + Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
    graphics.RotateTransform(-90 – (float)rotationAngle – (360/text.Length)*i);
    graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
    graphics.ResetTransform();
    }

    }
    break;
    }
    case Orientation.Rotate :
    {
    //For rotation, who about rotation?
    double angle = (rotationAngle/180)*Math.PI;
    graphics.TranslateTransform(
    (ClientRectangle.Width+(float)(height*Math.Sin(angle))-(float)(width*Math.Cos(angle)))/2,
    (ClientRectangle.Height-(float)(height*Math.Cos(angle))-(float)(width*Math.Sin(angle)))/2);
    graphics.RotateTransform((float)rotationAngle);
    graphics.DrawString(text,this.Font,textBrush,0,0);
    graphics.ResetTransform();

    break;
    }
    }
    }
    #endregion

    }

  3. 徐咏波 说:

    Graphics g = this.CreateGraphics();  Matrix x = new Matrix();  x.Rotate(45, MatrixOrder.Append);  g.Transform = x;  g.DrawString( "字符串 ", this.Font, SystemBrushes.ControlText, 0, 0);  g.ResetTransform();图像Graphics.TranslateTransform   string filePath =@"C:\a.jpg";     using (Bitmap bm = new Bitmap(500,500))     {     using (Graphics g = Graphics.FromImage(bm))     {     g.Clear(Color.Wheat);     g.TranslateTransform(0, 0, MatrixOrder.Prepend);     g.RotateTransform(45);     FontFamily ff = new FontFamily("宋体");     Font f =new Font(ff,10);     Brush b = new SolidBrush(Color.Black);     StringFormat sf = new StringFormat();     g.DrawString("", f, b, new PointF(10, 10), sf);     g.DrawString("", f, b, new PointF(10, 10 + 30 + 10), sf);     }     bm.Save(filePath, ImageFormat.Jpeg);     }  

  4. cxjasx 说:

    例子http://www.java2s.com/Code/CSharp/2D-Graphics/TextRotate.htm

  5. bobowanzi 说:

    每天回帖即可获得10分可用分!

  6. awaywind 说:

    请问有这种字库吗?如果一个一个翻转汉字那是不是占有资源太大?c#对于这样的操作不会这么白痴吧。

  7. forestyaser 说:

    我要的效果不是一整个字符串按照一定角度翻转,而是每个汉字翻转90度,相当于qq聊天里面的@字体

  8. kook 说:

    如果我把每个汉字翻转然后拼图出来是不是很傻?

  9. liangjiaqi 说:

    围观高手(【孟子E章】) ,(人生如梦)

  10. 546914408 说:

    使用"@宋体"字体

  11. bobowanzi 说:

    @宋体是另外一个字体文件吗?我搜遍了也没找到这个…

  12. cxjasx 说:

    翻转所有汉字–截图–旋转图片

  13. waterskin 说:

    翻转所有汉字–截图–旋转图片===========我也这么想的,可是如果有"@宋体"这样的字体这样岂不是浪费电脑资源…

  14. hahamy 说:

    Graphics g = this.CreateGraphics(); Matrix x = new Matrix(); x.Rotate(45, MatrixOrder.Append); g.Transform = x; g.DrawString( "字符串 ", this.Font, SystemBrushes.ControlText, 0, 0); g.ResetTran……支持了、

  15. forestyaser 说:

    你在使用QQ的话,就可以改成这样的字体的 @宋体,具体C#没测试。

  16. 风子 说:

    我要的是实现“@宋体”这样的效果。c#如何实现呀,暂时不想:翻转所有汉字–截图–旋转图片因为如果文字太多的话…

  17. sbamd 说:

    c#在字体前面加@不能实现

  18. mikeyao210 说:

    请问你们都没有做过这样的东西吗?

  19. wtaxum 说:

    上面有很多代码实现了。画图当然要 Draw了。

  20. my159 说:

    哦,那我就一个一个汉字翻转了再往上面贴…不过还要请问到底有没有@宋体这样的一种字体?

  21. waterskin 说:

    信二钻,不会错。

  22. 红烧生鱼片 说:

    看了很多软件里面的@字体都是软件自己实现的呀…

  23. kook 说:

    看来很多软件里面的@字体都是软件自己实现的呀…

  24. aw3fae 说:

    每天回帖即可获得10分可用分!

  25. look 说:

    哦,那我就一个一个汉字翻转了再往上面贴…不过还要请问到底有没有@宋体这样的一种字体?有, 但只能针对TextBox, richTextBox进行设置 , 人工输入@ 字符, 但不会被显示出来, Winform上的字体实际上已经发生变化了.

  26. wtaxum 说:

    但愿一个一个翻转不会被别人笑话,有2钻在不会出现理论盲区。

  27. water20042 说:

    图片的旋转Bitmap bitmap=(Bitmap)BitMap.FromFile("图片路径");bitmap.RotateFlip(RotateFlipType.Rotate90FlipX); //图像逆时针旋转90度pictureBox1.Image=bitmap;你看是否有用,虽然不是字体

  28. 15375 说:

    老凯,已经结贴了,不好意思不给你分了,我总共就70分。

  29. WOOMYWOODY 说:
  30. aw3fae 说:

    正在翻,没学过c#,用到那一块需要现学…

  31. aw3fae 说:

    不错啊

我要评论

评论功能因故关闭!

请加入我们的QQ群一起参与讨论:群号59400482(500人超级群)


返回首页 | 关于我们 | 联系我们 | 广告合作 | 网站地图 | 友情链接 | 版权声明