I wrote two application first one : Generate image with random colors using Random Class in C# with range from 0 to 255 ARGB Colors for every pixel , the image size is 3000 x 3000 width and height . Second Application : Generate image with the same width and height (3000 x 3000) but using range from 60 to 120 for A , R , G , B of ARGB colors for every pixel ...
First App Generate image with size : 500 KB . Second App Generate Image With Size : 24 MB .
both of them uses PNG as format of the image and 32 bit color depth . i can't understand what is the difference between both of image , and why this differ in image size ?? and what things affect significantly the size of the image ? ........................................................................................................................... Sorry for my bad english .
this is the first app code :
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);
}
second app code :
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;
}
}
This is a (common) mistake as the instances of Ran are created so fast they don't get different seeds. Take it out of the loop to get real (pseudo-)random numbers!
for (int x = 0; x < 3000; x++)
{
for (int y = 0; y < 3000; y++)
{
Random Ran = new Random();
Since many or most pixels will be the same in the first case, the image size is a lot smaller.