【发布时间】:2014-05-20 02:07:45
【问题描述】:
我先写了两个应用程序: 使用 C# 中的随机类生成具有随机颜色的图像,每个像素的范围从 0 到 255 ARGB 颜色,图像大小为 3000 x 3000 宽度和高度。 第二次申请: 生成具有相同宽度和高度 (3000 x 3000) 的图像,但对于每个像素的 ARGB 颜色的 A 、 R 、 G 、 B 使用 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;
}
}
【问题讨论】:
-
显示在每个应用程序中保存图像的代码 - 24M 感觉就像未压缩的版本。
-
3000 x 3000 x 4 = 36 MB,获得 24 MB 文件是包含随机像素的图像的预期结果。您的第一个应用程序可能没有正确使用 Random 来大量压缩图像,这是标准错误之一。写这样的代码没什么意义,最好继续。
-
答案已用代码更新...!!
-
循环使用
y变化最快会影响您的性能。在这两段代码中。你想要for (y ...) for (x ...)而不是for (x...) for (y...)。 -
有什么区别??!!宽度和高度相同(3000)...... !!!
标签: c# winforms image-processing drawing pixels