0

How created a simple slideshow with a PictureBox element. Now I would like to open an image by clicking on it to see it in full resolution - with the default app which is associated with .jpg files (e.g. Microsoft Photos). I think a click event on the PictureBox would be the way to go but I don't know what code to add. Couldn't find a good example on the internet.

Following the code I use so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Diashow
{
    public partial class Form1 : Form
    {
        private string[] folderFile = null;
        private int selected = 0;
        private int begin = 0;
        private int end = 0;

    public Form1()
    {
        InitializeComponent();
        this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        folderBrowserDialog1.SelectedPath = @"E:\MiscTest\";
        timer1.Interval = 5000;
    }

    private void btnOpen_Click(object sender, EventArgs e)
    {
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            string[] part1 = null, part2 = null, part3 = null;
            part1 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.jpg");
            part2 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.jpeg");
            part3 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.tif");
            folderFile = new string[part1.Length + part2.Length + part3.Length];
            Array.Copy(part1, 0, folderFile, 0, part1.Length);
            Array.Copy(part2, 0, folderFile, part1.Length, part2.Length);
            Array.Copy(part3, 0, folderFile, part1.Length + part2.Length, part3.Length);
            selected = 0;
            begin = 0;
            end = folderFile.Length;
            showImage(folderFile[selected]);

            btnPrevious.Enabled = true;
            btnNext.Enabled = true;
            bntSlideshow.Enabled = true;
        }
    }

    private void showImage(string path)
    {
        Image imgtemp = Image.FromFile(path);
        pictureBox1.Image = imgtemp;

    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        if (selected == folderFile.Length - 1)
        {
            selected = 0;
            showImage(folderFile[selected]);
        }
        else
        {
            selected = selected + 1; showImage(folderFile[selected]);
        }
    }

    private void btnPrevious_Click(object sender, EventArgs e)
    {
        if (selected == 0)
        {
            selected = folderFile.Length - 1;
            showImage(folderFile[selected]);
        }
        else
        {
            selected = selected - 1; showImage(folderFile[selected]);
        }
    }

    private void nextImage()
    {
        if (selected == folderFile.Length - 1)
        {
            selected = 0;
            showImage(folderFile[selected]);
        }
        else
        {
            selected = selected + 1; showImage(folderFile[selected]);
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (pictureBox1.Image != null)
        {
            pictureBox1.Image.Dispose();
        }
        nextImage();
    }

    private void bntSlideshow_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled == true)
        {
            timer1.Enabled = false;
            bntSlideshow.Text = "START Slideshow";
        }
        else
        {
            timer1.Enabled = true;
            bntSlideshow.Text = "STOP Slideshow";
        }
    }
    }
}
rs232
  • 1,248
  • 8
  • 16
Paul Smith
  • 499
  • 2
  • 6
  • 17
  • 1
    this may be of some help : https://stackoverflow.com/questions/11365984/c-sharp-open-file-with-default-application-and-parameters –  Sep 13 '18 at 10:13
  • @Orangesandlemons Thank you. Process.Start(@"C:\MyPicture.jpg"); would work but how do I get the filename of the current displayed image into my click event? – Paul Smith Sep 13 '18 at 11:42
  • the same way you get the image to display in the picturebox –  Sep 13 '18 at 11:50
  • @Orangesandlemons GREAT, thank you! – Paul Smith Sep 13 '18 at 12:04

0 Answers0