-1

im using visual studio to make a minimal image viewer and i want to be able to open up an image from any directory and use the arrow keys to go through all of the images in that directory. my code so far includes an open image button but i would like to just use the file explorer to go and open up that image then cycle through all of the images in the directory.

    public Form1()
    {
        InitializeComponent();

    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {


    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png, *.gif) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png; *.gif" ;

        if (ofd.ShowDialog()==DialogResult.OK && ofd.FileName.Length > 0)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
            pictureBox1.Image = Image.FromFile(ofd.FileName);
        }
    }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {

    }
  • Could you clarify what you are wanting a little more? As of now it is slightly confusing. Do you want to have a shortcut in windows explorer to start your program; have the program open and scroll through all images in a given directory; or be able to see all images in a directory and choose ones to scroll through? – Lilith Daemon Jun 30 '15 at 20:13
  • Put all image files from that folder into a List! You need to add all extension separately. Use directory.GetFiles ! – TaW Jun 30 '15 at 20:13
  • after making my program the default program i want to be able to open up an image from windows explorer into my program and use the arrow keys to cycle through all images that were in the directory of the image that i opened rootix – Calvin Maxwell Jun 30 '15 at 20:17
  • So something of a similar nature to Windows Photo Viewer? – Lilith Daemon Jun 30 '15 at 20:19
  • pretty much but i really dont like the bloat of the photo viewer and i would like to have support for more filetypes – Calvin Maxwell Jun 30 '15 at 20:20
  • Photoviewer is generally not that resource intensive, but I will see if I can dig up anything for you to look at. – Lilith Daemon Jun 30 '15 at 20:21

2 Answers2

0

Here is an article describing the process of creating a c# program to open a file if it is set as the default program.

http://www.c-sharpcorner.com/UploadFile/71c973/associating-a-file-type-with-an-application-in-C-Sharp/

This is an MSDN article concerning default applications in general that might be useful for you to read: https://msdn.microsoft.com/en-us/library/windows/desktop/cc144160(v=vs.85).aspx (The example is using C++, but the information it provides may be helpful to you. )

Good luck and let me know if this answers your question; and if not what else you need.

EDIT:

I was just thinking about something you might run into... Check out this article concerning the finding of a current default application for a certain file. (If you plan to distribute your software, not all users may desire it automatically setting itself to the default for a given file-type.)

Finding the default application for opening a particular file type on Windows

Community
  • 1
  • 1
Lilith Daemon
  • 1,473
  • 1
  • 19
  • 37
0

Since you need to ask the user to select an directory, you need a FolderBrowserDialog instead of OpenFileDialog which only select file(s).

Directory.GetFiles will allow you to extract list of file under that directory. Note that it doesnt seem to accept more than one extension like "*.jpg|*.bmp"; however, this can be solved via linq :

images = Directory.GetFiles(dialog.SelectedPath, "*", SearchOption.AllDirectories)
        .Where(x => Regex.IsMatch(Path.GetExtension(x), "(jpg|jpeg|...)$"))
        .ToList();

For navigation, you can add a handler to the PreviewKeyDown of the PictureBox and handle left/right arrow or any other key to change pictures.


You will also need something to iterate through the pictures. The easiest for you would be a Timer. Just add an event handler on the Tick event :

private int index; // remember to reset this to 0 each time you change the  folder
private List<string> images;
private void Timer1_Tick(object sender, EventArgs e)
{
    index = (index + 1) % images.Length;
    pictureBox1.Image = Image.FromFile(images[index]);
}
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44