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 set pixel into bitmap with Pixel format of Format8bppIndexed ? [modified]

How to set pixel into bitmap with Pixel format of Format8bppIndexed ? [modified]

Scheduled Pinned Locked Moved C#
graphicstutorialquestion
3 Posts 3 Posters 1 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I define new bitmap in my code Bitmap image = new Bitmap( width, height, PixelFormat.Format8bppIndexed); ==> that mean that this bitmap is gray scale color. Now, i want to define what color will be at place (i, j) so i usually use ( on 24 color bit ) the method 'SetPixel( i, j, Color.FromRGB( ... ) )' But in this case that the bitmap is on gray scale i cant do it because i have only one value of the color ... and the application crash all the time when i try to call Color.FromRGB(theOneColorIhave) Someone know other way to do set the pixel values - some way that can be use with 8 bit format ? Thanks

    modified on Wednesday, November 3, 2010 10:36 AM

    C _ 2 Replies Last reply
    0
    • L Lost User

      I define new bitmap in my code Bitmap image = new Bitmap( width, height, PixelFormat.Format8bppIndexed); ==> that mean that this bitmap is gray scale color. Now, i want to define what color will be at place (i, j) so i usually use ( on 24 color bit ) the method 'SetPixel( i, j, Color.FromRGB( ... ) )' But in this case that the bitmap is on gray scale i cant do it because i have only one value of the color ... and the application crash all the time when i try to call Color.FromRGB(theOneColorIhave) Someone know other way to do set the pixel values - some way that can be use with 8 bit format ? Thanks

      modified on Wednesday, November 3, 2010 10:36 AM

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      Try this function to get a color supported by your currently selected palette.

      Greetings Covean

      1 Reply Last reply
      0
      • L Lost User

        I define new bitmap in my code Bitmap image = new Bitmap( width, height, PixelFormat.Format8bppIndexed); ==> that mean that this bitmap is gray scale color. Now, i want to define what color will be at place (i, j) so i usually use ( on 24 color bit ) the method 'SetPixel( i, j, Color.FromRGB( ... ) )' But in this case that the bitmap is on gray scale i cant do it because i have only one value of the color ... and the application crash all the time when i try to call Color.FromRGB(theOneColorIhave) Someone know other way to do set the pixel values - some way that can be use with 8 bit format ? Thanks

        modified on Wednesday, November 3, 2010 10:36 AM

        _ Offline
        _ Offline
        _Erik_
        wrote on last edited by
        #3

        You cannot use SetPixel if your bitmap pixel format is indexed, so you will have to use the BitmapData object returned by LockBits[^] method. Edit Ok, this is a little tricky, so I will give a little sample on how to do this before you have to ask again:

        unsafe Bitmap ToGrayScale(Bitmap bmp)
        {
        Bitmap grayBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format8bppIndexed);

        // Prepare the palette
        ColorPalette pal = grayBmp.Palette;
        for (int i = 0; i < 256; i++)
            pal.Entries\[i\] = Color.FromArgb(i, i, i);
        
        grayBmp.Palette = pal;
        
        // Get BitmapData
        BitmapData bd = grayBmp.LockBits(
            new Rectangle(0, 0, bmp.Width, bmp.Height),
            ImageLockMode.ReadWrite,
            PixelFormat.Format8bppIndexed);
        
        byte\* ptr = (byte\*)bd.Scan0.ToPointer();
        
        for (int i = 0; i < bmp.Width; i++)
        {
            for (int j = 0; j < bmp.Height; j++)
            {
                Color c = bmp.GetPixel(i, j);
        
                // Offset must be Y\*Stride + X\*BytesPerPixel. In this case,
                // BytesPerPixel is 1
                ptr\[j \* bd.Stride + i\] = (byte)((c.R + c.G + c.B) / 3);
            }
        }
        
        // Unlock
        grayBmp.UnlockBits(bd);
        
        return grayBmp;
        

        }

        Sure I should have locked bits also for bmp, becouse GetPixel performance is really bad but, hey, I feel a little lazy today...

        modified on Wednesday, November 3, 2010 12:01 PM

        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