I want to create a single large PNG file from a set of PNGs (max 25 files of 4080 x 800 pixels each).
The result that I expect is a single PNG with all stacked images.
I wrote this code:
private void PngCreate(List<FileInfo> imageListRef, string basePathPng, string filenamePng, int counter)
{
if (imageListRef.Count > 0)
{
try
{
List<int> imageHeights = new List<int>();
int width = 0;
int height = 0;
foreach (FileInfo file in imageListRef)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(file.FullName);
width = img.Width;
height += img.Height;
img.Dispose();
}
Bitmap bigImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bigImage);
int y = 0;
foreach (FileInfo file in imageListRef)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(file.FullName);
g.DrawImage(img, new Point(0, y));
y += img.Height;
img.Dispose();
}
g.Dispose();
if (!Directory.Exists(basePathPng))
Directory.CreateDirectory(basePathPng);
bigImage.Save(filenamePng, System.Drawing.Imaging.ImageFormat.Png);
bigImage.Dispose();
}
catch (Exception ex)
{
debugLogger.Debug(ex.Message);
throw ex;
}
}
}
But I run out of memory with the new size of bigImage
(4080 x 20000).
What could I do in this case?
I thought about reducing or compressing the resolution of the single image, but I do not know how to do this.
I do not want to lose the quality of the image.