creating a .bmp file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • colin

    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,bp p, 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.ColorPa lette
    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 BITMAPFILEHEADE R,BITMAPINFOHEA DER,etc
    and write those to the file ?

    Colin =^.^=


  • Peter Duniho

    #2
    Re: creating a .bmp file

    On 2007-11-17 15:29:53 -0800, "colin" <colin.rowe1@nt world.NOSPAM.co msaid:
    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,bp p, 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.ColorPa lette
    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:


    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

    Comment

    • colin

      #3
      Re: creating a .bmp file

      "Peter Duniho" <NpOeStPeAdM@Nn OwSlPiAnMk.comw rote in message
      news:2007111716 05488930-NpOeStPeAdM@NnO wSlPiAnMkcom...
      On 2007-11-17 15:29:53 -0800, "colin" <colin.rowe1@nt world.NOSPAM.co m>
      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,b pp, 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.ColorPa lette
      >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:

      >
      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 =^.^=


      Comment

      • colin

        #4
        Re: creating a .bmp file


        "Peter Duniho" <NpOeStPeAdM@Nn OwSlPiAnMk.comw rote in message
        news:2007111716 553727544-NpOeStPeAdM@NnO wSlPiAnMkcom...
        On 2007-11-17 16:32:24 -0800, "colin" <colin.rowe1@nt world.NOSPAM.co m>
        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.Un safeAddrOfPinne dArrayElement(m ip.DataArray.To Array(),0);
        Bitmap bmp = new Bitmap(mip.USiz e, mip.VSize, ((mip.USize+3)/4)*4,
        PixelFormat.For mat8bppIndexed, 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.Ent ries[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 =^.^=


        Comment

        • Peter Duniho

          #5
          Re: creating a .bmp file

          On 2007-11-17 17:44:09 -0800, "colin" <colin.rowe1@nt world.NOSPAM.co msaid:
          [...]
          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

          Comment

          • colin

            #6
            Re: creating a .bmp file

            This is the coode I have so far ...

            IntPtr scan =
            Marshal.UnsafeA ddrOfPinnedArra yElement(mip.Da taArray.ToArray (), 0);
            Bitmap bmp = new Bitmap(mip.VSiz e, mip.USize, mip.VSize ,
            PixelFormat.For mat8bppIndexed, scan);
            int x;
            //System.Drawing. Imaging.ColorPa lette pal = bmp.Palette;
            for (x = 0; x < 256; x++)
            {
            FColor c = palette.Colors[x];
            bmp.Palette.Ent ries[x] = System.Drawing. Color.FromArgb( c.R, c.G, c.B);
            }
            bmp.Save(filena me + 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 =^.^=


            Comment

            • Peter Duniho

              #7
              Re: creating a .bmp file

              On 2007-11-17 18:22:53 -0800, "colin" <colin.rowe1@nt world.NOSPAM.co msaid:
              [...]
              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. GetImageEncoder s() 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

              Comment

              • colin

                #8
                Re: creating a .bmp file


                "Peter Duniho" <NpOeStPeAdM@Nn OwSlPiAnMk.comw rote in message
                news:2007111718 442750073-NpOeStPeAdM@NnO wSlPiAnMkcom...
                On 2007-11-17 18:22:53 -0800, "colin" <colin.rowe1@nt world.NOSPAM.co m>
                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. GetImageEncoder s() 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 =^.^=


                Comment

                • colin

                  #9
                  Re: creating a .bmp file

                  "Peter Duniho" <NpOeStPeAdM@Nn OwSlPiAnMk.comw rote in message
                  news:2007111719 362343658-NpOeStPeAdM@NnO wSlPiAnMkcom...
                  On 2007-11-17 19:20:50 -0800, "colin" <colin.rowe1@nt world.NOSPAM.co m>
                  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 =^.^=


                  Comment

                  • colin

                    #10
                    Re: creating a .bmp file

                    "colin" <colin.rowe1@nt world.NOSPAM.co mwrote in message
                    news:Wo60j.5806 6$Eq.54530@news fe3-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 =^.^=


                    Comment

                    Working...