calling pictures/files from a folder to show in a C# Screensaver

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1503 Views - Last Post: 06 July 2014 - 09:38 AM Rate Topic: -----

#1 yanguu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-July 14

calling pictures/files from a folder to show in a C# Screensaver

Posted 04 July 2014 - 09:32 PM

How do i go around the code for this screensaver..
http://www.dreaminco...3-screen-saver/

I tried some examples from other people's code but it doesn't seem to work. Tried to ask the owner aswell but it doesn't seem that he would be able to reply as he's been inactive.
Is This A Good Question/Topic? 0
  • +

Replies To: calling pictures/files from a folder to show in a C# Screensaver

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6337
  • View blog
  • Posts: 21,754
  • Joined: 05-May 12

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 12:50 AM

Describe how "it doesn't seem to work". Is it failing to compile? Is the screensaver failing to start? Is it giving an error message?
Was This Post Helpful? 0
  • +
  • -

#3 yanguu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-July 14

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 02:21 AM

View PostSkydiver, on 05 July 2014 - 12:50 AM, said:

Describe how "it doesn't seem to work". Is it failing to compile? Is the screensaver failing to start? Is it giving an error message?



I inserted this code in it, but somehow it gives me an error of the type or namespace name 'Directory Info' could not be found.. I was wondering where i should insert this? (I'm new to C# so i don't really know much my way in it yet)
private string getrandomfile2(string path)
    {
        string file = null;
        if (!string.IsNullOrEmpty(path))
        {
            var extensions = new string[] { ".png", ".jpg", ".gif" };
            try
            {
                var di = new DirectoryInfo(path);
                var rgFiles = di.GetFiles("*.*").Where( f => extensions.Contains( f.Extension.ToLower()));
                Random R = new Random();
                file = rgFiles.ElementAt(R.Next(0,rgFiles.Count())).FullName;
            }
            // probably should only catch specific exceptions
            // throwable by the above methods.
            catch {}
        }
        return file;
    }

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 02:45 AM

DirectoryInfo is part of the System.IO namespace, so you need to add using System.IO; to your list of usings.
Was This Post Helpful? 1
  • +
  • -

#5 yanguu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-July 14

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 02:55 AM

View PostJackOfAllTrades, on 05 July 2014 - 02:45 AM, said:

DirectoryInfo is part of the System.IO namespace, so you need to add using System.IO; to your list of usings.



Ahh i see! So that's why!!! Thanks! :D
Now i'm getting a problem/error that says " 'getrandomfile2': cannot declare instance member in static class". But why? I declared the getrandomfile2 as Private String.. why is it getting an error?. I placed it together too in the "Static class Program" section.. It took me a week to figure this out.. Still can't figure it out.. C# is hard :(
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • Unprocessable Entity
  • member icon

Reputation: 6580
  • View blog
  • Posts: 26,728
  • Joined: 12-December 12

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 04:44 AM

Are you studying a book or taking a tutorial? Which one?
Was This Post Helpful? 0
  • +
  • -

#7 yanguu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-July 14

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 04:46 AM

Hi! I am making a screensaver that gets file/pictures from a directory/folder. I wanted that the pictures from the file would be shown. But instead it shows the picture of the Mainform. Why is that? Mind checking my code? And help give me an alternative code for it or another way?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace VCScreensaver
{
    static class Program
    {
       
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                if (args[0].ToLower().Trim().Substring(0, 2) == "/s") 
                {
                   
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    ShowScreensaver();
                    Application.Run();
                }
                else if (args[0].ToLower().Trim().Substring(0, 2) == "/p") 
                {
                 
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm(new IntPtr(long.Parse(args[1]))));
                }
                else if (args[0].ToLower().Trim().Substring(0, 2) == "/c")
                {
                   
                    MessageBox.Show("This screensaver has no options that you can set",
                        "Screensaver",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
                else 
                {
                   
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    ShowScreensaver();
                    Application.Run();
                }
            }
            else
            {
               
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                ShowScreensaver();
                Application.Run();
            }
        }

        
        static void ShowScreensaver()
        {
           
            foreach (Screen screen in Screen.AllScreens)
            {
                
                MainForm screensaver = new MainForm(screen.Bounds);
                screensaver.Show();
            }
        }

        public static void getrandomfile2(string path)
        {

            string file = null;
            if (!string.IsNullOrEmpty(path))
            {
                var extensions = new string[] { ".png", ".jpg", ".gif" };
                try
                {
                    var di = new DirectoryInfo(@"C:\Users\Yang\Pictures");
                    var rgFiles = di.GetFiles(@"C:\Users\Yang\Pictures").Where(f => extensions.Contains(f.Extension.ToLower()));
                    Random R = new Random();
                    file = rgFiles.ElementAt(R.Next(0, rgFiles.Count())).FullName;
                }
                // probably should only catch specific exceptions
                // throwable by the above methods.
                catch { }
            }
           
        }

  
 
     
        }
        
    }


Was This Post Helpful? 0
  • +
  • -

#8 yanguu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-July 14

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 04:51 AM

View Postandrewsw, on 05 July 2014 - 04:44 AM, said:

Are you studying a book or taking a tutorial? Which one?


I am following a tutorial which is the link I posted. Also searched for other tutorials but they only showed the same codes which would only show 1 picture(and it's the mainform but this is not I wanted) when it is executed/run .
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • Unprocessable Entity
  • member icon

Reputation: 6580
  • View blog
  • Posts: 26,728
  • Joined: 12-December 12

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 04:57 AM

Please do not create duplicate topics. Topics merged (post #7).
Was This Post Helpful? 0
  • +
  • -

#10 yanguu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-July 14

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 05:07 AM

View Postandrewsw, on 05 July 2014 - 04:57 AM, said:

Please do not create duplicate topics. Topics merged (post #7).



Sorry i thought they would be different cases..
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 06:46 AM

Quote

cannot declare instance member in static class".


Quote

But why? I declared the getrandomfile2 as Private String..


Do you understand what an *instance* of a class is? Do you understand that objects are instances of classes? There is a tutorial on classes linked in my signature block if you need it.

a *static* class is a single instance, a single 'thing' that cannot be made into multiple instances. The error is telling you that your class is static. But your string variable is being used/created as an instance variable. The two don't mix: one is static the other is an instance.

Someone else asked if you were taking a class. You responded you were making a program. That doesn't answer the question. Are you taking a programming class? Are you trying to teach yourself C# from a book on your own? Or are you just jumping right into making this screen saver with no learning first? Do you think you can start by making this screen saver as your day-one project?

This post has been edited by tlhIn`toq: 05 July 2014 - 06:47 AM

Was This Post Helpful? 2
  • +
  • -

#12 yanguu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-July 14

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 07:32 AM

Yes i am aware, I do know what it is, Thank you for the effort of explaining. Also, I was able to solve the problem myself and wanted to delete that message but i couldn't find the delete button. Sorry about that... Please refer to the next question or post that was merged in this topic and hope you could help me with that instead.. And my english is not good so i do apologize, I am a Freelance Graphic Designer/3D concept artist that just stepped into the new world of PROGRAMMING especially C#.. And What i was trying to mean/say is that I am trying my best to teach myself C# from a book and internet tutorials on my own, maybe i could apply some designs in there... Hope it was clear :)

This post has been edited by andrewsw: 05 July 2014 - 07:37 AM
Reason for edit:: Removed large previous quote, press REPLY

Was This Post Helpful? 0
  • +
  • -

#13 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 08:02 AM

If you don't know anything about C#, start with basics, there are tutorials here on this forum. Also learn about OOP(Object oriented programing).
You could probably use the learning method you used to learn 3D modeling and apply it to C#.

You can't delete messages, once you solved your problem it is here for others to see and learn if they have similar problem. Having someone help you solve the problem and then delete it is in opinion very selfish.

This post has been edited by Michael26: 05 July 2014 - 08:04 AM

Was This Post Helpful? 0
  • +
  • -

#14 yanguu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-July 14

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 08:07 AM

I believe it won't hurt to help on regards my question right?

This post has been edited by tlhIn`toq: 05 July 2014 - 11:19 AM
Reason for edit:: Deleted unneccesary quote of entire question - Dude, we all see it.

Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: calling pictures/files from a folder to show in a C# Screensaver

Posted 05 July 2014 - 11:34 AM

Quote

I am a Freelance Graphic Designer/3D concept artist that just stepped into the new world of PROGRAMMING especially C#

Here is a link to our C# tutorial series. It is a good place to start. Much better than saying "I don't know anything about coding; I think I'll make a program."

C# Learning Series - tutorials here on DIC




Quote

I am a Freelance Graphic Designer/3D concept artist

You should probably be looking at WPF. It is the modern way to make UI. It allows the coder to separate UI creation from back end logic. It also leverages the power of DirectX without having to micromanage the DirectX interaction. The old 1990's WinForms you are working in now is far more rigid and doesn't allow for the cool effects and customization achievable in WPF. Nobody makes new programs in WinForms anymore. There are plenty of legacy applications out there and WinForms coders will be employed for years maintaining them. But new projects aren't started using decades-old technology.




Quote

I was able to solve the problem myself

Congratulations. Well done! Feel fee to post follow-up questions in this same thread in order to preserve continuity.

View Postyanguu, on 05 July 2014 - 09:07 AM, said:

I believe it won't hurt to help on regards my question right?


Did you have another question? I thought you said you solved the issue.

This post has been edited by tlhIn`toq: 05 July 2014 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2