大家好,我正在我的大学做一个基本的隐写术项目。我操纵我的位图,但是在某个地方有一个错误。我设置了断点来检查 .save() 方法之前的值,像素看起来很好,但当我保存并打开新图像时,一些像素与预期的不同,可能会出现 +-1 个比特。以下是代码:
public class EncryptController
{
public void EncryptMessage(Bitmap oldImage,string imagePath, DataSteg dataSteg)
{
Bitmap newImage = new Bitmap(oldImage.Width, oldImage.Height);
ImageFormat imageFormat = ImageCheck.GetFormat(oldImage);
string data = dataSteg.EncryptData();
int keyLength = data.Length;
int bitmapPos = 0;
for (int i = 0, n = oldImage.Size.Width; i < n; i++)
{
for (int j = 0, z = oldImage.Size.Height; j < z; j++)
{ //data = "010010110110000101101100011000110110100001101111"
if ((Convert.ToInt32(data[bitmapPos].ToString()) != CheckPixelVale(newImage.GetPixel(i, j)))&&keyLength>0)
{
Color newColor = ChangeMaxRate(oldImage.GetPixel(i, j));
newImage.SetPixel(i, j, newColor);
}
else
{
Color newColor = oldImage.GetPixel(i, j);
newImage.SetPixel(i, j, newColor);
}
if (bitmapPos < data.Length-1)
{
keyLength--;
bitmapPos++;
}
}
}
string pt = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\fl\roseEnc.jpg";
oldImage.Dispose();
newImage.Save(pt, imageFormat);
}
private Color ChangeMaxRate(Color color)
{
int red = color.R;
int blue = color.B;
int green = color.G;
if (red + blue + green < 255 * 3)
{
if ((red >= blue && red >= green) && red<255)
{
red++;
}
else if ((blue >= red && blue >= green) & blue < 255)
{
blue++;
}
else if ((green >= blue && green >= red) && green < 255)
{
green++;
}
}
else
{
red--;
}
return Color.FromArgb(red, green, blue);
}
private int CheckPixelVale(Color color)
{
return (color.R + color.B + color.G) % 2;
}
}