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. .NET (Core and Framework)
  4. coversion of image into pixel matrix [modified]

coversion of image into pixel matrix [modified]

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpquestion
6 Posts 4 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.
  • R Offline
    R Offline
    rohan4040
    wrote on last edited by
    #1

    How can i convert a color image into pixel matrix using c#.net

    modified on Wednesday, March 4, 2009 1:44 AM

    P A 2 Replies Last reply
    0
    • R rohan4040

      How can i convert a color image into pixel matrix using c#.net

      modified on Wednesday, March 4, 2009 1:44 AM

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      rohan4040 wrote:

      How can i convert a color image into pixel matrix

      By writing code - in the language of your choice.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      My blog | My articles | MoXAML PowerToys

      L 1 Reply Last reply
      0
      • R rohan4040

        How can i convert a color image into pixel matrix using c#.net

        modified on Wednesday, March 4, 2009 1:44 AM

        A Offline
        A Offline
        annathor
        wrote on last edited by
        #3

        a slow but easy way to do this is to use the Bitmap class' GetPixel method. I am writing this code as I go and will probably contains error:

        class ByteColor
        {
        public byte R;
        public byte G;
        public byte B;
        }

        Bitmap myBMP=new Bitmap("myimage.jpg");
        int iW=myBMP.Width;
        int iH=myBMP.Height;
        ByteColor[,] myPixelMatrix=new ByteColor[iW,iH];

        for(int w=0; w<iw;w++)>
        {
        for(int h=0; h<ih;h++)>
        {
        Color tmpColor=myBMP.GetPixel(w,h);

         byte cR=Convert.toByte(tmpColor.R);
         byte cG=Convert.toByte(tmpColor.G);
         byte cB=Convert.toByte(tmpColor.B);
        
         myPixelMatrix\[w,h\].R=cR;
         myPixelMatrix\[w,h\].G=cR;
         myPixelMatrix\[w,h\].B=cR;
        

        }
        }

        As I said this is a easy but slow way to do it. for some more speed you can use the LockBits and UnlockBits methodes and copy the byte array right from memory like this:

        Rectangle rect = new Rectangle(0, 0, myBMP.Width, myBMP.Height);
        BitmapData ImageColorData = myBMP.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

        unsafe
        {
        IntPtr pointer = ImageColorData .Scan0;
        int Bytesnmpr = ImageColorData .Stride * myBMP.Height;
        byte[] ColorValues = new byte[Bytesnmpr];
        Marshal.Copy(pointer, ColorValues , 0, Bytesnmpr);

        for (int i= 0; i< ColorValues .Length; i++) 
        {
            //copy the byte to your buffer or do what you want with it here
        }
        
        Marshal.Copy(ColorValues , 0, pointer , Bytesnmpr);
        

        }
        myBMP.UnlockBits(ImageColorData);

        this one is much faster but uses unsafe code. also remember, I worte the code on the fly, so there is most likely errors in this code

        R 1 Reply Last reply
        0
        • P Pete OHanlon

          rohan4040 wrote:

          How can i convert a color image into pixel matrix

          By writing code - in the language of your choice.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          My blog | My articles | MoXAML PowerToys

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          If you had magnified between the lines of his post like times 100 you would have seen: "Send me the codes pleezzzzz, it's urgent"

          P 1 Reply Last reply
          0
          • L led mike

            If you had magnified between the lines of his post like times 100 you would have seen: "Send me the codes pleezzzzz, it's urgent"

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            I urgently ignored the subtext.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys

            1 Reply Last reply
            0
            • A annathor

              a slow but easy way to do this is to use the Bitmap class' GetPixel method. I am writing this code as I go and will probably contains error:

              class ByteColor
              {
              public byte R;
              public byte G;
              public byte B;
              }

              Bitmap myBMP=new Bitmap("myimage.jpg");
              int iW=myBMP.Width;
              int iH=myBMP.Height;
              ByteColor[,] myPixelMatrix=new ByteColor[iW,iH];

              for(int w=0; w<iw;w++)>
              {
              for(int h=0; h<ih;h++)>
              {
              Color tmpColor=myBMP.GetPixel(w,h);

               byte cR=Convert.toByte(tmpColor.R);
               byte cG=Convert.toByte(tmpColor.G);
               byte cB=Convert.toByte(tmpColor.B);
              
               myPixelMatrix\[w,h\].R=cR;
               myPixelMatrix\[w,h\].G=cR;
               myPixelMatrix\[w,h\].B=cR;
              

              }
              }

              As I said this is a easy but slow way to do it. for some more speed you can use the LockBits and UnlockBits methodes and copy the byte array right from memory like this:

              Rectangle rect = new Rectangle(0, 0, myBMP.Width, myBMP.Height);
              BitmapData ImageColorData = myBMP.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

              unsafe
              {
              IntPtr pointer = ImageColorData .Scan0;
              int Bytesnmpr = ImageColorData .Stride * myBMP.Height;
              byte[] ColorValues = new byte[Bytesnmpr];
              Marshal.Copy(pointer, ColorValues , 0, Bytesnmpr);

              for (int i= 0; i< ColorValues .Length; i++) 
              {
                  //copy the byte to your buffer or do what you want with it here
              }
              
              Marshal.Copy(ColorValues , 0, pointer , Bytesnmpr);
              

              }
              myBMP.UnlockBits(ImageColorData);

              this one is much faster but uses unsafe code. also remember, I worte the code on the fly, so there is most likely errors in this code

              R Offline
              R Offline
              rohan4040
              wrote on last edited by
              #6

              is this coding related to c#.net or any other form of coding?

              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