Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pick random file - .net 3.5

Status
Not open for further replies.

adrianjohnson

Programmer
May 16, 2002
145
GB
I'm trying to write a program which picks up an image at random. Here is some of the code:

Code:
string nextImage = "";
int fileCount = 0;

// Get a count of images within the folder.
DirectoryInfo imageFiles = new DirectoryInfo(Properties.Settings.Default.ScreensaverImagePath);

// For each image extension (.jpg, .bmp, etc.)
foreach (string imageExtension in imageExtensions)
{
 int imageCount = imageFiles.GetFiles(imageExtension).Length;
 fileCount = fileCount + imageCount;
}

// Generate a random number.
Random random = new Random();
int randomNo = random.Next(fileCount);

for (int a = 0; a < fileCount; a++)
{
 if (a == randomNo)
 {
  // Set the image.
                    
 }
}

return nextImage;

It's the section marked "Set the image" where I need to be able to pick up the filename. I tried "Image.FromFile" but it doesn't recognise "FromFile" as it's a .net 2 statement.

Any ideas?

Thanks,

Adrian

Adrian Johnson
 
I would load all the file names into an array and randomly select a file name. then display that file. something like this.
Code:
function string[] LoadImagesFrom(string directoryPath)
{
   return Directory.GetFiles(directoryPath);
}

private Random random = new Random();
function Image SelectARandomImageFrom(string[] listOfImages)
{
   int randomNo = random.Next(listOfFileNames.Length);
   return new Image(listOfFileNames[randomNo]);
}


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Alex. I did try that before, but get the following error message:

The type name 'FromFile' does not exist in the type 'System.Windows.Controls.Image'

Next, I added the System.Drawing dll and the directive, but it opened up a whole host of error messages about it getting confused between Windows.Controls.Image and Drawing.Image !

Now I'm even more confused.

Thanks for the help so far,

Adrian

Adrian Johnson
 
namespace resolution is the problem. example say Namespace 1 and 2 both have a class Foo. the following will not work.
Code:
using Namespace1
using Namespace2

public class SomeClass
{
   public void DoSomething()
   {
      //error, which foo should code use?
      IFoo = new Foo();
   }
}
you have a few choices. reference the full name of the object
Code:
public class SomeClass
{
   public void DoSomething()
   {
      //fully qualtified namespace
      Namespace1.IFoo = new Namespace1.Foo();
   }
}
removed the unecessary namespace
Code:
using Namespace1

public class SomeClass
{
   public void DoSomething()
   {
      //no conflict, only 1 namespace
      IFoo = new Foo();
   }
}
or alias the namespace
Code:
using Fu = Namespace1;
using Namespace2;

public class SomeClass
{
   public void DoSomething()
   {
      //alias clarifies the object/namespace
      Fu.IFoo = new Fu.Foo();
   }
}


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Great explanation Jason!

I didn't even pick up on the "Windows.Controls" taking the place of "Drawing" [blush]

[small]----signature below----[/small]
I'm pushing an elephant up the stairs

My Crummy Web Page
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top