Lots of bitmap problems

  • Thread starter Thread starter James Dean
  • Start date Start date
J

James Dean

I have 1 bit per pixel information and i have the width and height of
this data.Each bit corresponds to a 24 bit colour value. I want to
convert this to 24bit per pixel bitmap.
Do i need to multiply the width and the height by 24 or just the width?.
I tried to multiply the width by 24 but the value is too large?.....what
can i do?.
The 1bpp data is at resolution 600dpi.....can i just use the set
resolution function to change it to 96dpi or do i need to do something
else?.

Basically i want to know the steps i need to do in C#......How to create
a 24bpp 96dpi bitmap from 1bpp 600dpi(width and height) resolution data.



*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
You can simply draw the 1 bit-per-pixel image onto a 24 bit image using
DrawImage...

Bitmap onebit=(Bitmap)Image.FromFile("theimagefile");
Bitmap bm=new Bitmap(onebit.Width, onebit.Height,
PixelFormat.Format24bppRGB);
Graphics g=Graphics.FromImage(bm);
g.DrawImage(onebit,new
Rectangle(0,0,onebit.Width,onebit.Height),0,0,onebit.Width,onebit.Height,Gra
phicsUnit.Pixel);
g.dDispose();

The bm bitmap now contains a 24bpp copy of the original one bit per pixel
image.

The resolution of the copy will not be the same as that of the original. You
can change this using the SetResolution method. Remember that resolution has
nothing to do with pixel-dimensions.


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

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

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

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
But unfortunately its compressed 1bpp data.....i have an RGB color value
for each pixel that is set?.


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Hi James
I have 1 bit per pixel information and i have the width and height of
this data.Each bit corresponds to a 24 bit colour value. I want to
convert this to 24bit per pixel bitmap.

Try this:

Bitmap.Clone(Rectangle rect,
PixelFormat format).
Do i need to multiply the width and the height by 24 or just the width?.
I tried to multiply the width by 24 but the value is too large?.....what
can i do?.
The 1bpp data is at resolution 600dpi.....can i just use the set
resolution function to change it to 96dpi or do i need to do something
else?.

Basically i want to know the steps i need to do in C#......How to create
a 24bpp 96dpi bitmap from 1bpp 600dpi(width and height) resolution data.

I don't know much about dpis, because i allways count bitmaps in
pixels, but there is...

Bitmap.SetResolution(
float xDpi,
float yDpi
);

....that you can try.

Cheers

Marcin
 
Hi James
But unfortunately its compressed 1bpp data.....i have an RGB color value
for each pixel that is set?.

"Do you want to convert 1bit bitmap data - not Bitmap - (width*height/8
bytes) into 24bpp Bitmap?"
If this data is compressed, then only easy way is to try
to create Bitmap from stream... :(

The simplest way is to create new 24bpp Bitmap
and iterate through whole (1bit) bitmap data:

1. Remeber that 1bpp bitmap row has length = width/8,
2. Your destination 24bpp bitmap will be approx. 24x greater than 1bpp
3. Treat each byte of source as 8 pixels for destiation bitmap
(8 * 3 bytes of output per 1 input byte)

There should be more threads in this group history that
could be very usefull.

Regards

Marcin
 
I have trouble displaying the bitmap. I have 1bpp information. I also
have a command telling me what color i need to set the bytes to when the
colour is switched on. The trouble is i do all this but it will not
display properly. I convert this to 8bpp information. I set the relevant
pixel to for example "255,0,0" for Red.....is this right?....i set the
Red byte to 255 and the other two values set to 0. I think the Bitmap
data class i am using is not working properly. Have a look at the data
below.

bitmap = new
Bitmap(PageSizeInPixels.Width,PageSizeInPixels.Height,System.Drawing.Ima
ging.PixelFormat.Format8bppIndexed);

public struct PixelData
{
public byte RedValue;
public byte GreenValue;
public byte BlueValue;
};

public void LockBitmap()
{
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF boundsF = bitmap[bitmap_Count - 1].GetBounds(ref unit);
Rectangle bounds = new Rectangle((int) boundsF.X,
(int) boundsF.Y,
(int) boundsF.Width,
(int) boundsF.Height);

// Figure out the number of bytes in a row
// This is rounded up to be a multiple of 4
// bytes, since a scan line in an image must always be a multiple of 4
bytes
// in length.
width = (int) boundsF.Width * sizeof(PixelsData);
if (width % 4 != 0)
{
width = 4 * (width / 4 + 1);
}
bitmapData =
bitmap[bitmap_Count - 1].LockBits(bounds, ImageLockMode.ReadWrite,
PixelFormat.Format8bppIndexed);

pBase = (Byte*) bitmapData.Scan0.ToPointer();
}

public void Make24BppBitmap()
{

//LineDataStore stores the 1bpp information
fixed(byte* ThisData = LineDataStore)
{
byte* CurrLine = ThisData + (y * ByteWidth);
PixelData* pPixel = PixelAt(0, y);
for (int x = 0; x < size.X; x++)
{
//check to see if bit is set by using masking
if it is set
then
{
pPixel->RedValue = GetBitmapColorValue[0];
pPixel->GreenValue = GetBitmapColorValue[1];
pPixel->BlueValue = GetBitmapColorValue[2];
}
pPixel++;
}

}

}

For the above i only get a black bitmap......whats the problem.......




*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Hi James,

1st of all, because i'm missing somethig:

- Can't you simply load this 1bpp bitmap by Image.Load(...) or
can't you initialize Bitmap with 1bpp data (as stream)?
- What do you convert into 8bpp (1bpp data)?

I have trouble displaying the bitmap. I have 1bpp information. I also
have a command telling me what color i need to set the bytes to when the
colour is switched on. The trouble is i do all this but it will not
display properly. I convert this to 8bpp information. I set the relevant
pixel to for example "255,0,0" for Red.....is this right?....i set the
Red byte to 255 and the other two values set to 0. I think the Bitmap
data class i am using is not working properly. Have a look at the data
below.

bitmap = new
Bitmap(PageSizeInPixels.Width,PageSizeInPixels.Height,System.Drawing.Ima
ging.PixelFormat.Format8bppIndexed);

public struct PixelData
{
public byte RedValue;
public byte GreenValue;
public byte BlueValue;
};

If your structure has fields in byte order then i think
that Red should be replaced with Blue (100% in Windows .bmp)
public void LockBitmap()
{
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF boundsF = bitmap[bitmap_Count - 1].GetBounds(ref unit);
Rectangle bounds = new Rectangle((int) boundsF.X,
(int) boundsF.Y,
(int) boundsF.Width,
(int) boundsF.Height);

// Figure out the number of bytes in a row
// This is rounded up to be a multiple of 4
// bytes, since a scan line in an image must always be a multiple of 4
bytes
// in length.
width = (int) boundsF.Width * sizeof(PixelsData);
if (width % 4 != 0)
{
width = 4 * (width / 4 + 1);
}
bitmapData =
bitmap[bitmap_Count - 1].LockBits(bounds, ImageLockMode.ReadWrite,
PixelFormat.Format8bppIndexed);

pBase = (Byte*) bitmapData.Scan0.ToPointer();
}

public void Make24BppBitmap()
{

//LineDataStore stores the 1bpp information
fixed(byte* ThisData = LineDataStore)
{
byte* CurrLine = ThisData + (y * ByteWidth);
PixelData* pPixel = PixelAt(0, y);
for (int x = 0; x < size.X; x++)
{
//check to see if bit is set by using masking
if it is set
then
{
pPixel->RedValue = GetBitmapColorValue[0];
pPixel->GreenValue = GetBitmapColorValue[1];
pPixel->BlueValue = GetBitmapColorValue[2];
}
pPixel++;
}

}

}

For the above i only get a black bitmap......whats the problem.......

I'm sorry but i cannot help you with your code!
I found some parts missing:
- GetBitmapColorValue
- PixelAt
....and the better way is to pass volatile variables by parameters than
keeping them as a class fields.

But i can give few tips:
- Doesn't forget to UnlockBits(...) !

If this 1bpp bitmap is not "top secret" then you can
send me directly (for testing only ;) :
(e-mail address removed)

Regards

Marcin
 
The 1bpp image will have a colour palette, you can set the palette values or
alternatively use a ColorMap and an ImageAttributes class to change the
colour as it's drawn.

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

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

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

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
Back
Top