2

I see there is an area in the bitmap header to store additional information. So if I were to write an image in C#, would it be possible for me to add extra ASCII information in the header?

public void writeToPath(GMapControl form)
{
    if(path == String.Empty || path == null)
        path=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    int width = form.Size.Width;
    int height = form.Size.Height;

    Bitmap bm = new Bitmap(width, height);
    form.DrawToBitmap(bm, new Rectangle(0, 0, width, height));

    bm.Save("C:\SomePath\blah");

}

enter image description here

joce
  • 9,624
  • 19
  • 56
  • 74
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
  • Where in this specification do you see a field for "Additional information"? And it's very likely that you will not be able to do what you want using the `Bitmap` class, as I'm sure that was not intended for byte-by-byte editing of a bitmap file. Instead, you'll likely need to open the bitmap file and manipulate it directly. And might I ask *why* you want to insert data into the header of a bitmap? It sounds like whatever you're trying to achieve could be better implemented using some other solution. – qJake Mar 21 '13 at 19:26
  • @SpikeX Please see the page http://en.wikipedia.org/wiki/BMP_file_format. What would be the 'better' solution. I would like to create a bitmap that my applicatin alone knows it created it. That way i can rescan a directory full of pictures to know that my app made it so i can scan in only those pictures – stackoverflow Mar 21 '13 at 19:27
  • Okay, and where, specifically, are you looking to insert data? What byte offset? How much data? – qJake Mar 21 '13 at 19:29
  • just a little uuid somewhere, any where possible. something that I can identify my bitmaps that my app created. I think at 0006h (2bytes) that will work – stackoverflow Mar 21 '13 at 19:30
  • Okay, so in the actual file, at offsets `0x0006` and `0x0008`, you get two bytes each to put in whatever you want. Open your file as a byte[] and edit those particular bytes in the file after it's been saved. – qJake Mar 21 '13 at 19:34
  • Duplicate of http://stackoverflow.com/questions/1755185/how-to-add-comments-to-a-jpeg-file-using-c-sharp ? The API should be the same for both, I believe. – Ryan Cavanaugh Mar 21 '13 at 19:35
  • I may be 'off' - but have you considered just 'naming' files in a way that could give you a clue that it belongs to you? Unless your files will 'migrate' and could be editing using other apps etc. – NSGaga-mostly-inactive Mar 21 '13 at 19:35
  • @NSGaga Nah. that wont be sufficient for my needs. The end user is free to do whatever he/she wants to the image after its spit out. – stackoverflow Mar 21 '13 at 19:36
  • I thought so. Anyway, Bitmap format doesn't support any metadata (not officially) - I'd take a look at `steganography` and it might be easier way out. e.g. http://stackoverflow.com/questions/397537/adding-an-invisible-image-watermark-in-c – NSGaga-mostly-inactive Mar 21 '13 at 19:50
  • You are actually saving the image in the PNG format, not the BMP format. Good choice, it supports [metadata](http://stackoverflow.com/questions/8113314/does-png-support-metadata-fields-like-author-camera-model-etc) – Hans Passant Mar 21 '13 at 23:17

2 Answers2

0

I believe the easiest way to go about this without delving into the business of BITMAPINFOHEADER is to simply open the file again and edit the particular bytes you are after.

Here's some (untested) code:

using (var stream = File.Open(@"C:\SomePath\blah.jpg", FileMode.Open))
{
    var myBytes = new byte[] { /* put up to 4 bytes here */ };

    stream.Position = 6; //there is some space at 0x006 and 0x0008 for your data
    stream.Write(myBytes, 0, myBytes.Length);
}
Mohammed Hossain
  • 1,319
  • 10
  • 19
0

You can increase the file offset to pixel array by the size of your meta data. Then you put your data just before the pixel array. However, you need to have your own bmp io routines to write and retrieve the extra data chunk.

Simon1X
  • 101
  • 9