简体   繁体   English

为什么从一个图像读取像素并将像素设置在另一个图像上时,并非所有像素都设置在第二个图像上?

[英]Why when reading pixels from one image and set the pixels over another image not all the pixels are set on the second image?

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:这就是我在 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:图像 resized1 的大小为 512x512 位深度 32 这是图像:

resized image调整大小的图像

This is the base image that the resized1 should be over it.这是 resized1 应该覆盖的基础图像。 This image is size 512x512 72 dpi Bit depth 24此图像尺寸为 512x512 72 dpi 位深度 24

base image基本图像

The result is the image mynewbmp.bmp:结果是图像 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.问题是 setpixel 将其设置在错误的 position 中。在制作新点 (0, 0) 时,代码 0,0 是正确的 position,但这不是我想要的。 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.我希望云读取的像素位于基本图像 clean_radar_image11 的中心周围我将其更改为格式 bmp 并且所有云都显示不在右侧 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.我创建的这张图片现在显示左侧的图像,其中右侧 0,0 处 position 中的云像素是云像素的定位方式。 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 position 中应该如何设置云像素的示例

maybe image2's format is.jpg image,its have not alpha value也许image2的格式是.jpg图像,它没有alpha值

A possible problem is that the images dpi does not match.一个可能的问题是图像 dpi 不匹配。 Try calling SetResolution for both images尝试为两个图像调用SetResolution

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.您应该能够设置任何分辨率,只要它们相同即可,但 96 往往是 windows 上的默认值。

You might also try using DrawImageUnscaled , since that should draw the image according to physical/pixel size.您也可以尝试使用DrawImageUnscaled ,因为它应该根据物理/像素大小绘制图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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