Click here to Skip to main content
15,213,043 members
Rate this:
Please Sign up or sign in to vote.
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:

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:


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...
Rate this:
Please Sign up or sign in to vote.

Solution 1

It's quite complicated! Start here: Packing problems - Wikipedia, the free encyclopedia[^] and follow a few of the relevent links.
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Found a working solution for now.

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

int column = bestColumn / 2;


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

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100