Getting the wrong colors after image manipulation

I am trying to XOR some values ​​with the RGB values ​​of my image, save this image and take steps back to get the original image. The problem is that I do not know why I get an incomprehensible (with some noise) image. Here is my code and 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 saving the image, I change

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

for

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

and delete

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

and I get an image like

enter image description here

(left original, right - the result); As you can see, I get the wrong colors in Figure 4, why? In general, he is close to the original, but still he is wrong

+5
source share
2 answers

Okey, I found a problem. The problem was saving the image.

That helped:

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

I assume that your image does not use 8-bit colors. % 256 assumes you have an 8-bit image.

0
source

Source: https://habr.com/ru/post/1216314/


All Articles