• Facebook
  • RSS Feed
  • Instagram
  • LinkedIn
  • Twitter
Oct 072011
 

A very slight change of tack …  I was going to dynamically work out how big to make the picture rectangles, and then add them all with the images that are in the folder.  Very hard, looking at the algorithms out there on the internet.  Maybe that’ll be saved for (another) rainy day.

In the meantime I can see for now that image boxes 160 wide by 270 high seem to be a-okay.  (I should probably make that configurable and stored in a .exe.config at some point).  So what I decided to do was (as a first step for fully dynamic loading) add N images to the form.

I do this as follows :

 

const int imagewidth  = 160;
const int imageheight = 270;
const int startxpos = 20;
const int startypos = 40;
const int buffer = 5;
            
int NumImages = 12; // temporary number for now !

int xcoord = startxpos;
int ycoord = startypos;

for (int i = 1; i <= NumImages; i++)
    {
      AddPictureBox(xcoord, ycoord, imagewidth, imageheight);
       xcoord = xcoord + imagewidth + buffer;
       if (xcoord + imagewidth > this.Width) // Gone past the end; star new row
                { 
                    xcoord = startxpos;
                    ycoord = ycoord + imageheight + buffer;

                    // Do something if we have now gone off the bottom 
                    // Autoscroll is on, on the form, so it's good
                }
     }
            

In there you can see I call a function AddPictureBox, that just does this :

private void AddPictureBox(int xcoord, int ycoord, int imagewidth, int imageheight)
{
   PictureBox pbox1 = new PictureBox();
   pbox1.Location = new Point(xcoord, ycoord);
   pbox1.Size = new System.Drawing.Size(imagewidth, imageheight);
   pbox1.ImageLocation =
       @"C:My Dropboxcode~CodingDocsBackgroundReadingPicture 2.jpg";
   pbox1.SizeMode = PictureBoxSizeMode.StretchImage;
   pbox1.BorderStyle = BorderStyle.FixedSingle;
   this.Controls.Add(pbox1);
}

 

I also changed the AutoResize property on the form in design mode to TRUE.  This means that if you try to add controls beyond the visible boundaries of the form, a scroll bar is automatically added.  My code controls the width, so we shouldn’t ever end up with a horizontal scrollbar, so it should just be height.

I tested it with 8, and 38 as the number of images, and the scroll bar does correctly get added at the right time.

 

All good so far.

 

Modifying the plan somewhat, I think I now need to :-

  • Take as input a folder name
  • Scan that folder for image files (.png, .jpg, .jpeg, .tiff, .png – for now)
  • Add the picture boxes for each image found as I go along

I’ll need to modify the function above to take as input the full path for a file that has been found in the folder that has been selected.. but largely it’s good to go.  Just need to do the scanning code next.

 

    Time Spent So Far : 90 minutes
    References: No new ones

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)