[英]How to place maximum images in a blank Bitmap based on column/row input & margin variables and preserving aspect ratio of images
This might be a tricky question. 这可能是一个棘手的问题。
I will just cut to the chase. 我只会追逐。 First i make a blank Bitmap image:
首先,我制作一个空白的位图图像:
Bitmap MasterImage = new Bitmap(PageSize.Width, PageSize.Height, PixelFormat.Format32bppArgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(MasterImage);
Afterwards: I get a parameter of how much images i want to place in the Blank Bitmap (page) based on columns and rows: 之后:基于列和行,我得到了要在空白位图(页面)中放置多少图像的参数:
int numOfColumns=2; //(could be any value)
int numOfRows=4;
So we should get something like this: 所以我们应该得到这样的东西:
I also get a parameter if i have some margin at top image and left image: 如果我在顶部图像和左侧图像有一定的余量,我也会得到一个参数:
int PagetopMargin=0; //(could be any value)
int PageLeftMargin=0;
Than i have a variable called: imagesPath
which is type of List<String>
it contains full path of images. 比我有一个名为:
imagesPath
的变量,它是List<String>
类型,它包含图像的完整路径。
Now i loop through images: 现在我遍历图像:
while (imagesPath.Count > 0)
{
Image image = Image.FromFile(imagesPath[0]);
//Logic comes here.
//after finishing processing and drawing images in Bitmap i remove image from list
imagesPath.RemoveAt(0);
}
What i am trying to achieve is how to place maximum images in that Bitmap page based on column/row and margin variables and preserving aspect ratio of images. 我想要实现的是如何基于列/行和边距变量以及保留图像的长宽比,在该位图页面中放置最大图像。 (so images wont get distorted).
(因此图像不会失真)。 if some images are left behind, its okey, i will continue next blank Bitmap to place them.
如果遗留了一些图像,这很好,我将继续下一个空白位图来放置它们。 just need to fill the Bitmap blank image to the full.
只需将位图空白图像填满即可。
Here is a function to fit a rectangle into another, either centered or uncentered (aligned top-left): 这是一个函数,可将一个矩形拟合到另一个居中或未居中的矩形(左上对齐):
Rectangle FitToBox(Rectangle scr, Rectangle dest, bool centered)
{
var ratioX = (double)dest.Width / scr.Width;
var ratioY = (double)dest.Height / scr.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(scr.Width * ratio);
var newHeight = (int)(scr.Height * ratio);
if (!centered)
return new Rectangle(0, 0, newWidth, newHeight);
else
return new Rectangle((dest.Width - newWidth) / 2,
(dest.Height - newHeight) / 2, newWidth, newHeight);
}
Its basic math is taken form this post . 它的基本数学取材于此 。
Here is a test bed, centered and uncentered using different random values: 这是一个使用不同随机值居中和未居中的测试台:
Random rnd = new Random();
int cols = rnd.Next(3) + 2;
int rows = rnd.Next(4) + 3;
int w = pan_dest.Width / cols;
int h = pan_dest.Height / rows;
using (Graphics G = pan_dest.CreateGraphics())
{
G.Clear(Color.White);
for (int c = 0; c < cols; c++)
for (int r = 0; r < rows; r++)
{
Rectangle rDest = new Rectangle(c * w, r * h, w, h);
Rectangle rSrc = new Rectangle(0, 0, rnd.Next(200) + 10, rnd.Next(200) + 10);
Rectangle rResult = FitToBox(rSrc, rDest, checkBox1.Checked);
Rectangle rDestPlaced = new Rectangle(c * w + (int)rResult.X,
r * h + rResult.Y, rResult.Width, rResult.Height);
using (Pen pen2 = new Pen(Color.SlateGray, 4f))
G.DrawRectangle(pen2, Rectangle.Round(rDest));
G.DrawRectangle(Pens.Red, rDestPlaced);
G.FillEllipse(Brushes.LightPink, rDestPlaced);
}
}
You would then draw your images like this: 然后,您将像这样绘制图像:
G.DrawImage(someImageimg, rDestPlaced, rSrc, GraphicsUnit.Pixel);
This maximizes the image sizes , not the number of images you can fit on a page, as specified in your comment. 这样可以最大化图像的大小 ,而不是注释中指定的页面上可以容纳的图像数 。 For the latter you should look into something like 2-dimensional packing ..
对于后者,您应该研究二维包装 ..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.