views:

232

answers:

4

Hello, :)

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());
+1  A: 

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());
Khanzor
thank you for the link. A more detailed explanation has been added to my question (on bottom) :)
baeltazor
thank you very much for this code Khanzor. I've added a few questions to my main question if you're intersted :)
baeltazor
+3  A: 

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.

Tim Jarvis
thank you tim jarvis, may i please ask you some followup questions regarding your answer?
baeltazor
Sure, ask away.
Tim Jarvis
yay thank you! so, with your answer... do I just put this code in a button click event when i want to save an image, and inside another when i want to load an image? it seems so simple, yet i'm a little confused. (maybe because i was expecting a task like this to be so complicated and tedius)??
baeltazor
Sure, as an extension method you would just call it from your Bitmap directly...string insertThis = myBitmap.ToBase64String();///file writing code, Xml or just text whatever, just add this string.
Tim Jarvis
Would you like a short end to end example? send me an email to <myfirstname>@<mylastname>.com.au and I'll knock something up for you
Tim Jarvis
A: 

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

Jeff Youel
A: 

@Tim how can i take the Base64 string and put it into image? thnx

Jason Smythe