Overview
Recently, while working on a feature for the resource manager background, I needed to pass information to a DLL.
The main task was to pass some simple parameters, including image fit ratio, opacity, and so on. There are multiple communication methods, and since this was a practice feature, I wanted to try some approaches I hadn't used before.
In my previous article, I introduced digital watermarking technology, where two of the methods involved embedding data in image files:
https://www.cnblogs.com/zhaotianff/p/17222056.html
So, I also wanted to directly write this data into the image file and then read it in the DLL.
After checking online, there is indeed a relatively simple way to write custom data into JPG files.
Moreover, this method theoretically allows writing data of unlimited size.
As a side note, no matter what format a file is, it follows a standard format. It generally includes a file header, file body, etc. Once we are familiar with the file structure, writing our own data into the file header without destroying the original file structure is theoretically achievable.
However, this process can take some time.
Here, I directly used the existing approach from NVISO Labs to share with those in need.
Implementation Principle
First, let's open a regular JPG file with a hex editor.

We can see that the first two bytes are FF D8.
FF D8 => This is the marker indicating the start of JPEG data stream.
After finding the start marker of the JPEG data stream, we can insert a "comment" marker right after it.
FF FE => This is a "comment" marker, which JPEG decoders will ignore. These markers are exactly how we will insert data while still having a valid image.
FF FE is followed by 2 bytes representing the size of the "comment" content.
Example
Suppose we write HelloWorld into a jpg file. "HelloWorld" is ten bytes, so after FF FE we write:
00 0A (10 bytes) 48 65 6C 6C 6F 57 6F 72 6C 64
This totals 14 bytes, including FF FE (2 bytes header), 00 0A (2 bytes size), and 10 bytes of content.
So we insert 14 bytes after FF D8 using a hex editor.


Then we fill in the data as shown below.

After saving the file, the JPG file still opens normally.

Code Implementation
Using C# as an example, other languages have similar implementations. Here I wrote opacity and stretch data into the file.
private void WriteImageInfoToFile(string filePath,double opacity,int stretch)
{
//读取文件数据
var buffer = System.IO.File.ReadAllBytes(filePath);
//判断是否是JPG文件
if (buffer[0] == 0xFF && buffer[1] == 0xD8)
{
//将原始数据扩容6个字节
var newBuffer = new byte[buffer.Length + 6];
//拷贝JPG文件开始标记 FF D8
Array.Copy(buffer, 0, newBuffer, 0, 2);
//设置数据
//注释标记
newBuffer[2] = 0xFF;
newBuffer[3] = 0xFE;
//大小 0x02
newBuffer[4] = 0;
newBuffer[5] = 0x02;
//数据
newBuffer[6] = (byte)nOpacity;
newBuffer[7] = (byte)stretch;
//将原图片剩下的数据拷贝到新buffer中
Array.Copy(buffer, 2, newBuffer, 7, buffer.Length - 2);
//写入文件
System.IO.File.WriteAllBytes(filePath, newBuffer);
}
}
When reading, follow the same rules.
References
https://blog.nviso.eu/2020/07/13/how-to-embed-secret-data-in-jpeg-files/