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. Reducing color depth of an image in c#

Reducing color depth of an image in c#

Scheduled Pinned Locked Moved C#
csharpgraphicsquestion
9 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.
  • C Offline
    C Offline
    CyberKewl
    wrote on last edited by
    #1

    Is there a way to reduce the color depth (in bits) of an image (whether it is saved or in a Bitmap form) in c#? If possible, please include a sample code.

    J 1 Reply Last reply
    0
    • C CyberKewl

      Is there a way to reduce the color depth (in bits) of an image (whether it is saved or in a Bitmap form) in c#? If possible, please include a sample code.

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      There might be a simpler way, but this way works:

      //assume the original bitmap is named bmp, and the
      //new pixel format flag is in a variable named format.
       
      Bitmap newbmp=new Bitmap(format,bmp.Width,bm.Height);
      Graphics newbmpgr=Graphics.FromImage((Image)newbmp);
       
      newbmpgr.DrawImageUnscaled((Image)bmp,0,0);
      newbmpgr.Dispose();
       
      //newbmp now contains old bitmap's data,
      //but in the new format.

      Remember that if you convert the image from another format into 8-bit indexed color, you will have to set the palette entries yourself before doing the DrawImageUnscaled() call.

      "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
      "You must be the change you wish to see in the world." - Mahatma Gandhi

      C 1 Reply Last reply
      0
      • J J Dunlap

        There might be a simpler way, but this way works:

        //assume the original bitmap is named bmp, and the
        //new pixel format flag is in a variable named format.
         
        Bitmap newbmp=new Bitmap(format,bmp.Width,bm.Height);
        Graphics newbmpgr=Graphics.FromImage((Image)newbmp);
         
        newbmpgr.DrawImageUnscaled((Image)bmp,0,0);
        newbmpgr.Dispose();
         
        //newbmp now contains old bitmap's data,
        //but in the new format.

        Remember that if you convert the image from another format into 8-bit indexed color, you will have to set the palette entries yourself before doing the DrawImageUnscaled() call.

        "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
        "You must be the change you wish to see in the world." - Mahatma Gandhi

        C Offline
        C Offline
        CyberKewl
        wrote on last edited by
        #3

        I was trying to convert a 1024x768 image and i got an out of memory error (i have around 200mb free out of 512mb so that's not possible). Is there a workaround?

        J 1 Reply Last reply
        0
        • C CyberKewl

          I was trying to convert a 1024x768 image and i got an out of memory error (i have around 200mb free out of 512mb so that's not possible). Is there a workaround?

          J Offline
          J Offline
          J Dunlap
          wrote on last edited by
          #4

          CyberKewl wrote: I was trying to convert a 1024x768 image and i got an out of memory error :rolleyes: :confused: :~ I don't know why that happens. CyberKewl wrote: Is there a workaround? Not that I know of - any way you do it, you're going to have to create 2 different bitmaps. But it really should work. What's your OS?

          "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
          "You must be the change you wish to see in the world." - Mahatma Gandhi

          C 1 Reply Last reply
          0
          • J J Dunlap

            CyberKewl wrote: I was trying to convert a 1024x768 image and i got an out of memory error :rolleyes: :confused: :~ I don't know why that happens. CyberKewl wrote: Is there a workaround? Not that I know of - any way you do it, you're going to have to create 2 different bitmaps. But it really should work. What's your OS?

            "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
            "You must be the change you wish to see in the world." - Mahatma Gandhi

            C Offline
            C Offline
            CyberKewl
            wrote on last edited by
            #5

            Windows XP SP1

            C 1 Reply Last reply
            0
            • C CyberKewl

              Windows XP SP1

              C Offline
              C Offline
              CyberKewl
              wrote on last edited by
              #6

              Ok i've just tried again but changed the pixelformat to PixelFormat.Format24bppRgb but the filesize and the color depth did not change at all (i saved newbmp as png).

              J R 2 Replies Last reply
              0
              • C CyberKewl

                Ok i've just tried again but changed the pixelformat to PixelFormat.Format24bppRgb but the filesize and the color depth did not change at all (i saved newbmp as png).

                J Offline
                J Offline
                J Dunlap
                wrote on last edited by
                #7

                So what's your starting pixel format and what's your new pixel format?

                "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                "You must be the change you wish to see in the world." - Mahatma Gandhi

                A 1 Reply Last reply
                0
                • J J Dunlap

                  So what's your starting pixel format and what's your new pixel format?

                  "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
                  "You must be the change you wish to see in the world." - Mahatma Gandhi

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #8

                  I'm using the default which is 32bpp i think, i didn't change the pixelformat for the original, only the destination one as mentioned earlier to 24bpp...

                  1 Reply Last reply
                  0
                  • C CyberKewl

                    Ok i've just tried again but changed the pixelformat to PixelFormat.Format24bppRgb but the filesize and the color depth did not change at all (i saved newbmp as png).

                    R Offline
                    R Offline
                    Russell Morris
                    wrote on last edited by
                    #9

                    Internally, GDI+ represents all bitmap/drawing data as 32bpp. As far as I am aware, none of the standard Microsoft image codecs (bmp,jpg,png,etc...) will do any sort of color conversion for you. -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

                    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