数据点在vb.net中旋转

时间:2015-06-24 14:13:15

标签: vb.net

我有一个位置列表(x; y)显示在一个角度旋转的图片框中。

For Each pat As cPattern In mPattern
    Dim angle As Double = pat.Angle / 180 * Math.PI

    For Each pos As PointF In pat.Positions
        Dim p As Point = New Point(picPattern.Width \ 2, picPattern.Height \ 2)

        Dim G As Graphics = e.Graphics

        Dim x1 As Single = CSng(pos.X * Math.Cos(angle))
        Dim y1 As Single = CSng(pos.Y * Math.Sin(angle))

        G.DrawEllipse(New Pen(Brushes.Cyan, 2), p.X+x1, p.Y-y1, 3, 3)

    Next
Next

使用此代码我可以为每个位置绘制一个圆,但只能在水平模式下绘制。

2 个答案:

答案 0 :(得分:0)

如果要围绕PictureBox的中心旋转,请使用以下代码:

    private System.IO.Stream GetBinaryDataStream(string nameToPrint, string functionToPrint)
    {
        return new System.IO.MemoryStream(CreateImageForSignatureLine(nameToPrint, functionToPrint));
    }


    private byte[] CreateImageForSignatureLine(string nameToPrint, string functionToPrint)
    {
        using (Image img = new Bitmap(My.Resources.SignatureLineEmpty))
        {
            if (!string.IsNullOrEmpty(nameToPrint))
            {
                DrawNameOnImage(img, nameToPrint);
            }
            if (!string.IsNullOrEmpty(functionToPrint))
            {
                DrawFunctionOnImage(img, functionToPrint);
            }
            DrawFunctionOnImage(img, functionToPrint);
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                return ms.ToArray();
            }
        }
    }


    private void DrawNameOnImage(Image img, string nameToPrint)
    {
        DrawOnSignature(img, nameToPrint, 7, 80);
    }
    private void DrawFunctionOnImage(Image img, string functionToPrint)
    {
        DrawOnSignature(img, functionToPrint, 7, 96);
    }

    private void DrawOnSignature(Image img, string text, int x, int y)
    {
        using (System.Drawing.Font font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, 8))
        {
            using (Graphics drawing = Graphics.FromImage(img))
            {
                Brush textBrush = new SolidBrush(System.Drawing.Color.Black);
                drawing.DrawString(text, font, textBrush, x, y);
                drawing.Save();
            }
        }
    }

答案 1 :(得分:0)

听起来你需要将新移位和旋转的点送回你的桌子?

如果是这样,这里有一个示例显示获得点的绝对位置,居中并围绕传入位置旋转:

Imports System.Drawing.Drawing2D

Public Class Form1

    Private mPattern As New List(Of cPattern)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Small rectangle with rotation around its center:
        Dim pat As New cPattern
        pat.Angle = 45
        pat.Positions.Add(New Point(35, 35))
        pat.Positions.Add(New Point(10, 10))
        pat.Positions.Add(New Point(60, 10))
        pat.Positions.Add(New Point(60, 60))
        pat.Positions.Add(New Point(10, 60))
        mPattern.Add(pat)
    End Sub

    Private Sub picPattern_Paint(sender As Object, e As PaintEventArgs) Handles picPattern.Paint
        Dim G As Graphics = e.Graphics
        Dim center As Point = New Point(picPattern.Width / 2, picPattern.Height / 2)
        For Each pat As cPattern In mPattern
            Dim pts As List(Of Point) = pat.CenterAndRotateAt(center)
            Using cyan As New Pen(Brushes.Cyan, 2)
                For Each pos As Point In pts
                    Dim r As New Rectangle(pos.X, pos.Y, 1, 1)
                    r.Inflate(3, 3)
                    G.DrawEllipse(cyan, r)
                Next
            End Using
        Next
    End Sub

End Class

Public Class cPattern

    Public Angle As Integer
    Public Positions As New List(Of Point)

    Public Function CenterAndRotateAt(ByVal center As Point) As List(Of Point)
        Dim myMatrix As New Matrix
        ' Make the center of rotation for this pattern the new origin:
        myMatrix.Translate(-Me.Positions(0).X, -Me.Positions(0).Y, MatrixOrder.Append)
        ' Rotate the pattern to the desired angle:
        myMatrix.Rotate(Me.Angle, MatrixOrder.Append)
        ' Move the rotated pattern to the desired position:
        myMatrix.Translate(center.X, center.Y, MatrixOrder.Append)

        ' Return the absolute positions of the pattern in the new position with rotation applied:
        ' *** this will NOT change the original points in the pattern! ***
        Dim pts() As Point = Me.Positions.ToArray
        myMatrix.TransformPoints(pts)
        Return New List(Of Point)(pts)
    End Function

End Class