Hi

First of, I am posting to this newsgroup as it seemed best suited even though I am writing C++ for a standard Windows application. The application uses GDI+ which forms the basis of the System.Drawing namespace...anyway here goes:

I am trying to create a 1 bit per pixel mask to use as an overlay. The data for the mask i generated dynamically based on user input and is available as a byte array. I get the first failure when trying to create the mask bitmap, the code is a follows:

/**
* Utiliy method, creates a mask bitmap and modifes the palette accordingly.
* @param size Size of the mask.
* @param color Color of the mask.
* @param pixels The pixels.
*/
Bitmap* createMask( const Size& size, const Color& color, Byte* pixels ) {
Bitmap* mask = new Bitmap( size.width, size.height, ((size.width+3)>>2)*4/8, PixelFormat1bppIndexed, pixels );
UInt paletteSize = mask->GetPaletteSize();
if( paletteSize > 0 ) {
ColorPalette* palette = (ColorPalette*)malloc( paletteSize );
mask->GetPalette( palette, paletteSize );
if( palette->Count == 2 ) {
palette->Entries[0] = Gdiplus::Color::Black;
palette->Entries[1] = Gdiplus::Color::MakeARGB( color.GetA(), color.GetR(), color.GetG(), color.GetB() );
}
mask->SetPalette( palette );
free( palette );
}

return mask;
}

Since that didn't work, I tried letting GDI+ create a mask bitmap of the specified size and the copy the pixels into that bitmap. But when I try to display the image I either get nothing or a GPF. My display code is very simple:

// Bitmap *maskBitmap; // Defined elsewhere

Graphics g( hDC );
g.DrawImage( maskBitmap, 0, 0 );

So it basically just creates Graphics object and attempts to draw the image. The latter fails however...

Teis Johansen

Re: Creating a 1bpp mask from a byte array by Bob

Bob
Sat Apr 02 13:36:01 CST 2005

You cannot obtain a Graphics object for any indexed pixel image such as a
1bpp bitmap.

There are several ways to go about this. Why do you need a 1bpp mask image?

While you're rhinking about that read the GDI+ FAQ as well. The articles on
1bpp images and LockBits will have particular relavence for you.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"Teis Johansen" <news@siet.dk> wrote in message
news:ugDMAg4NFHA.244@TK2MSFTNGP12.phx.gbl...
> Hi
>
> First of, I am posting to this newsgroup as it seemed best suited even
> though I am writing C++ for a standard Windows application. The
> application uses GDI+ which forms the basis of the System.Drawing
> namespace...anyway here goes:
>
> I am trying to create a 1 bit per pixel mask to use as an overlay. The
> data for the mask i generated dynamically based on user input and is
> available as a byte array. I get the first failure when trying to create
> the mask bitmap, the code is a follows:
>
> /**
> * Utiliy method, creates a mask bitmap and modifes the palette
> accordingly.
> * @param size Size of the mask.
> * @param color Color of the mask.
> * @param pixels The pixels.
> */
> Bitmap* createMask( const Size& size, const Color& color, Byte* pixels ) {
> Bitmap* mask = new Bitmap( size.width, size.height,
> ((size.width+3)>>2)*4/8, PixelFormat1bppIndexed, pixels );
> UInt paletteSize = mask->GetPaletteSize();
> if( paletteSize > 0 ) {
> ColorPalette* palette = (ColorPalette*)malloc( paletteSize );
> mask->GetPalette( palette, paletteSize );
> if( palette->Count == 2 ) {
> palette->Entries[0] = Gdiplus::Color::Black;
> palette->Entries[1] = Gdiplus::Color::MakeARGB( color.GetA(),
> color.GetR(), color.GetG(), color.GetB() );
> }
> mask->SetPalette( palette );
> free( palette );
> }
>
> return mask;
> }
>
> Since that didn't work, I tried letting GDI+ create a mask bitmap of the
> specified size and the copy the pixels into that bitmap. But when I try to
> display the image I either get nothing or a GPF. My display code is very
> simple:
>
> // Bitmap *maskBitmap; // Defined elsewhere
>
> Graphics g( hDC );
> g.DrawImage( maskBitmap, 0, 0 );
>
> So it basically just creates Graphics object and attempts to draw the
> image. The latter fails however...
>
> Teis Johansen