473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

creating a .bmp file

Hi,
I have all the components of a .bmp file in memory wich i need
to write to a .bmp file, (or other convenient image file)
ie i have 256 color pallete of RGBquads and array of 8bit pixels,

I can create a System.Drawing.Bitmap and it allows me to set the
witdh,height,bpp, and even the pixel data,
but it doesnt seem to let me set the color palette,
the palette property field is read/write
but requires a System.Drawing.Imaging.ColorPalette
but there seems to be no way of creating one of these,

there must be a way surly am I missing something or do
I have to go and create the wheel all over again and
write my own BITMAPFILEHEADER,BITMAPINFOHEADER,etc
and write those to the file ?

Colin =^.^=
Nov 17 '07 #1
9 6545
On 2007-11-17 15:29:53 -0800, "colin" <co*********@ntworld.NOSPAM.comsaid:
I have all the components of a .bmp file in memory wich i need
to write to a .bmp file, (or other convenient image file)
ie i have 256 color pallete of RGBquads and array of 8bit pixels,

I can create a System.Drawing.Bitmap and it allows me to set the
witdh,height,bpp, and even the pixel data,
but it doesnt seem to let me set the color palette,
the palette property field is read/write
but requires a System.Drawing.Imaging.ColorPalette
but there seems to be no way of creating one of these,
I admit, I don't have any real experience using indexed color formats
in .NET. I do find it surprising that there's no good way to create a
palette.

However, the Microsoft knowledge base includes this suggestion to
create a dummy bitmap and grab the palette from that before disposing
the dummy bitmap:
http://support.microsoft.com/kb/319061

Also, it seems to me that if you've created an indexed-format Bitmap
instance, you should be able to just modify the palette that is created
with that instance, rather than replacing it altogether.

I would try the latter first.

And barring either of those methods working out for you, it's not like
it's really all that difficult to write the BMP file from scratch, even
if it is annoying. :)

Pete

Nov 18 '07 #2
"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:200711171605488930-NpOeStPeAdM@NnOwSlPiAnMkcom...
On 2007-11-17 15:29:53 -0800, "colin" <co*********@ntworld.NOSPAM.com>
said:
>I have all the components of a .bmp file in memory wich i need
to write to a .bmp file, (or other convenient image file)
ie i have 256 color pallete of RGBquads and array of 8bit pixels,

I can create a System.Drawing.Bitmap and it allows me to set the
witdh,height,bpp, and even the pixel data,
but it doesnt seem to let me set the color palette,
the palette property field is read/write
but requires a System.Drawing.Imaging.ColorPalette
but there seems to be no way of creating one of these,

I admit, I don't have any real experience using indexed color formats in
.NET. I do find it surprising that there's no good way to create a
palette.

However, the Microsoft knowledge base includes this suggestion to create a
dummy bitmap and grab the palette from that before disposing the dummy
bitmap:
http://support.microsoft.com/kb/319061

Also, it seems to me that if you've created an indexed-format Bitmap
instance, you should be able to just modify the palette that is created
with that instance, rather than replacing it altogether.

I would try the latter first.

And barring either of those methods working out for you, it's not like
it's really all that difficult to write the BMP file from scratch, even if
it is annoying. :)

Pete
thanks, well I dont actually have an instance of a BMP image to start with,
but I do have the individual components wich are extracted from a texture
file, where one palette is shared by more than one pixel array.

I simply wish to put each image into a more easily displayed format.

il prob do the write the data to file directly,
seems a fag just becuase you cant actually create a palette.

Colin =^.^=
Nov 18 '07 #3

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:2007111716553727544-NpOeStPeAdM@NnOwSlPiAnMkcom...
On 2007-11-17 16:32:24 -0800, "colin" <co*********@ntworld.NOSPAM.com>
said:
>thanks, well I dont actually have an instance of a BMP image to start
with,
but I do have the individual components wich are extracted from a texture
file, where one palette is shared by more than one pixel array.

Well, how did you expect to get the raw pixel data into a Bitmap instance?
with the Bitmap constructor, it allows you to provide all but the palette...

IntPtr
scan=Marshal.UnsafeAddrOfPinnedArrayElement(mip.Da taArray.ToArray(),0);
Bitmap bmp = new Bitmap(mip.USize, mip.VSize, ((mip.USize+3)/4)*4,
PixelFormat.Format8bppIndexed, scan);
Presumably you would be using LockBits. But LockBits requires a Bitmap
instance to be created in the first place.

So, presumably you were going to create a Bitmap instance, use LockBits to
set the pixel data for the image, and apply a palette to that Bitmap.
Well, just create the correct Bitmap format in the first place, get the
palette from the Palette property and modify that palette directly.
>I simply wish to put each image into a more easily displayed format.

And so you can.
>il prob do the write the data to file directly,
seems a fag just becuase you cant actually create a palette.

I don't understand that comment. I just described TWO different ways to
"create a palette" (or at least to get one you can set appropriately).
I re read the link to the gif file, and saw the answer,
it was rather long and didnt seem to apply,
it seemed quicker to do it the dirty way,
however this didnt work and rather than spend ages debuging i re read what
you wrote a few times.

I failed to see how to actually modify the palette before re-reading a few
times lol.
for (x = 0; x < 256; x++)
{
bmp.Palette.Entries[x] = Color.FromArgb(palette[x].R, palette[x].G,
palette[x].B);
}
thanks it works now, although i think ive got my rows/cols messed up somehow
as it looks odd
but im kinda guesing at the format ive got.

Colin =^.^=
Nov 18 '07 #4
On 2007-11-17 17:44:09 -0800, "colin" <co*********@ntworld.NOSPAM.comsaid:
[...]
thanks it works now, although i think ive got my rows/cols messed up somehow
as it looks odd
but im kinda guesing at the format ive got.
Glad to hear that you were able to figure out what I meant.

As far as how it looks, I would guess you've got something wrong with
the stride somehow (that usually being the issue when someone says
their bitmap "looks odd" :) ), but it's hard to say for sure without
having all the information.

Pete

Nov 18 '07 #5
This is the coode I have so far ...

IntPtr scan =
Marshal.UnsafeAddrOfPinnedArrayElement(mip.DataArr ay.ToArray(), 0);
Bitmap bmp = new Bitmap(mip.VSize, mip.USize, mip.VSize ,
PixelFormat.Format8bppIndexed, scan);
int x;
//System.Drawing.Imaging.ColorPalette pal = bmp.Palette;
for (x = 0; x < 256; x++)
{
FColor c = palette.Colors[x];
bmp.Palette.Entries[x] = System.Drawing.Color.FromArgb(c.R, c.G, c.B);
}
bmp.Save(filename + i.ToString() + ".bmp");
However the file seems to be saved in .png format,
256x256 8bpp file is only 22k insterad of at least 65k.
but my data isnt compressed,
wich is probably why its looking weird lol.

how can i specify/force it to use .BMP type format rather than png ?

Colin =^.^=
Nov 18 '07 #6
On 2007-11-17 18:22:53 -0800, "colin" <co*********@ntworld.NOSPAM.comsaid:
[...]
However the file seems to be saved in .png format,
256x256 8bpp file is only 22k insterad of at least 65k.
but my data isnt compressed,
wich is probably why its looking weird lol.
Probably not. You can tell for sure by displaying the bitmap before
you save it (which is trivial to do in .NET), but the act of
compressing a bitmap for saving as PNG wouldn't normally change how the
image looks, since decompressing it again to turn it back into a Bitmap
instance would just reverse the original compression.
how can i specify/force it to use .BMP type format rather than png ?
You have to specify a codec in the call to Save(). PNG is the default
that's used when you use a Save() overload that doesn't include an
ImageCodecInfo parameter.

I don't know if there's a better way, but the MSDN samples say that you
have to use ImageCodecInfo.GetImageEncoders() to get an array of
encoders, and then enumerate them to look for the format you want
(checking the MimeType property), using the encoder with that format.

This is the method I use in my programs and it works fine.

Pete

Nov 18 '07 #7

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:2007111718442750073-NpOeStPeAdM@NnOwSlPiAnMkcom...
On 2007-11-17 18:22:53 -0800, "colin" <co*********@ntworld.NOSPAM.com>
said:
>[...]
However the file seems to be saved in .png format,
256x256 8bpp file is only 22k insterad of at least 65k.
but my data isnt compressed,
wich is probably why its looking weird lol.

Probably not. You can tell for sure by displaying the bitmap before you
save it (which is trivial to do in .NET), but the act of compressing a
bitmap for saving as PNG wouldn't normally change how the image looks,
since decompressing it again to turn it back into a Bitmap instance would
just reverse the original compression.
>how can i specify/force it to use .BMP type format rather than png ?

You have to specify a codec in the call to Save(). PNG is the default
that's used when you use a Save() overload that doesn't include an
ImageCodecInfo parameter.

I don't know if there's a better way, but the MSDN samples say that you
have to use ImageCodecInfo.GetImageEncoders() to get an array of encoders,
and then enumerate them to look for the format you want (checking the
MimeType property), using the encoder with that format.

This is the method I use in my programs and it works fine.

Pete
hmm wel ive found the BMP codec, its the first one,
it seems to need a parameter, ive pased null,
but cant find out where the info for this is,
but it produces a .bmp file wich has 'BM' as first 2 bytes.

it stil produces the same weird output however,
but I did manage to debug my directly aproach
and the .bmp file looks fine.

the 2 files look nothing alike whatsoever in a hex viewer.

the colours are weird too, its actually a boring grey rock surface texture,
but the weird one looks like a colourful tartan pattern.

gues il stick with the direct aproach but id be interested in
finding out why this doesnt work.

Colin =^.^=
Nov 18 '07 #8
"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:2007111719362343658-NpOeStPeAdM@NnOwSlPiAnMkcom...
On 2007-11-17 19:20:50 -0800, "colin" <co*********@ntworld.NOSPAM.com>
said:
...
>but it produces a .bmp file wich has 'BM' as first 2 bytes.

it stil produces the same weird output however,
but I did manage to debug my directly aproach
and the .bmp file looks fine.

the 2 files look nothing alike whatsoever in a hex viewer.

the colours are weird too, its actually a boring grey rock surface
texture,
but the weird one looks like a colourful tartan pattern.

Does the palette actually include any color? If the palette is a
general-purpose palette and you know that only the grey colors are
actually used, then it's possible you've some wrong pixel values that are
using the wrong colors in the palette. Alternatively, if the palette
itself is wrong, you could have the right pixel values and still get
colors instead of the grey shades you're expecting.

Also, did you display the bitmap before saving it? Did it look correct
then? Or was it already wrong at that point?
I never actually viewed the bmp file before saving,
at the momnent im just trying to get the file import
working, saving them all to disc and viewing them is
an easy way to see if theres any anomolies,
the boring images had about 15 greyscale values in the 256 color table,
the badly saved images had a full color table full of seemingly random
colours.

also the pixel tsble seemed to bear no relation whatsoever to the value
given to the constructor.

there are other images wich do have plenty of colour and these look pretty
much the same
weird tartan pattern, my direct aproach seems to display all these
beutifully however,
despite they are so dammed dark, but this seems to be a 'feature' of the
game they come from
(unreal), i might play around with the gamma of the color table.

the images will ultimatly be used as textures for a 3d surface.
but this is also an editor so a list will be displayed to be able to select
the desired pattern.

I dont know how the palette is so wrong as im putting in the right vallues
unless im just
getting a copy of the palette to wich my changes are just simply thrown
away.
wich is why i thought i couldnt change the palette in the first place.

but the bitmap data is so wrong too, maybe my pinned array isnt behaving as
I expect.
I use marshaleing elsewhere in the program and I suspect it is cuasing weird
problems,
although it does seem to do what its suposed to.
I may try and avoid unsafe totaly code if i can.

thanks
Colin =^.^=
Nov 18 '07 #9
"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:Wo******************@newsfe3-gui.ntli.net...
however when I display it the colours look odd,
the red and blue seem true, but the green seems to be missing,
evrything has a purpleness to it,
yet it displays fine in explorer/paint,
other images dont seem to display too wel either,
they seem toget far more than just the colour wrong.
ah no problem, id copied one of the tutorials,
and it had a colour filter set lol
works quite wel now :D

Colin =^.^=
Nov 19 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeremy Langworthy | last post by:
Hi I am trying to create a MS Excel format CSV but I can't figure out how to get the line feed/carriage return/new record working properly. I am nding each line/record with these characters:...
1
by: Inpreet | last post by:
Hello I am using: ************************************************************************* header("Content-Type:application/force-download",false); header("Content-Disposition: attachment;...
1
by: dave | last post by:
I first started using HCW.exe to compile .rtf filew created with MS Word a couple of weeks ago. I used the file | new menu then selected New project in the dialog box and everything worked as...
1
by: C-man | last post by:
Basically I have this little program that will look through directories and rename any file it finds. The renaming that takes place is like removing of dashes or Caps the first letter from each...
2
by: George Marsaglia | last post by:
I have a set of, say, 2000 points in the 8-dimensional simplex S={(x_1,x_2,...,x_8),x_1+x_2+...+x_8=1, x's>=0}. To help analyze that 8-dimensional set, I wish to project the set of points onto...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
1
by: MT | last post by:
We have a .NET dll that we are deploying to our customers using a deployment project in VS .NET 2003. There is a lot of information about creating publisher policies on the web, but we have not...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
5
by: Sam777 | last post by:
I was under the impression that creating the app_offline.htm file at the root of the webapp would cause all handles to be closed so that the app could be removed. Unfortunately, this isn't the...
15
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.