static bool saveText(string path, string text)
{
byte[] bytes = Encoding.Default.GetBytes(text);
Bitmap bm = null;
using (Image img = Image.FromFile(path))
{
bm = new Bitmap(img);
}
if (bm.Width * bm.Height * 3 / 8 < bytes.Length + 4) return false;
bytes = HEADER.Concat(bytes).ToArray();
{
byte[] newBytes = new byte[bm.Width * bm.Height * 3 / 8];
Array.Copy(bytes, newBytes, bytes.Length);
bytes = newBytes;
}
BitArray a = new BitArray(bytes);
BitmapData data = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
for (int y = 0; y < bm.Height; ++y)
{
byte* p = (byte*)data.Scan0.ToPointer() + data.Stride * y;
for (int x = 0; x < bm.Width; ++x)
{
int bitArrayIdx = (bm.Width * y + x) * 3;
if (bitArrayIdx < a.Length) if (a[bitArrayIdx]) p[0] |= 1; else p[0] &= 0xfe;
++bitArrayIdx;
if (bitArrayIdx < a.Length) if (a[bitArrayIdx]) p[1] |= 1; else p[1] &= 0xfe;
++bitArrayIdx;
if (bitArrayIdx < a.Length) if (a[bitArrayIdx]) p[2] |= 1; else p[2] &= 0xfe;
p += 3;
}
}
}
bm.UnlockBits(data);
bm.Save("_" + path);
bm.Dispose();
return true;
}