简体   繁体   中英

Getting wrong colors after an image manipulations

I am trying to XOR some values with RGB values of my image, save that image and to do back steps, to get an original image. The problem is, that I don't know why I get not clear (with some noise) image. Here is my code, and an image below:

Bitmap original = new Bitmap("D:\\img\\1.jpg");
Bitmap inp_bmp = new Bitmap("D:\\img\\1.jpg");

int width = inp_bmp.Width;
int height = inp_bmp.Height;
Color pixel;

for (int y = 0; y < height; y += 1)
{
    for (int x = 0; x < width; x += 1)
    {
        pixel = inp_bmp.GetPixel(x, y);

        int a = pixel.A;
        int r = (pixel.R ^ (1000))%256;
        int g = (pixel.G ^ (185675))%256;
        int b = (pixel.B ^ (78942))%256;
        inp_bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));


    }
}

pictureBox2.Image = inp_bmp;
pictureBox1.Image = original;
inp_bmp.Save("D:\\img\\4.jpg");

After an image has been saved, I change

Bitmap inp_bmp = new Bitmap("D:\\img\\1.jpg");

for

 Bitmap inp_bmp = new Bitmap("D:\\img\\4.jpg");

and remove

//inp_bmp.Save("D:\\img\\4.jpg");

and I get an image like

在此输入图像描述

(left original, right - result); As you can see, I get some wrong colors at picture 4, why? All in all it is close to original, but still it's not right

Okey, I found the problem. The problem was with saving an image.

This helped:

 inp_bmp.Save("D:\\img\\4.png", System.Drawing.Imaging.ImageFormat.Png); 

I'm guessing your image does not use 8 bit colors. % 256 assumes you have an 8-bit image.

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