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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Bitmaps and Unsafe Code

Bitmaps and Unsafe Code

Scheduled Pinned Locked Moved C#
graphicshelpquestion
8 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.
  • S Offline
    S Offline
    stewwi
    wrote on last edited by
    #1

    Hi all i have a small problem I am creating a new blank bitmap in a program, then using the lock bitmap i edit pixels. the Purpose iss to build a spectograph. Now my problem is that when i create the bitmap Bitmap pane = new Bitmap(800, 800); then edit it using pointers the image remains blank :doh: the only work around i could find is initialiazing the bitmap with setpixels one by one. This is obviously very slow and ive been trying to find a faster way :/ any suggetions ??

    C 1 Reply Last reply
    0
    • S stewwi

      Hi all i have a small problem I am creating a new blank bitmap in a program, then using the lock bitmap i edit pixels. the Purpose iss to build a spectograph. Now my problem is that when i create the bitmap Bitmap pane = new Bitmap(800, 800); then edit it using pointers the image remains blank :doh: the only work around i could find is initialiazing the bitmap with setpixels one by one. This is obviously very slow and ive been trying to find a faster way :/ any suggetions ??

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If you posted some code, we could perhaps suggest where it was broken. Obviously, the answer is to lock bits and edit the pixels, so you're doing something wrong. My image processing articles have lots of examples on how to do this, they are on this site, just search for graus and you'll find them :-) Christian Graus - Microsoft MVP - C++

      S 1 Reply Last reply
      0
      • C Christian Graus

        If you posted some code, we could perhaps suggest where it was broken. Obviously, the answer is to lock bits and edit the pixels, so you're doing something wrong. My image processing articles have lots of examples on how to do this, they are on this site, just search for graus and you'll find them :-) Christian Graus - Microsoft MVP - C++

        S Offline
        S Offline
        stewwi
        wrote on last edited by
        #3

        Sure i Create the bitmap here Bitmap pane = new Bitmap(800, 800); then i had to init the bitmap with a colour to get it working for (int i = 0; i < pane.Height; i++) { for (int j = 0; j < pane.Width; j++) { pane.SetPixel(j, i, Color.Black); } } basically i have to get rid of the code above then i use unsafe mode to set colours to individual pixels public Bitmap backup(Bitmap pane) { BitmapData panedata = pane.LockBits(new Rectangle(0, 0, pane.Width, pane.Height), ImageLockMode.ReadWrite, pane.PixelFormat); int stride = panedata.Stride; //returns the location of the first pixel of the image IntPtr Scan0 = panedata.Scan0; unsafe { byte* p = (byte*)(void*)Scan0; int nOffset = stride - pane.Height *3 ; int nWidth = pane.Width * 3; for (int y = 0; y < pane.Height; ++y) { for (int x = 0; x < pane.Width; ++x) { *(p + 0) = (byte)255; *(p + 1) = (byte)0; *(p + 2) = (byte)0; p += 4; } } } pane.UnlockBits(panedata); return pane; } like that it works but its takes ages to start

        A R C 3 Replies Last reply
        0
        • S stewwi

          Sure i Create the bitmap here Bitmap pane = new Bitmap(800, 800); then i had to init the bitmap with a colour to get it working for (int i = 0; i < pane.Height; i++) { for (int j = 0; j < pane.Width; j++) { pane.SetPixel(j, i, Color.Black); } } basically i have to get rid of the code above then i use unsafe mode to set colours to individual pixels public Bitmap backup(Bitmap pane) { BitmapData panedata = pane.LockBits(new Rectangle(0, 0, pane.Width, pane.Height), ImageLockMode.ReadWrite, pane.PixelFormat); int stride = panedata.Stride; //returns the location of the first pixel of the image IntPtr Scan0 = panedata.Scan0; unsafe { byte* p = (byte*)(void*)Scan0; int nOffset = stride - pane.Height *3 ; int nWidth = pane.Width * 3; for (int y = 0; y < pane.Height; ++y) { for (int x = 0; x < pane.Width; ++x) { *(p + 0) = (byte)255; *(p + 1) = (byte)0; *(p + 2) = (byte)0; p += 4; } } } pane.UnlockBits(panedata); return pane; } like that it works but its takes ages to start

          A Offline
          A Offline
          Aaron Dilliard
          wrote on last edited by
          #4

          Is there any particular reason you are using pointers to do this? It could be accomplished whithout them. You could tie your bitmap to a graphics object... Graphics g=Graphics.FromImage(myBmp); ... then you could fill the image (much faster than indexing) g.Clear(Color.Black); Next, I take it you are setting the RGB value of each pixel in your for loop, but you are setting them all to Red as it looks, so why not just use.... g.Clear(Color.Red); This will work very fast. If this isnt the solution you are needing, let me know, Aaron

          S 1 Reply Last reply
          0
          • A Aaron Dilliard

            Is there any particular reason you are using pointers to do this? It could be accomplished whithout them. You could tie your bitmap to a graphics object... Graphics g=Graphics.FromImage(myBmp); ... then you could fill the image (much faster than indexing) g.Clear(Color.Black); Next, I take it you are setting the RGB value of each pixel in your for loop, but you are setting them all to Red as it looks, so why not just use.... g.Clear(Color.Red); This will work very fast. If this isnt the solution you are needing, let me know, Aaron

            S Offline
            S Offline
            stewwi
            wrote on last edited by
            #5

            i pasted only part of the code, the g.clear to fill the bitmap worked nicely just what i needed :) and basically im trying to build a spectograph so not every pixel is being drawn and everypixel i draw has a different weight :) thats why im going tthrough it pixel by pixel. the program is still in the early stages though thats why it may look a bit raw. Thanks a lot for your help ! :)

            1 Reply Last reply
            0
            • S stewwi

              Sure i Create the bitmap here Bitmap pane = new Bitmap(800, 800); then i had to init the bitmap with a colour to get it working for (int i = 0; i < pane.Height; i++) { for (int j = 0; j < pane.Width; j++) { pane.SetPixel(j, i, Color.Black); } } basically i have to get rid of the code above then i use unsafe mode to set colours to individual pixels public Bitmap backup(Bitmap pane) { BitmapData panedata = pane.LockBits(new Rectangle(0, 0, pane.Width, pane.Height), ImageLockMode.ReadWrite, pane.PixelFormat); int stride = panedata.Stride; //returns the location of the first pixel of the image IntPtr Scan0 = panedata.Scan0; unsafe { byte* p = (byte*)(void*)Scan0; int nOffset = stride - pane.Height *3 ; int nWidth = pane.Width * 3; for (int y = 0; y < pane.Height; ++y) { for (int x = 0; x < pane.Width; ++x) { *(p + 0) = (byte)255; *(p + 1) = (byte)0; *(p + 2) = (byte)0; p += 4; } } } pane.UnlockBits(panedata); return pane; } like that it works but its takes ages to start

              R Offline
              R Offline
              Robert Rohde
              wrote on last edited by
              #6

              I think you are not seeing any changes because you don't set the alpha value and when it is left to 0 you won't see anything (everything is transparent). Adjust the contents of your inner loop:

              *(p + 0) = (byte)255;
              *(p + 1) = (byte)0;
              *(p + 2) = (byte)0;
              *(p + 3) = (byte)255;
              p += 4;

              S 1 Reply Last reply
              0
              • R Robert Rohde

                I think you are not seeing any changes because you don't set the alpha value and when it is left to 0 you won't see anything (everything is transparent). Adjust the contents of your inner loop:

                *(p + 0) = (byte)255;
                *(p + 1) = (byte)0;
                *(p + 2) = (byte)0;
                *(p + 3) = (byte)255;
                p += 4;

                S Offline
                S Offline
                stewwi
                wrote on last edited by
                #7

                erm yes that was it ! forgot about the alpha component :/ silly me :-O

                1 Reply Last reply
                0
                • S stewwi

                  Sure i Create the bitmap here Bitmap pane = new Bitmap(800, 800); then i had to init the bitmap with a colour to get it working for (int i = 0; i < pane.Height; i++) { for (int j = 0; j < pane.Width; j++) { pane.SetPixel(j, i, Color.Black); } } basically i have to get rid of the code above then i use unsafe mode to set colours to individual pixels public Bitmap backup(Bitmap pane) { BitmapData panedata = pane.LockBits(new Rectangle(0, 0, pane.Width, pane.Height), ImageLockMode.ReadWrite, pane.PixelFormat); int stride = panedata.Stride; //returns the location of the first pixel of the image IntPtr Scan0 = panedata.Scan0; unsafe { byte* p = (byte*)(void*)Scan0; int nOffset = stride - pane.Height *3 ; int nWidth = pane.Width * 3; for (int y = 0; y < pane.Height; ++y) { for (int x = 0; x < pane.Width; ++x) { *(p + 0) = (byte)255; *(p + 1) = (byte)0; *(p + 2) = (byte)0; p += 4; } } } pane.UnlockBits(panedata); return pane; } like that it works but its takes ages to start

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  Having read the thread, I can't believe you need to set alpha. I've never, ever had to set the fourth value, and it's not actually used for alpha by windows, unless the Bitmap object uses it. int nOffset = stride - pane.Height *3 ; This is wrong, you mean pane.Width. However, as you're using a 32 bit image, you don't need an offset, anyhow. Christian Graus - Microsoft MVP - C++

                  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