两个宽度和高度相同的图像,第一个大小为500KB&;第二个大小是24MB,为什么

本文关键字:500KB 小为 第一个 amp 为什么 24MB 第二个 图像 两个 高度 | 更新日期: 2023-09-27 17:57:26

我写了两个应用程序,第一个:使用C#中的随机类生成具有随机颜色的图像,每个像素的颜色范围从0到255 ARGB,图像大小为3000 x 3000宽和高。第二次申请:生成具有相同宽度和高度(3000 x 3000)的图像,但每个像素的ARGB颜色范围从60到120。。。

第一个应用程序生成的图像大小:500 KB。第二个应用程序生成图像大小:24 MB。

两者都使用PNG作为图像格式和32位颜色深度。我不明白这两个图像之间的区别是什么,为什么图像大小不同??哪些因素会显著影响图像的大小?。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。抱歉我英语不好。

这是第一个应用程序代码:

public void GenerateImage2()
    {
        Bitmap Img = new Bitmap(3000, 3000, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        LockBitmap LBM = new LockBitmap(Img);
        LBM.LockBits();
        for (int x = 0; x < 3000; x++)
        {
            for (int y = 0; y < 3000; y++)
            {
                Random Ran = new Random();
                Color C = Color.FromArgb(Ran.Next(0, 255), Ran.Next(0, 255), Ran.Next(0, 255), Ran.Next(0, 255));
                LBM.SetPixel(x, y, C);
            }
        }
        LBM.UnlockBits();
        Img.Save("redandrandom.png", System.Drawing.Imaging.ImageFormat.Png);
    }

第二个应用程序代码:

GC.Collect();
                int XDim = 0;
                int YDim = 0;
    int ImageDimentions = 3000;
int ForloopRange = ImageDimentions * ImageDimentions;
                Color CurrentColor = Color.Empty;
                Bitmap Btm = new Bitmap(ImageDimentions, ImageDimentions);
                LockBitmap Img = new LockBitmap(Btm);
                Img.LockBits();
                Random Rand = new Random();
                for (int i = 0; i < ForloopRange; i++)
                {
                    CurrentColor = Color.FromArgb(Rand.Next(0, 255), Rand.Next(0, 255), Rand.Next(0, 255), Rand.Next(0, 255));
                    Img.SetPixel(XDim, YDim, CurrentColor);
                    YDim += 1;
                    if (YDim == ImageDimentions)
                    {
                        XDim += 1;
                        YDim = 0;
                    }
                    if (XDim == ImageDimentions)
                    {
                        Img.UnlockBits();
                        Btm.Save(SavedFileName + ".png", ImageFormat.Png);
                        return;
                    }
                }

两个宽度和高度相同的图像,第一个大小为500KB&;第二个大小是24MB,为什么

这是一个(常见)错误,因为Ran的实例创建得太快了,它们不会得到不同的种子。把它从循环中取出,得到实(伪)随机数!

    for (int x = 0; x < 3000; x++)
    {
        for (int y = 0; y < 3000; y++)
        {
            Random Ran = new Random();

由于在第一种情况下,许多或大多数像素都是相同的,因此图像大小要小得多。

PNG是压缩格式。其中一个压缩得比另一个好。