开发者

How to save custom data as a valid jpeg image in C#

开发者 https://www.devze.com 2023-02-08 17:20 出处:网络
I need to store XML data on a server that only accepts jpeg images. I thought of writing my XML data inside a valid jpeg file. After all, other than the jpeg header, the content of the image file is a

I need to store XML data on a server that only accepts jpeg images. I thought of writing my XML data inside a valid jpeg file. After all, other than the jpeg header, the content of the image file is arbitrary data right?

Is it possible to produce a valid jpeg file, but have its "body" filled with custom bytes?

Of course, I also need to be able to decode the custom jpeg file and restore the data.

I'm not familiar with the jpeg file format, so I'd appreciate an explicit example.开发者_JAVA百科


Perhaps just appending the data to a small jpeg will work?

  • Create a small jpeg.
  • Append your (obfuscated/encrypted) XML to the file.
  • Upload to server.

FWIW, you can easily see this works using a Hex editor. Just create a small jpeg and append your xml to the end. Then open it using any image editor.

This is a perfectly valid thing to do to a jpeg file:

Will random data appended to a JPG make it unusable?


Uhmmm it is a strange architecture... but anyway I think this post would be useful: How to Add 'Comments' to a JPEG File Using C# so the proposal is add the data as a metadata of a jpeg blank image.


If you want to add your data as the actually jpeg data, you first create a BitmapSource with BitmapSource.Create and put your data in the buffer parameter. Than use the JpegBitmapEncoder to save it as a jpeg file (an example is here).

However, as far as I know, the .Net jpeg encoder is not lossless (even if you set it's quality to 100%) so you will need a third party library that can encode JPEG lossless.


I don't know of a JPEG specific way but there is a PNG/GIF method to encode arbitrary data and pixels. Check out this post. Some sites allow you to upload PNGs and GIFs renamed to JPEG so you could try that. http://blog.nihilogic.dk/2008/05/compression-using-canvas-and-png.html

He's saving javascript but you could use and text, really.

0

精彩评论

暂无评论...
验证码 换一张
取 消