Click here to Skip to main content
15,913,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Codeproject,

I have a list of images that I'm rendering, now the length of the array may vary from time to time, but in order to not only use new Bitmap(Width * Images.Length, Height) I'd like to sort all these images in the best possible size for a rectangle.

Does anyone have any idea on how to do this?

Currently my code is:

C#
Bitmap CombinedImages = new Bitmap(MaxWidth * Images.Count, MaxHeight);       

 for (int i = 0; i < Images.Count; i++)
 {
        g.DrawImage(Images[i], MaxWidth * i, 0, Images[i].Width, Images[i].Height);
 }


I'd much rather have a two for loops so I can put them all in a square instead of just a single line.

What I have tried:

I've tried using the modular function:


C#
int bestColumn = Images.Count;

while(Images.Count % 2 != 0)
{
    bestColumn += 1;
}

int Columns = BestColumn / 2;
int Rows = BestColumn / 2;


I'm not sure what to do next.
Posted
Updated 22-Jul-16 8:29am
v5
Comments
Patrice T 22-Jul-16 14:09pm    
Explain in details what you want to do.
I fear you try to build a solution which is wrong or at best very complicated for what is really needed.
Philippe Mori 23-Jul-16 10:11am    
It is very hard to understand your question...

It's quite complicated! Start here: Packing problems - Wikipedia, the free encyclopedia[^] and follow a few of the relevent links.
 
Share this answer
 
Comments
Member 11841791 22-Jul-16 14:13pm    
I don't need a packing solution Griff. Found the solution. Thanks for linking though, gonna read through it later.
Found a working solution for now.

C#
int bestColumn = (int)Math.Ceiling(Math.Sqrt(Images.Count)) * 2;

int column = bestColumn / 2;


C#
for (int i =0; i< column; i++)
{
    for (int j = 0; j < column; j++)
    {
        if(j * column + i >= 0 && j * column + i < Images.Count)
        {
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900