×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Pick random file - .net 3.5

Pick random file - .net 3.5

Pick random file - .net 3.5

(OP)
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
http://www.amanian.co.uk
http://www.adrian-johnson.com

RE: Pick random file - .net 3.5

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.

RE: Pick random file - .net 3.5

(OP)
Thanks for the code Jason, but I now get an error:

Error    1    'System.Windows.Controls.Image' does not contain a constructor that takes '1' arguments    

on the line:

CODE

return new Image(listOfFileNames[randomNo]);

Adrian Johnson
http://www.amanian.co.uk
http://www.adrian-johnson.com

RE: Pick random file - .net 3.5

(OP)
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
http://www.amanian.co.uk
http://www.adrian-johnson.com

RE: Pick random file - .net 3.5

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.

RE: Pick random file - .net 3.5

Great explanation Jason!

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

----signature below----
I'm pushing an elephant up the stairs

My Crummy Web Page

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close