Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm making project To save many small images into one large image and I used this code :
C#
private void Openbutton_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            string selected = folderBrowserDialog1.SelectedPath;
            string[] imagefilelist = Directory.GetFiles(selected);

            int wid = 0, hei = 0, med = 0;
            foreach (string imagefile in imagefilelist)
            {
                if (Image.FromFile(imagefile) != null)
                {
                    Image.FromFile(imagefile).Dispose();
                }
                PictureBox eachpicturebox = new PictureBox();
                imgs = eachpicturebox;
                eachpicturebox.Size = new Size(241, 155);
                eachpicturebox.Location = new Point(wid * 241 + med, hei);
                eachpicturebox.Image = Image.FromFile(imagefile);
                wid++;
                if (wid == 3)
                {
                    wid = 0;
                    hei += 155;
                }
                panel1.Controls.Add(eachpicturebox);
            }
        }


This code creates picture box for each image and all picture boxes placed in the panel1.
The problem here is: I want to be able to change the size of small images.

Can anyone help me.
Posted
Updated 10-Oct-11 8:11am
v2

First, don't use individual pictureboxes - it is not very efficient.
Secondly, why are you doing this:
C#
if (Image.FromFile(imagefile) != null)
{
    Image.FromFile(imagefile).Dispose();
}
The if condition reads a file, converts it to a new Image, compares it and then throws away the reference. If it isn't null - which will always be true - it reads it again, creates another new Image and immediately Disposes it.
What do you think that does that is reasonable?

Instead of using PictureBoxes, handle the Paint event for the Panel, and draw the images onto the supplied Graphics object. This will allow you much better control over the placement and size of the images, as well as being considerably quicker.
 
Share this answer
 
Create a user control that loads the images and displays them. Then you can just give the control a directory name that contains the files, and let it do the work for you behind the scense, thus uncluttering your form code.

C#
public class MyControl : Control
{
    private List<image> images = new List<image>();

    public MyControl(){}

    public MyControl(string folder)
    {
        FileInfo[] files = Diretory.GetFiles("*.jpg");
        foreach (FileInfo info in files)
        {
            Image img = Image.FromFile(info.FullName);
            if (img != null)
            {
                images.Add(img);
            }
        }
    }
}</image></image>


At this point, you can do anything you want with the images.
 
Share this answer
 
Comments
anonymous.legend 10-Oct-11 23:01pm    
Thanks So Much , but i have an error here :
FileInfo[] files = Diretory.GetFiles("*.jpg");
The Error : "Cannot Convert String[] to System.IO.FileInfo[]"
And i'm Asking The Resizing Images Code Please , how can i resize an image ??
:( .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900