How to invoke Load_form function without using button

64 views Asked by At

I am trying to create a GUI that shows small 3 images on the app. When there is a new image in the folder, the new image has to be on the right side of the imageBoxes. In order to do that, I am using a timer to invoke Load_Form function. The interval is 1000ms. However after some time ( approximately 3:20 min), the program gives an exception "Out of memory" or "Parameter is not valid". Is there another way to update imageboxes automatically( when there is new image, update it)? The code is here, thank you in advance:

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

 namespace test1
 {
    public partial class Form1 : Form
       {
        string actCode = null;
        string pathLogNames = @"D:\LOG\Right";
        string[] imPaths;

    public Form1()
    {
        InitializeComponent();
        //timer1.Start();
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }
    string[] getFiles(string path, string text, string fileExtension)
    {
        try
        {
            string searchingText = text;
            searchingText = "*" + text + "*";
            string[] filesArray = Directory.GetFiles(path, searchingText, SearchOption.AllDirectories);
            List<string> filesList = new List<string>(filesArray);
            List<string> newFilesList = new List<string>();
            foreach (string file in filesList)
            {
                if (file.Contains(fileExtension) == true)
                {
                    newFilesList.Add(file);
                }
            }
            string[] files = newFilesList.ToArray();
            return files;
        }
        catch
        {
            string[] files = new string[0];
            return files;
        }

    }

  

    string[] viewerPaths;
    int pathsNumber;
    Image actImage;
    Image actImage1;
    Image actImage2;
    //int actIndex;
    double imageDefaultZoom;
    public bool mouseFlag = false;
    //Point startPoint = new Point();

    public void setPaths(string[] paths, double defZoom)
    {
        viewerPaths = paths;
        pathsNumber = viewerPaths.Length;
        imageDefaultZoom = defZoom;
        int total = viewerPaths.Length;
        if (pathsNumber > 0)
        {
            actImage = Image.FromFile(viewerPaths[total - 3]);
            Bitmap b = new Bitmap(actImage);
            Image i = resizeImage(b, new Size(100, 100));
            pictureBox1.Image = i;

            actImage1 = Image.FromFile(viewerPaths[total - 2]);
            Bitmap b1 = new Bitmap(actImage1);
            Image i1 = resizeImage(b1, new Size(100, 100));
            pictureBox2.Image = i1;

            actImage2 = Image.FromFile(viewerPaths[total - 1]);
            Bitmap b2 = new Bitmap(actImage2);
            Image i2 = resizeImage(b2, new Size(100, 100));
            pictureBox3.Image = i2;
        }
    }

    private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
    {
        //Get the image current width  
        int sourceWidth = imgToResize.Width;
        //Get the image current height  
        int sourceHeight = imgToResize.Height;
        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;
        //Calulate  width with new desired size  
        nPercentW = ((float)size.Width / (float)sourceWidth);
        //Calculate height with new desired size  
        nPercentH = ((float)size.Height / (float)sourceHeight);
        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;
        //New Width  
        int destWidth = (int)(sourceWidth * nPercent);
        //New Height  
        int destHeight = (int)(sourceHeight * nPercent);
        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((System.Drawing.Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        // Draw image with new width and height  
        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();
        return (System.Drawing.Image)b;
    }
    private void onChanged(object sender, FileSystemEventArgs e)
    {
        //Dispose();
        imPaths = getFiles(pathLogNames, actCode, ".tif");
        setPaths(imPaths, 0.5);
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        
        imPaths = getFiles(pathLogNames, actCode, ".tif");
        setPaths(imPaths, 0.5);
        
        }
    }
 }


}
0

There are 0 answers