how can i draw a free-shape (it means no particular shape or sides) and fill it in with the colors chosen from colordialog?

e.g. im gonna draw a rounded rectangle and i want to fill its insides with color red.

answers will be appreciated :)

Dani AI

Generated

wanted a freehand “trace the lips and fill” tool and pointed to GDI+ references. The idea below is a compact, beginner-friendly pattern you can drop into a WinForms app: collect mouse points, build a GraphicsPath, CloseFigure(), then FillPath onto an overlay Bitmap. Draw the overlay in PictureBox.Paint instead of using CreateGraphics so the fill persists through repaints.

' form-level
Private pts As New List(Of Point)()
Private path As New Drawing2D.GraphicsPath()
Private overlay As Bitmap
Private selColor As Color = Color.Red
Private drawing As Boolean = False

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If PictureBox1.Image Is Nothing Then PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    overlay = New Bitmap(PictureBox1.Image.Width, PictureBox1.Image.Height, Imaging.PixelFormat.Format32bppArgb)
End Sub

Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    pts.Clear() : pts.Add(e.Location) : drawing = True
End Sub

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    If Not drawing Then Return
    Dim p As Point = e.Location
    If pts.Count = 0 OrElse p <> pts(pts.Count - 1) Then
        pts.Add(p)
        path.Reset()
        If pts.Count > 1 Then path.AddLines(pts.ToArray())
        PictureBox1.Invalidate()
    End If
End Sub

Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
    drawing = False
    If pts.Count < 3 Then Return
    path.CloseFigure()
    Using g As Graphics = Graphics.FromImage(overlay)
        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        g.FillPath(New SolidBrush(selColor), path)
    End Using
    path.Reset() : pts.Clear() : PictureBox1.Invalidate()
End Sub

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    e.Graphics.DrawImage(overlay, Point.Empty)
    If drawing AndAlso path.PointCount > 0 Then
        Using sb As New SolidBrush(Color.FromArgb(120, selColor))
            e.Graphics.FillPath(sb, path) ' live preview
        End Using
    End If
End Sub

Private Sub btnPickColor_Click(sender As Object, e As EventArgs) Handles btnPickColor.Click
    If ColorDialog1.ShowDialog() = DialogResult.OK Then selColor = ColorDialog1.Color
End Sub

Notes and quick tips: always call CloseFigure before filling; use SmoothingMode.AntiAlias for nicer edges; use a semi-transparent preview while drawing; dispose Graphics/Brushes as shown; if your PictureBox uses Zoom/Stretch convert mouse coords to image coords before adding points; reduce point density (add only when distance > a threshold) for performance; save the overlay bitmap (PNG) to persist edits. This pattern is easy to extend (blur edges, layered fills, opacity sliders) and avoids the common “drawing disappears on repaint” trap.

Recommended Answers

All 11 Replies

You need to check the following links.

link1
link2
link3

your links are somewhat hard to understand for a beginner like me, thanks anyway :)

im looking for a code that can be easily understood and really does the task.

You need to start with drawing a circle.

Gradually change its different parameters to create oval shapes and proceed further.

thanks for your suggestion, but im really running out of time to study the whole thing, do you have a custom-made codes which can do it? thanks

Could not you find any in the provided links ?

i believe there are enough code there.

and all of them are custom made.

yes there are codes there, but it doesnt fit the work i want.

let me explain it, i am making a hairstyle and makeup software, and the region to be filled is for the lipstick feature of the software wherein the user will trace the sides of the lips of the image and the fill will be the color of the lipstick using a colordialog. the links provided custom-made shapes like triangle , ellipse, etc. but what im looking for is that the user will actually draw the lips on the picturebox.

what about reading this or this.

thanks for your posts but it doesnt get the point that i want, thanks for your efforts :)

After all the links I have provided, just want to know what you have codes so far.

But where is the code that you are working on, not Google search provided code.

Have you created anything of your own ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.