Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9015789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:50:15+00:00 2026-06-16T03:50:15+00:00

I am using the below code to extract RGB values from images, sometimes this

  • 0

I am using the below code to extract RGB values from images, sometimes this works, however on certain files (seemingly where the Stride is not divisible by the width of the bitmap) it is returning mixed up values:

Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format24bppRgb)
Dim ptr As IntPtr = bmpData.Scan0
Dim cols As New List(Of Color)
Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
Dim rgbValues(bytes - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)

' Retrieve RGB values
For i = modByte To rgbValues.Length Step 3
     cols.Add(Color.FromArgb(rgbValues(i + 2), rgbValues(i + 1), rgbValues(i)))
Next

bmp.UnlockBits(bmpData)
bmp.Dispose()
Dim colsCnt As List(Of RgbPixels) = cols.GroupBy(Function(g) New With {Key .R = g.R, Key .G = g.G, Key .B = g.B}).Select(Function(s) New RgbPixels With {.Colour = Color.FromArgb(s.Key.R, s.Key.G, s.Key.B), .Amount = s.Count()}).ToList()

After grouping the resulting colours, the values are something like:

R    G    B
255  255  255
255  255  0
255  0    0
0    0    255
0    255  255

Or some variation of that, when they should just be:

R    G    B
255  255  255
0    0    0

Please point me in the right direction, BTW my source bmp is in PixelFormat.Format24bppRgb too, so I don’t believe that is the problem. Also if you can only answer in C# that is not a problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:50:16+00:00Added an answer on June 16, 2026 at 3:50 am

    The problem is that you’re not considering the stride value. Stride is always padded so that the width of the byte-array per image row is dividable by 4. This is an optimization related to memory copy and how the CPU works, that goes decades back and still is useful.

    F.ex, if one image has a width of 13 pixels, the stride would be like this (simplified to one component):

    =============    (width 13 pixels = 13 bytes when using RGB)
    ================ (stride would be 16)
    

    for an image of 14 pixels it would look like this:

    ==============   (width 14 pixels = 14 bytes when using RGB)
    ================ (stride would still be 16)
    

    So in your code you need to handle a stride row instead of a byte array, unless you are using fixed and defined widths of the images.

    I modified your code so it skips rows by stride:

    Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
    Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format24bppRgb)
    Dim ptr As IntPtr = bmpData.Scan0
    Dim cols As New List(Of Color)
    Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
    Dim rgbValues(bytes - 1) As Byte
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
    
    Dim x, y, dx, l as Integer
    
    For y = 0 To rect.Height - 1
    
        l = y * bmpData.Stride 'calulate line based on stride
    
        For x = 0 To rect.Width - 1
    
            dx = l + x * 3  '3 for RGB, 4 for ARGB, notice l is used as offset
    
            cols.Add(Color.FromArgb(rgbValues(dx + 2), _
                                    rgbValues(dx + 1), _
                                    rgbValues(dx)))
        Next
    Next
    
    ' Retrieve RGB values
    'For i = modByte To rgbValues.Length Step 3
    '     cols.Add(Color.FromArgb(rgbValues(i + 2), rgbValues(i + 1), rgbValues(i)))
    'Next
    
    bmp.UnlockBits(bmpData)
    bmp.Dispose()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to extract some files from a jar-file downloaded using java-webstart. below code
I am using the below code to extract meta 'generator' tag content from a
I'm using the code below to extract data from a gridview and populate it
I'm using the javascript code below trying to extract the number after gallery-entry_ in
I am using below code to generate photo gallery from a folder. How can
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?
I wanted to extract Url of image from html code, e.g. html code below:
I am using below code to upload excel and INSERT in mysql in php
I am using below code to get profile image of friends using Resfb. I
I'm using below code to check some form fields and render datatable table on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.