jay_t55
jay_t55

Reputation: 11652

Embed Image into own file

I have edited this question to make it easier to understand.

I have an image file and I have to store the image data into an existing file in binary. And when that file gets opened in my program again, that binary data should be somehow read and that image displayed inside a picturebox. How would I go about doing this in C#?

Any help/suggestions much appreciated.

Thank you jase

EDIT:

Because our files are of the following structure:

Control
"Text here"
Location

...And there will be many cases where there are more than one or a few controls in the same file like so:

Label
"This is a label"
23, 44
Label
"This is another label"
23, 64
LinkLabel
"This is a linkLabel"
23, 84

...

I don't know where to place/save the following code:

Maybe inside the file like so...:

Image
"<controlLocationData type="Image">
  <Data>
    Base64 encoded image data here
  </Data>
  <FreeformLocation>60, 40</FreeforLocation>
</controlLocationData>"
60, 40

and then use this code below to save/load and display the image?...

var image = LoadBitMap("My Bitmap");
var stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
string base64Encoded = Convert.ToBase64String(stream.ToArray());

Upvotes: 1

Views: 757

Answers (3)

Tim Jarvis
Tim Jarvis

Reputation: 18815

I would probably be inclined just create a block of base64 text in your file that represents the BMP bits.

Edit:

Looks like you are already on the right track here, I find that with these types of conversions, a couple of extension methods are pretty handy...

public static string ToBase64String(this Bitmap bm)
{
  MemoryStream s = new MemoryStream();
  bm.Save(s, System.Drawing.Imaging.ImageFormat.Bmp);
  s.Position = 0;
  Byte[] bytes = new Byte[s.Length];
  s.Read(bytes, 0, (int)s.Length);
  return Convert.ToBase64String(bytes);
}

public static Bitmap ToBitmap(this string s)
{
  Byte[] bytes = Convert.FromBase64String(s);
  MemoryStream stream = new MemoryStream(bytes);
  return new Bitmap(stream);
}

The format of your text file is no big deal, you just need to be able to index into it for your data, so Xml is a common format, but as I said, its just a case of finding the base64 block that you are after.

Upvotes: 3

Khanzor
Khanzor

Reputation: 5000

Is there any reason why you can't just use a native image format and associate metadata with it?

Have a look at this document on msdn for information about accessing the metadata in an abstracted manner.

I don't understand the purpose of the "Control" section - what is this being used for?

EDIT

As Tim said, it's probably not a bad idea to base64 encode the image, and just surround that information in ... some kind of markup.

If you don't hate angle bracket tax, you could try

<controlLocationData type="Image">
  <Data>
    Base64 encoded image data here
  </Data>
  <FreeformLocation>60, 40</FreeformLocation>
</controlLocationData>

To encode and decode the data, you need to use the Convert.ToBase64String method, something like this

var image = LoadBitMap("My Bitmap");
var stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
string base64Encoded = Convert.ToBase64String(stream.ToArray());

Upvotes: 1

Jeff Youel
Jeff Youel

Reputation: 653

Microsoft Word documents are structured storage files (essentially a file system in a file). MSDN starts to explain it here

Upvotes: 0

Related Questions