如何在Vb net中旋转标签?

时间:2016-07-26 18:14:38

标签: vb.net

我正在尝试将标签在Vb网中旋转90度,但无法使其正常工作。我的代码如下。任何帮助将不胜感激。

    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Dim sf As New StringFormat

    sf.Alignment = StringAlignment.Center

    sf.LineAlignment = StringAlignment.Center

    MetroLabel50.Text = ""

    e.Graphics.TranslateTransform(MetroLabel50.ClientSize.Width, MetroLabel50.ClientSize.Height)

    e.Graphics.RotateTransform(90)

    e.Graphics.DrawString("Label", MetroLabel50.Font, Brushes.Black, RectangleF.op_Implicit(MetroLabel50.ClientRectangle), sf)

    e.Graphics.ResetTransform()

End Sub

2 个答案:

答案 0 :(得分:3)

所以稍后检查一下,这一行

e.Graphics.TranslateTransform(MetroLabel50.ClientSize.Width, MetroLabel50.ClientSize.Height)

应该是

e.Graphics.TranslateTransform(csng(MetroLabel50.ClientSize.Width/2), csng(MetroLabel50.ClientSize.Height/2))

你必须在中间设置

还会Handles Me.Paint

更改Handles label1.Paint

示例代码;

第1步,新项目
步骤2,在表格中间放一个标签
第3步,把那段代码

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label1.AutoSize = False
    Label1.Text = ""
    Label1.Width = 75
    Label1.Height = 75
    Label1.Refresh()
End Sub

Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint
    e.Graphics.TranslateTransform(CSng(Label1.Width / 2), CSng(Label1.Height / 2))
    e.Graphics.RotateTransform(90)
    e.Graphics.DrawString("Hello", Label1.Font, Brushes.Black, New Point(0, 0))
    e.Graphics.ResetTransform()
End Sub

步骤4,运行应用程序

答案 1 :(得分:0)

我已经搜索了很多来解决这个问题,所以我在这里分享我的简单代码 我创建了一个图片框来放置文本,您可以在填充后更改图片框大小以保持纵横比

Dim bmp As New Bitmap(TextToRotateLabel.Width, TextToRotateLabel.Height)
TextToRotateLabel.DrawToBitmap(bmp, New Rectangle(0, 0, 
TextToRotateLabel.Width, TextToRotateLabel.Height))
bmp.RotateFlip(RotateFlipType.Rotate270FlipNone)
bmp.Save("c:\Path\text.bmp")
PicBox.Image = bmp