5 Replies - 2813 Views - Last Post: 23 December 2008 - 02:39 AM Rate Topic: -----

#1 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Embed JPEG data into a textfile

Post icon  Posted 22 December 2008 - 03:05 AM

Hi everyone

I'm trying to save files for my system to use, which are basically textfiles, which also contain the data for a small jpeg image.

I'm using the Drawing.Image class to save the existing jpegs data to a memory stream which I then set to a byte array...

// create a memory stream for handling this stream
MemoryStream memoryStream = new MemoryStream();
// save this image to stream with encoding
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// set the byte array and initialise
this.JPEGData = new Byte[memoryStream.Length];
// save stream to byte array
this.JPEGData = memoryStream.ToArray();



... and then I use this byte array to return a string of characters representing the bytes so that I can save it as part of a file...

// concatonate string from array
for (Int64 index = 0; index < this.JPEGData.Length; index++)
{
	// get the character for this byte
	returnString += (Char)this.JPEGData[index];
}



...which I sandwedge between two predefined strings ("BEGINGJPEGEMBED" and "ENDJPEGEMBED") so that when reading the file I know where the data begins and ends. However this just doesn't work!

Even if I save the file with no other info, just the jpeg data, it is corrupt. If I dont convert the data to a string of characters then it just appears numerically as bytes "1525372627" for example. However by using the Drawing.Image.Save() method I can save correctly, but as you can see this is no good for me as I need to put the data into my own file, not have the jpeg saved seperately.

Hope someone can shed some light on this :)

Is This A Good Question/Topic? 0
  • +

Replies To: Embed JPEG data into a textfile

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Embed JPEG data into a textfile

Posted 22 December 2008 - 04:30 AM

Honestly, I'd use a DataSet for this. At the very least an Xmldocument. However, if you must roll your own, check out Base 64 encoding.

You problem is that values like 10 and 13 are characters, so is all of binary. Binary is just a bunch of bytes, which can be seen as ascii. It's just that some ascii makes it real hard to parse. Base64 is what some XML messaging formats use. It's basically characters represented in such a way that a text parser doesn't choke.
Was This Post Helpful? 0
  • +
  • -

#3 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Embed JPEG data into a textfile

Posted 22 December 2008 - 05:43 AM

Hi, thanks for reply, that makes a lot of sense, I've tried encoding to Base 64 this works ok except that it spazzes on characters that are a not alphanumeric or simple or common - for example ascii character 240a and appear as squares when I try and decode from Base 64, where as the numbers and letters decode fine.

I had a look at Dataset and XML files, bit confused by it all, would I be using the Dataset class to greate a table off all my data then export this to an XML file?

This kinda stuff is all pretty new to me

Thanks for your time

This post has been edited by ragingben: 22 December 2008 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 beatles1692  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 62
  • Joined: 03-December 08

Re: Embed JPEG data into a textfile

Posted 22 December 2008 - 07:07 AM

Hi
I have the same problem once (I wanted to save an image as a text to a xml file).
Here is what I did:
1.Serialize the image using a binaryformatter into a memorystream
2.Convert the buffer of memorystream into string (using Base64 string)
3.Add the string to my xml file.
When I wanted to recreate the image I got its string from xml file,converted it to bytes array and deserialized it and there I had the image again.
Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Embed JPEG data into a textfile

Posted 22 December 2008 - 09:13 AM

View Postragingben, on 22 Dec, 2008 - 06:43 AM, said:

it spazzes on characters that are a not alphanumeric


Odd. Wrote a quick test, all values should work:
byte[] bytes = new byte[256];
for (int i = 0; i <= 255; i++) { bytes[i] = (byte)i; }
Debug.WriteLine(bytes);
string s = System.Convert.ToBase64String(bytes);
Debug.WriteLine("---" + s + "---");

byte[] byteBack = System.Convert.FromBase64String(s);
// validate
for (int i = 0; i <= 255; i++) {
	if (byteBack[i] != i) { throw new System.Exception("bad byte " + i); }
}





View Postragingben, on 22 Dec, 2008 - 06:43 AM, said:

I had a look at Dataset and XML files, bit confused by it all, would I be using the Dataset class to greate a table off all my data then export this to an XML file?


Yep, you got it. A DataSet if kind of a catch all object. Used extensively in manipulating all kinds of data, it's like XML+. The GUI in Visual Stdio allows you to make custom datasets reasonably easily. However, the prior post is probably a better idea for you.

Take the data you want to save and create a class to hold that data. Then just serialize and deserialize that object.
Was This Post Helpful? 1
  • +
  • -

#6 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Embed JPEG data into a textfile

Posted 23 December 2008 - 02:39 AM

YOU LITTLE BEATUIES!!!

Thanks so much guys I've got it reading and saving to a text file, and now reading it back and it pops up nicely as a JPEG!!!

BACK OF THE NET

Thank you so much, I'll post my code when I've dusted it a bit for anyone else whos interested
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1