Home All Groups Group Topic Archive Search About

Editing an indexed PixelFormat

Author
24 Nov 2007 5:15 AM
Nathan Sokalski
I have created declared a Bitmap using the following statement:

Dim bmp As New Bitmap(100, 100, PixelFormat.Format8bppIndexed)

Because the SetPixel() method is disabled and a Graphics object cannot be
created for indexed PixelFormats, I am not sure how to edit the Bitmap. I am
assuming that there is some class or technique other than the following:

  Dim bmpdata As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width,
bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed)
  For y As Integer = 0 To bmp.Height - 1
   For x As Integer = 0 To bmp.Width - 1
    System.Diagnostics.Debug.WriteLine(System.Runtime.InteropServices.Marshal.ReadByte(bmpdata.Scan0,
y * bmpdata.Stride + x))
   Next
  Next
  bmp.UnlockBits(bmpdata)

Which is basically just directly changing the data that will be saved when
the Bitmap is saved. However, using the technique shown above can make it
require multiple steps and calculations when drawing shapes such as
ellipses. Is there a more efficient way to edit an indexed Bitmap? Thanks.
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/

Author
24 Nov 2007 2:59 PM
Michael Phillips, Jr.
> Is there a more efficient way to edit an indexed Bitmap?

If you use any of the CLR routines, you are stuck with LockBits.

If you are willing to use P-Invoke, you can avail yourself of the many GDI
routines which provide direct access to the image's memory
and allow the use of fast BitBlt and similar functions that will operate on
all image pixel formats including indexed.

Create your indexed bitmap with CreateDIBSection and you will have all of
the fast GDI functions at your disposal.
CreateDIBSection returns a pointer to the image's memory that can be
directly accessed without locking and unlocking.

You can still mix and match the managed and unmanaged image API's.
You can still benefit from the System.Drawing namespace methods.


Show quoteHide quote
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
news:uTspDklLIHA.2268@TK2MSFTNGP02.phx.gbl...
>I have created declared a Bitmap using the following statement:
>
> Dim bmp As New Bitmap(100, 100, PixelFormat.Format8bppIndexed)
>
> Because the SetPixel() method is disabled and a Graphics object cannot be
> created for indexed PixelFormats, I am not sure how to edit the Bitmap. I
> am assuming that there is some class or technique other than the
> following:
>
>  Dim bmpdata As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width,
> bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed)
>  For y As Integer = 0 To bmp.Height - 1
>   For x As Integer = 0 To bmp.Width - 1
>
> System.Diagnostics.Debug.WriteLine(System.Runtime.InteropServices.Marshal.ReadByte(bmpdata.Scan0,
> y * bmpdata.Stride + x))
>   Next
>  Next
>  bmp.UnlockBits(bmpdata)
>
> Which is basically just directly changing the data that will be saved when
> the Bitmap is saved. However, using the technique shown above can make it
> require multiple steps and calculations when drawing shapes such as
> ellipses. Is there a more efficient way to edit an indexed Bitmap? Thanks.
> --
> Nathan Sokalski
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>