Home > database >  Why when reading pixels from one image and set the pixels over another image not all the pixels are
Why when reading pixels from one image and set the pixels over another image not all the pixels are

Time:01-31

This is the method i'm using to read and set the pixels :

public void ReadSetPixels(Bitmap image1, Bitmap image2)
        {
            int tolerance = 64;
            for (int x = 0; x < image1.Width; x  )
            {
                for (int y = 0; y < image1.Height; y  )
                {
                    Color pixelColor = image1.GetPixel(x, y);
                    // just average R, G, and B values to get gray. Then invert by 255.
                    int invertedGrayValue = 255 - (int)((pixelColor.R   pixelColor.G   pixelColor.B) / 3);
                    if (invertedGrayValue > tolerance) { invertedGrayValue = 255; }
                    // this keeps the original pixel color but sets the alpha value
                    image1.SetPixel(x, y, Color.FromArgb(invertedGrayValue, pixelColor));
                }
            }
            // composite image1 on top of image2
            using (Graphics g = Graphics.FromImage(image2))
            {
                g.CompositingMode = CompositingMode.SourceOver;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.DrawImage(image1, new Point(0, 0));
            }

            image2.Save(@"d:\mynewbmp.bmp");
            image1.Dispose();
            image2.Dispose();
        }

This is how i'm using it in form1 :

RadarPixels rp = new RadarPixels();
            rp.ReadSetPixels(new Bitmap(@"d:\resized1.png"),
                new Bitmap(@"d:\clean_radar_image11.jpg"));

The image resized1 is size 512x512 Bit depth 32 This is the image :

resized image

This is the base image that the resized1 should be over it. This image is size 512x512 72 dpi Bit depth 24

base image

The result is the image mynewbmp.bmp :

result

UPDATE :

i found the problem.

i found the problem but not the solution. the problem is that the setpixel set it in the wrong position. it is the right position by the code 0,0 when making new Point(0, 0) but it's not what i want. iwant that the clouds the readed pixels to be around the center of the base image clean_radar_image11 i changed it to format bmp and all the clouds are show just not in the right position.

This image i created now show the image on the left where the clouds pixels in the position at 0,0 on the right side is how the clouds pixels should be positioned. the right image is just example to show how the clouds should be positioned on the left image.

example of how the clouds pixels should be set in position

CodePudding user response:

maybe image2's format is .jpg image,its have not alpha value

CodePudding user response:

A possible problem is that the images dpi does not match. Try calling SetResolution for both images

image1.SetResolution(96, 96);
image2.SetResolution(96, 96);

You should be able to set any resolution, as long as they are the same, but 96 tend to be the default on windows.

You might also try using DrawImageUnscaled, since that should draw the image according to physical/pixel size.

  • Related