adding multiple images next to each other in the same cell

Dec 22, 2009 at 6:42 PM

I am using MultiImages to display a variable number of images in a cell.

This can change in each column from 1-4 images based on some other criteria.

I would like the images to display next to each other in a single file and can't seem to figure out what to do.

Currently, I am using the AnchorArea and the images are placed on top of each other.  I can't just align them to "MiddleLeft", "MiddleCenter", and "MiddleRight" as there might be up to 4 images.

Here is my code so far.  Please let me if you have any suggestions.  Thanks.

 

 

                int currentImage = 0;
                grid1[r, ctr] = new SourceGrid.Cells.Cell("");
                SourceGrid.Cells.Views.MultiImages modelMultiImages = new SourceGrid.Cells.Views.MultiImages();
                if (Condition1)
                {
                    modelMultiImages.SubImages.Add(new DevAge.Drawing.VisualElements.Image(Properties.Resources.image1));
                    modelMultiImages.SubImages[currentImage].AnchorArea = new DevAge.Drawing.AnchorArea(DevAge.Drawing.ContentAlignment.MiddleCenter, false);
                    currentImage++;
                }
                if (Condition2)
                {
                    modelMultiImages.SubImages.Add(new DevAge.Drawing.VisualElements.Image(image2));
                    modelMultiImages.SubImages[currentImage].AnchorArea = new DevAge.Drawing.AnchorArea(DevAge.Drawing.ContentAlignment.MiddleRight, false);
                    currentImage++;
                }
                if (Condition3)
                {
                    modelMultiImages.SubImages.Add(new DevAge.Drawing.VisualElements.Image(image3));
                    modelMultiImages.SubImages[currentImage].AnchorArea = new DevAge.Drawing.AnchorArea(DevAge.Drawing.ContentAlignment.MiddleLeft, false);
                    currentImage++;
                }
                if (Condition4)
                {
                    modelMultiImages.SubImages.Add(new DevAge.Drawing.VisualElements.Image(image4));
                    modelMultiImages.SubImages[currentImage].AnchorArea = new DevAge.Drawing.AnchorArea(DevAge.Drawing.ContentAlignment.MiddleLeft, false);
                    currentImage++;
                }
                modelMultiImages.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
                grid1[r, ctr++].View = modelMultiImages;

 

 

Coordinator
Dec 28, 2009 at 12:38 PM

No idea :)

Dec 28, 2009 at 1:56 PM

I found a solution that works.  Rather than adding the images individually, it combines them into one image and places it into the cell.  I have all of the images in my resources in my project, so I used that method to reference the images.  You can easily do this by adding files from a directory too.

Here's my code:

 

int currentImage = 0;
grid1[r, ctr] = new SourceGrid.Cells.Cell("");
List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
System.Drawing.Bitmap finalImage = null;
int width = 0;
int height = 0;

SourceGrid.Cells.Views.MultiImages modelMultiImages = new SourceGrid.Cells.Views.MultiImages();
if (condition1)
{
	width += Properties.Resources.image1.Width;
	height = Properties.Resources.image1.Height > height ? Properties.Resources.image1.Height : height;
	images.Add(Properties.Resources.image1);
}
if (condition2)
{
	width += Properties.Resources.image2.Width;
	height = Properties.Resources.image2.Height > height ? Properties.Resourcesimage2.Height : height;
	images.Add(Properties.Resources.image2);
}
if (condition3)
{
	width += Properties.Resources.image3.Width;
	height = Properties.Resources.image3.Height > height ? Properties.Resources.image3.Height : height;
	images.Add(Properties.Resources.image3);
}
if (condition4)
{
	width += Properties.Resources.image4.Width;
	height = Properties.Resources.image4.Height > height ? Properties.Resources.image4.Height : height;
	images.Add(Properties.Resources.image4);
}
if (condition5)
{
	width += Properties.Resources.image5.Width;
	height = Properties.Resources.image5.Height > height ? Properties.Resources.image5.Height : height;
	images.Add(Properties.Resources.image5);
}
if (condition6)
{
	width += Properties.Resources.image6.Width;
	height = Properties.Resources.image6.Height > height ? Properties.Resources.image6.Height : height;
	images.Add(Properties.Resources.image6);
}

if (width > 0 && height > 0)
{
	finalImage = new System.Drawing.Bitmap(width, height);

	//get a graphics object from the image so we can draw on it
	using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
	{
		//set background color
		g.Clear(System.Drawing.Color.Black);

		//go through each image and draw it on the final image
		int offset = 0;
		foreach (System.Drawing.Bitmap image in images)
		{
			g.DrawImage(image,
			  new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
			offset += image.Width;
		}
	}

	modelMultiImages.SubImages.Add(new DevAge.Drawing.VisualElements.Image(finalImage));
	modelMultiImages.SubImages[currentImage].AnchorArea = new DevAge.Drawing.AnchorArea(DevAge.Drawing.ContentAlignment.MiddleLeft, false);
}
grid1[r, ctr++].View = modelMultiImages;
modelMultiImages.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;