Wrox Programmer Forums
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 10th, 2007, 06:38 PM
Registered User
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default read bitmap pixel

Hi. I have a project that is to read pixel in a carpet image.

how to find which color what percent used ?

Old February 12th, 2007, 03:18 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts

what is a "carpet" image? in which format is stored?
you can load the image in a PictureBox control, and then use the Point method


"There are two ways to write error-free programs. Only the third one works."
Old February 13th, 2007, 07:20 AM
Registered User
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

AnalyseImage("[image path]")

Sub AnalyseImage(ByVal imgPath As String)

        Dim bm As Bitmap = Image.FromFile(imgPath)
        Dim cols As New Hashtable
        Dim item As New System.Collections.DictionaryEntry

        Dim wid As Integer = bm.Width
        Dim hgt As Integer = bm.Height
        Dim totalPixels As Integer = Math.BigMul(wid, hgt)
        Dim bFound As Boolean = False

        For x As Integer = 0 To wid - 1
            For y As Integer = 0 To hgt - 1
                Dim tempcolor As Color = bm.GetPixel(x, y)

                For Each item In cols
                    If item.Key.ToString = tempcolor.ToString Then
                        bFound = True
                        Exit For
                    End If

                If bFound Then
                    '//increment pixel count
                    cols.Item(tempcolor.ToString) = cols.Item(tempcolor.ToString) + 1
                    '//add color to collection
                    cols.Add(tempcolor.ToString, 1)
                End If

        '//we have all the data, so for each color get %
        For Each item In cols
            Dim sKey As String = item.Key.ToString
            Dim iNo As Integer = item.Value
            Dim percent As String = FormatPercent(iNo / totalPixels, 2, TriState.True).ToString
            Debug.Print("color: " & sKey & " - " & percent)

    End Sub

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to read Bitmap from Resource file ashu_from_india .NET Framework 2.0 2 July 6th, 2007 05:54 AM
pixel coordinates david.iulian Classic ASP Basics 0 March 31st, 2005 07:03 AM
How to get bitmap pixel to store in the array ck C++ Programming 1 January 20th, 2005 05:02 PM
How to get bitmap pixel to store in the array ck Visual C++ 0 December 23rd, 2004 02:52 AM
Read bitmap image? RichardRose C# 3 October 9th, 2004 11:45 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.