Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27489

VS 2019 [RESOLVED] Centre of Circle gets shifted elsewhere with Text with its background colour

$
0
0
Hello

Centre of Circle gets shifted elsewhere. with Text with its background colour. I really don't understand
I would like to keep the Centre points as standard
Although incorporated below in code.I don't know how the below statements are useful.

Dim state As GraphicsState = gph.Save()
gph.TranslateTransform(ctr.X, ctr.Y) 'to get it from center

gph.Save()
gph.ResetTransform()


It is the same point where one gets the centre point, from here one is able to draw pies, fill up pies, also even respective text in respective segments

But when I tried to incorporate the background effect for the Text full co-ordinates shifted else where rather than being placed at correct positions.

Have attached Two images one with filled up pies and another without filled up pies
Code:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Color
Imports System.Drawing.SystemColors
Imports System.Drawing.Imaging


Public Class BackgroundNumbers

    Dim RedPen As New Pen(Color.Red, 2)
    Dim greenPen As New Pen(Color.Green, 2)
    Dim BluePen As New Pen(Color.Blue, 2)

    Dim Numbers16() As String = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen"}
    Dim textBGColBrushFor16() As Color = {Color.Yellow, Color.Orange, Color.SkyBlue, Color.Pink, Color.Brown, Color.LightCyan, Color.Yellow, Color.Green, Color.Red, Color.Beige, Color.Orange, Color.LightCyan, Color.Purple, Color.Pink, Color.LightCyan, Color.LightGreen}

    Dim BlueBrush As SolidBrush = New SolidBrush(Color.Blue)
    Dim OrangeBrush As SolidBrush = New SolidBrush(Color.Orange)
    Dim YellowBrush As SolidBrush = New SolidBrush(Color.Yellow)
    Dim GreenBrush As SolidBrush = New SolidBrush(Color.Green)
    Dim RedBrush As SolidBrush = New SolidBrush(Color.Red)
    Dim PinkBrush As SolidBrush = New SolidBrush(Color.Pink)
    Dim BrownBrush As SolidBrush = New SolidBrush(Color.Brown)
    Dim SkyblueBrush As SolidBrush = New SolidBrush(Color.SkyBlue) '20 52, 164
    Dim WhiteBrush As SolidBrush = New SolidBrush(Color.White) '20 52, 164

    Private bsh() As Brush = {BlueBrush, OrangeBrush, YellowBrush, GreenBrush,
        RedBrush, PinkBrush, BrownBrush, SkyblueBrush, BlueBrush, OrangeBrush, YellowBrush, GreenBrush, RedBrush, PinkBrush, BrownBrush, SkyblueBrush}

    Dim radius As Integer


    Dim OrgnlPicCenterX As Single
    Dim OrgnlPicCenterY As Single

    Dim SizeFontRotatedTxt As Integer = 15 'For segments


    Private Sub BackgroundNumbers_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.FormBorderStyle = FormBorderStyle.FixedSingle
        'FormBorderStyle.None
        Me.Text = ""
        Me.Size = New Size(1200, 1200)

        PictureBox1.Size = New Size(1000, 1000)
        PictureBox1.Location = New Size(100, 5)

        CentringImages()

    End Sub

    Private Sub CentringImages()

        Dim OrgnlPicCenterX As Single = PictureBox1.Width / 2
        Dim OrgnlPicCenterY As Single = PictureBox1.Height / 2
        Dim OrgnlPicCenterXCenter As New PointF(OrgnlPicCenterX, OrgnlPicCenterY)

        Dim Img2 As New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Dim gph As Graphics = Graphics.FromImage(Img2)

        Dim SideOfSqaure As Integer = 970

        Dim rad As Single = Math.Min(SideOfSqaure, SideOfSqaure) / 2  '- 20
        radius = rad

        GetNumbersBG(gph, New Point(OrgnlPicCenterX, OrgnlPicCenterY), 16)

        PictureBox1.Image = Img2

    End Sub

    Private Sub GetNumbersBG(ByVal gph As Graphics, ByVal ctr As PointF, ByVal segnum As Integer)

        Dim state As GraphicsState = gph.Save()
        gph.TranslateTransform(ctr.X, ctr.Y) 'to get it from center

        Dim rim As New Rectangle(-radius, -radius, radius * 2, radius * 2)
        Dim sweepAngle As Single = CSng(360 / segnum)
        Dim ch As Integer = 0
        Dim br As Integer = 0
        Dim clrInt As Integer = 0
        Dim gap As Integer = 15

        For ang As Single = -90 - (sweepAngle / 2) To 360 - 90 - sweepAngle Step sweepAngle
            'gph.FillPie(bsh(clrInt), rim, ang, sweepAngle)
            gph.DrawPie(New Pen(Color.Red, 1), rim, ang, sweepAngle)

            Dim innerXfont As Single = ctr.X + CSng(Math.Cos(Math.PI / 180 * (ang + sweepAngle / 2)) * (radius * 0.85F))
            Dim innerYfont As Single = ctr.X + CSng(Math.Sin(Math.PI / 180 * (ang + sweepAngle / 2)) * (radius * 0.85F))

            DrawRotatedTextAsPerSegmentsBG12F(gph, Numbers16(ch), (ch - 4) * sweepAngle, ctr.X, ctr.Y, FontStyle.Bold, textBGColBrushFor16(ch), radius)

            br += 1
            If br > 1 Then br = 0

            ch += 1
            clrInt += 1


        Next
        gph.Save()
        gph.ResetTransform()

    End Sub



    Private Sub DrawRotatedTextAsPerSegmentsBG12F(gph As Graphics, txt As String, angle As Single, cx As Double, cy As Double, fntSyle As FontStyle, bgColor As Color, radius As Integer)
        Dim string_format As New StringFormat With {
        .Alignment = StringAlignment.Center,
        .LineAlignment = StringAlignment.Center}

        Dim nFont As New Font("Calibri", 14, fntSyle)
        Dim sweepAngle As Single = 360 / Numbers16.Length ' 16 segments

        Dim innerRadius As Single = radius - 55
        Dim outerRadius As Single = radius - 30

        Dim path As New GraphicsPath()
        Dim startAngle As Single = angle - (sweepAngle / 2) ' Start at correct position
        Dim outerRect As New RectangleF(cx - outerRadius, cy - outerRadius, outerRadius * 2, outerRadius * 2)
        Dim innerRect As New RectangleF(cx - innerRadius, cy - innerRadius, innerRadius * 2, innerRadius * 2)

        path.AddArc(outerRect, startAngle, sweepAngle)
        path.AddArc(innerRect, startAngle + sweepAngle, -sweepAngle)
        path.CloseFigure()

        Using bgBrush As New SolidBrush(bgColor)
            gph.FillPath(bgBrush, path)
        End Using

        Dim textRadius As Single = (innerRadius + outerRadius) / 2
        Dim textX As Single = cx + textRadius * Math.Cos(Math.PI / 180 * (angle))
        Dim textY As Single = cy + textRadius * Math.Sin(Math.PI / 180 * (angle))

        Dim originalState As GraphicsState = gph.Save()
        gph.TranslateTransform(textX, textY)
        gph.RotateTransform(angle + 90) ' Rotate text correctly

        gph.DrawString(txt, nFont, Brushes.DarkBlue, New PointF(0, 0), string_format)

        gph.Restore(originalState)
    End Sub

End Class

Will really appreciate your inputs
Thanks
nkvb
Attached Images
  

Viewing all articles
Browse latest Browse all 27489

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>