Home>

I am making an application that replaces each hiragana character typed in C # with a encrypted image.

Therefore, I would like to connect the specified number of images (10 this time) horizontally and then connect them to the second row.

public partial class Form1: Form
{
    Bitmap ArrangeImage (string [] paths)
    {
        List<Image>images = new List<Image>();
        foreach (var path in paths)
        {
            images.Add (Image.FromFile (path));
        }
        int maxHieght = images.Max (x =>x.Height);
        List<Rectangle>rects = new List<Rectangle>();
        int px = 0;
        foreach (var img in images)
        {
            int newHeight = img.Height * maxHieght/img.Height;
            int newWidth = img.Width * maxHieght/img.Height;
            rects.Add (new Rectangle (px, 0, newWidth, newHeight));
            px + = newWidth;
        }
        int width = rects.Sum (x =>x.Width);
        Bitmap bmp = new Bitmap (width, maxHieght);
        Graphics g = Graphics.FromImage (bmp);
        for (int i = 0;i<rects.Count;i ++)
        {
            g.DrawImage (images [i], rects [i]);
        }
        g.Dispose ();
        foreach (var img in images)
        {
            img.Dispose ();
        }
        return bmp;
    }
    private void button1_Click (object sender, EventArgs e)
    {
        List<string>paths = new List<string>();
       for (int i = 0;i<20;i ++) {
        paths.Add (textBox1.Text);
        paths.Add (textBox2.Text);
        paths.Add (textBox3.Text);
 }
        try
        {
            Bitmap bmp = ArrangeImage (paths.ToArray ());
            bmp.Save (@ "D: \ output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            bmp.Dispose ();
        }
        catch
        {
            MessageBox.Show ("Failed");
        }
    }
}


Quoted from this site

In other words, if you connect 10 images to the right in the first column, I want to connect them to the right in the second column from the 11th column.

What should i do?

Please teach.

c#
  • Answer # 1

    I am the administrator of the source site "C # that can be understood even by pigeons".

    Are the vertical and horizontal sizes of the specified number of images to be connected the same?
    If they are the same

    Listpaths = new List();
    paths.Add (@ "image paths");
    paths.Add (@ "image paths");
    // Add as many as you like, 10 or 20

    Image img = Image.FromFile (paths [0]);
    int width = img.Width * 10;
    int height = paths.Count/10 * img.Height;
    if (paths.Count% 10! = 0)
    height + = img.Height;
    img.Dispose ();

    Bitmap bitmap = new Bitmap (width, height);
    // It is assumed that all images are the same size.
    // If not, you have to think of an appropriate value
    Graphics g = Graphics.FromImage (bitmap);

    int cnt = 0;
    foreach (string path in paths)
    {
    Image image = Image.FromFile (path);
    int x = (cnt% 10) * image.Width;
    int y = cnt/10 * image.Height;
    g.DrawImage (image, new Point (x, y));
    image.Dispose ();
    cnt ++;
    }
    g.Dispose ();

    bitmap.Save (@ "Specify the path where you want to save the image", System.Drawing.Imaging.ImageFormat.Png);
    bitmap.Dispose ();

    This is possible. I also checked the operation.

Related articles