Palette in a picturebox image

Megalith

Well-known member
Joined
Aug 21, 2006
Messages
66
Programming Experience
10+
Can somebody help me with this problem, I have a picturebox on my form which is loaded with an image using an openfile dialog control and scaled to fit the picurebox. What i am trying to do is obtain the colors used in the image on the picturebox, my initial thoughts on this was to create a collection and add to it new colors on a pixel by pixel method as they are found and counting the number of occurances of each color, then i found the palette method and it occured to me i could extract this palette from the picturebox and that it should be already sorted by frequency. this is what i wrote thinking this is an easy solution.

VB.NET:
Dim p() As Color = PictureBox1.Image.Palette.Entries
        Dim NumColors As Integer = p.Length

this however didnt work throwing an exception.

is there a way to do this? is the colors in the palette in order of useage?
 
You seen to be on the right track. It would have helped if you could have shown what the exception was. However you are correct that the colorpalette.entries is an array of color structures that make up the image. Like i say i'd need to see what the exception was without trying it out myself.
 
Sorry for not including the exception in my post, its an exception error :-

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

This is the rest of the code in the button click method, without the 2 lines in the last post post it successfully loads the image chosen into the picturebox

VB.NET:
    Private Sub UseImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UseImage.Click
        Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "Bitmap Picture (*.bmp)|*.bmp|Portable net graphics (*.png)|*.png|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True
        If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PictureBox1.ImageLocation = openFileDialog1.FileName
        End If
        ' we have know loaded our image into the picture box and stretched it to fit 
        ' we need to put the colours into an array from the picturebox
        Dim p() As Color = PictureBox1.Image.Palette.Entries
        Dim NumColors As Integer = p.Length

    End Sub

also is there a way to have all image types as an option in the filedialog control?
 
It just looks to be that you are trying to get the entries beofre there is any to get. If you were to put those 2 lines of code into a click event or something i'm pretty sure it would work. Have you tried an application.doevents before them. Also i'm not sure of a way to have AllImageTypes as an option but here's a filter for the most common ones...

VB.NET:
 openFileDialog1.Filter = "Bitmap Files|*.bmp" +
                "|Enhanced Windows MetaFile|*.emf" + 
                "|Exchangeable Image File|*.exif" +
                "|Gif Files|*.gif|Icons|*.ico|JPEG Files|*.jpg" + 
                "|PNG Files|*.png|TIFF Files|*.tif|Windows MetaFile|*.wmf"
 
The inclusion of My.Application.DoEvents did the trick and the code created the palette as desired, however it isnt what i need for my application as it contains colors not used in the image (i got 10 almost identical bright green colors in the zapotec.bmp file from the windows directory, none of which are in the actual image lol) and the colors arent in order of frequency(i could have just picked say the top 8 colors if it was the case), so it looks like i will have to go back to my initial idea of scanning each pixel and summing the colors as they occur :( shame tho it appears to be a very simple method to solve my problem. maybe there is another method in some dark dank corner of the VB code :) getpixel is not slick in any sense of the word but if its the only way then it will have to do.
 
Back
Top