简体   繁体   English

如何基于列/行输入和边距变量以及保留图像的高宽比将最大图像放置在空白位图中

[英]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.

相关问题 如何调整图像大小而又不影响纵横比 - How to resize the images without disturbing the aspect ratio 如何在不运行Windows运行时保留纵横比的情况下实现压缩变焦? - How implement pinch zoom without preserving aspect ratio in windows runtime? 如何使用DataGridViewImageColumn通过缩放并保持宽高比显示图像? - How to use DataGridViewImageColumn to display images using zoom and also keeping the aspect ratio? 如何基于百万像素和宽高比计算分辨率 - How to calculate resolution based on megapixels and aspect ratio 基于位图输入文件,显示其他三个位图图像,分别显示三个颜色分量 - Based on a bitmap input file , show three other bitmap images showing separately the three color components c# 将图像调整为不同的大小,同时保持纵横比 - c# Image resizing to different size while preserving aspect ratio C#调整图像大小并使其适合正方形,同时保持宽高比 - c# resize image and fit it into the square while preserving aspect ratio 在网格的行和列中添加图像 - Adding Images in Row and Column in Grid 根据文本框输入显示图像 - Display images based on Textbox input 如何缩放具有固定纵横比的多个窗口,以使它们覆盖最大的显示器而不会重叠 - How can I scale multiple windows with fixed aspect ratio so that they cover maximum of monitor without overlapping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM