C#bitmap.save丢失了一些像素

时间:2014-03-09 12:45:48

标签: c# bitmap jpeg pixels loss

我编写了一个代码来为我的位图图像像素着色

int[,] unClusteredImage = new int[367,158];
Bitmap clusteredImage = new Bitmap(367, 158);
for (int row = 0; row < unClusteredImage.GetLength(0); row++)
                        for (int col = 0; col < unClusteredImage.GetLength(1); col++)
                            if (unClusteredImage[row, col] == 0)
                                clusteredImage.SetPixel(row, col, Color.Red);
                            else if (unClusteredImage[row, col] == 1)
                                clusteredImage.SetPixel(row, col, Color.Blue);
                            else if (unClusteredImage[row, col] == 2)
                                clusteredImage.SetPixel(row, col, Color.Green);
                            else if (unClusteredImage[row, col] == 3)
                                clusteredImage.SetPixel(row, col, Color.Yellow);
                            else if (unClusteredImage[row, col] == 4)
                                clusteredImage.SetPixel(row, col, Color.Black);
                            else if (unClusteredImage[row, col] == 5)
                                clusteredImage.SetPixel(row, col, Color.Orange);
                            else if (unClusteredImage[row, col] == 6)
                                clusteredImage.SetPixel(row, col, Color.Lime);
                            else if (unClusteredImage[row, col] == 7)
                                clusteredImage.SetPixel(row, col, Color.Purple);
clusteredImage.Save("test.jpeg");

问题是,当我使用bitmap.save方法保存位图时,当我在绘画中打开它时会丢失一些像素我注意到我的图像底部没有一些像素

1 个答案:

答案 0 :(得分:3)

您将图像保存为JPEG。

JPG格式是一种有损压缩文件格式。这使得以比BMP更小的尺寸存储照片变得有用。

PNG格式是一种无损压缩文件格式,这使其成为在应用程序中使用的常用选择。

我建议您尝试将其保存为PNG格式,然后检查结果。如果您不关心大小,可以将其保存为Bitmap。

clusteredImage.save("test.png", ImageFormat.Png);