Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get a RGB values from an image buffer?

How to get a RGB values from an image buffer?

Scheduled Pinned Locked Moved C#
questioncsharpperformancetutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CopperCircle
    wrote on last edited by
    #1

    Hi, I am new to C# and im working trying some image processing, im using this bit of code to invert the pixels on an image grabbed from a video file, how do I extract the RGB values from a pixel in C# using the image buffer? Speed is of the essence... Thanks in advance.

    unsafe
    {
    byte* p = (byte *)(void *)pBuffer;
    int nWidth = Width * 3;
    for(int y = 0; y < Height; y++)
    {
    for(int x = 0; x < nWidth; x++ )
    {
    p[0] = (byte)(255-p[0]);
    p++;

    L G 2 Replies Last reply
    0
    • C CopperCircle

      Hi, I am new to C# and im working trying some image processing, im using this bit of code to invert the pixels on an image grabbed from a video file, how do I extract the RGB values from a pixel in C# using the image buffer? Speed is of the essence... Thanks in advance.

      unsafe
      {
      byte* p = (byte *)(void *)pBuffer;
      int nWidth = Width * 3;
      for(int y = 0; y < Height; y++)
      {
      for(int x = 0; x < nWidth; x++ )
      {
      p[0] = (byte)(255-p[0]);
      p++;

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See this article[^], there are some examples how to extract and modify the RGB values of an image. regards

      1 Reply Last reply
      0
      • C CopperCircle

        Hi, I am new to C# and im working trying some image processing, im using this bit of code to invert the pixels on an image grabbed from a video file, how do I extract the RGB values from a pixel in C# using the image buffer? Speed is of the essence... Thanks in advance.

        unsafe
        {
        byte* p = (byte *)(void *)pBuffer;
        int nWidth = Width * 3;
        for(int y = 0; y < Height; y++)
        {
        for(int x = 0; x < nWidth; x++ )
        {
        p[0] = (byte)(255-p[0]);
        p++;

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        This is how you get the color components of a pixel, specified by the x and y coordinates:

        byte* p = (byte *)(void *)pBuffer;
        p += (x + y * Width) * 3;
        byte r = *(p++);
        byte g = *(p++);
        byte b = *p;

        If you increase the pointer for the last component also, it will point to the next pixel. This is very useful if you access the pixels in sequence, as you don't have to calculate a new address for each pixel.

        Despite everything, the person most likely to be fooling you next is yourself.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups