Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#5
I'm making project To save many small images into one large image and I used this code :
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 10-Oct-11 9:03am
Edited 10-Oct-11 9:11am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First, don't use individual pictureboxes - it is not very efficient.
Secondly, why are you doing this:
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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.
  Permalink  
Comments
anonymous.legend at 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)

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 189
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 10 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100