简体   繁体   中英

c# bitmap don't save the way it is

Hi guys I am working on a basic steganography project at my university. I manipulate my bitmap but somewhere there is a bug. I put a breakpoint just to check the values before .save() method and pixels are looking fine but when I save it and open the new image some of the pixels are +-1 bit instate of what was expected. Here is the code:

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;
    }
}

I suspect the problem may be the filetype that you're using. It might be a deception, but the filename in the code is JPG, which is a 'lossy' format. The image will look the same to a human, but will may not contain the exact bits that you saved. Try using a PNG image format.

You would have better separation of concerns by passing in the "string data", rather than "DataSteg k". This would bring it closer to being a MCVE. No need to pass the first parameter by ref. I'd suggest that its a bad design for this method to dispose of a parameter that it has been given. Also, unless I've missed something string t is unused.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM