Hi

I recently learnt how to do crash collision based on colours. So you can use something like img.getpixel(0,0) to retrieve the colour of the pixel at (0,0) on the picture "img." I have experimented quite a lot with it and have managed to make some programs that draw a rectangular box the same colour as the pixel on an image I am highlighting.

How do you do the same to a polygon or a rectangle. I can't seem to be able to use the getpixel command with any sort of shape, though for the purposes of my program I would like to. Is there any way for this or do I have to use an image for colour detection?

Thanks

Recommended Answers

All 4 Replies

Hi,

Here's an example how to create an Ellipse and show the same color as the pixel color.

Public Class Form1

    Public MyColor As Color
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
        PictureBox1.Image = My.Resources. your picturename
        Dim TempBitmap As New Bitmap(PictureBox1.Image)
        MyColor = TempBitmap.GetPixel(e.X, e.Y)
        Me.Invalidate()
        End Sub
    
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        Dim g As Graphics = e.Graphics
        Dim brs As SolidBrush
        brs = New SolidBrush(MyColor)
        g.FillEllipse(brs, 20, 20, 50, 50)
        
    End Sub
End Class

Hi

Sorry, I was probably a bit ambiguous, but thanks for posting anyway. What I meant was that I could get a colour from another picture and draw a shape based on this colour. What I want to be able to do is get a colour from a SHAPE (which is filled with a colour) and use this for crash collision. I know what to do except for how to get a colour from a shape.

getpixel only seems to get colours from a picture, not a shape unfortunatly.

Thanks

Hi,

Here's a way how to detect the color in a shape.
Add a label to show the color.

Public Class Form1
    Public path As New System.Drawing.Drawing2D.GraphicsPath
    Public thecolor As Color = Color.Aqua
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        path.AddEllipse(20, 160, 50, 50)

    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If path.IsVisible(e.X, e.Y) Then
            Label1.BackColor = thecolor
        End If
    End Sub
    
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim brl As SolidBrush
        brl = New SolidBrush(thecolor)
        e.Graphics.FillPath(brl, path)
                
    End Sub
End Class

Hi

Thanks for the assistance!

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.