private IEnumerable <SliceRectangle <T> > GetRectangles <T>( SliceRectangle <T> sliceRectangle) { var isHorizontalSplit = sliceRectangle.Width >= sliceRectangle.Height; var currentPos = 0; foreach (var subSlice in sliceRectangle.Slice.SubSlices) { var subRect = new SliceRectangle <T> { Slice = subSlice }; int rectSize; if (isHorizontalSplit) { rectSize = (int)Math.Round(sliceRectangle.Width * subSlice.Size); subRect.X = sliceRectangle.X + currentPos; subRect.Y = sliceRectangle.Y; subRect.Width = rectSize; subRect.Height = sliceRectangle.Height; } else { rectSize = (int)Math.Round(sliceRectangle.Height * subSlice.Size); subRect.X = sliceRectangle.X; subRect.Y = sliceRectangle.Y + currentPos; subRect.Width = sliceRectangle.Width; subRect.Height = rectSize; } currentPos += rectSize; if (subSlice.Elements.Count() > 1) { foreach (var sr in GetRectangles(subRect)) { yield return(sr); } } else if (subSlice.Elements.Count() == 1) { yield return(subRect); } } }
public static Rectangle FillInBackground(SliceRectangle<User> r, string path) { var client = new WebClient(); var byteArray = client.DownloadData(path); var stream = new MemoryStream(byteArray); var image = new Bitmap(stream); var ratioX = (double)r.Width / image.Width; var ratioY = (double)r.Height / image.Height; var ratio = ratioX > ratioY ? ratioX : ratioY; var newHeight = Convert.ToInt32(image.Height * ratio); var newWidth = Convert.ToInt32(image.Width * ratio); var posX = Convert.ToInt32((r.Width - (image.Width * ratio)) / 2); var posY = Convert.ToInt32((r.Height - (image.Height * ratio)) / 2); return new Rectangle(posX, posY, newWidth - 1, newHeight - 1); }
public static IEnumerable <SliceRectangle <T> > GetRectangles <T>(Slice <T> slice, int width, int height) { var area = new SliceRectangle <T> { Slice = slice, Width = width, Height = height }; foreach (var rect in GetRectangles(area)) { // Make sure no rectangle go outside the original area if (rect.X + rect.Width > area.Width) { rect.Width = area.Width - rect.X; } if (rect.Y + rect.Height > area.Height) { rect.Height = area.Height - rect.Y; } yield return(rect); } }