Im using for to extract image to images but its slowy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chocolade
    New Member
    • Aug 2010
    • 69

    Im using for to extract image to images but its slowy

    I have this code in Form1 its pictureBox1 double click event. When i double click on it its openning a new form.
    Now im getting from another class some function im using to extract images from one image on hard disk. I put in the function one image and it return me 15 images wich i extract on hard disk and then im reading the 15 images from hard disk from the new form.

    So the first problem is on Form1 in the double click pictureBox1 event. When i click the new form stuck for 3-5 seconds untill the for is ending. I want to it to be smooth. Second problem is i dont want to extract the images to a directory on hard disk but to memory or something so i can use the images on the new form without reading them from the hard disk.

    This is the code in Form1:

    Code:
    private void pictureBox1_DoubleClick_1(object sender, EventArgs e)
            {
               
    
                    pb1_fs = new Picturebox1_Fullscreen();
                    pb1_fs.WindowState = FormWindowState.Maximized;
                    pb1_fs.Show();
                    pb1_fs.picturebox1(pictureBox1);
                    pb1_fs.FormClosing += new FormClosingEventHandler(pb1_fs_FormClosing);
                    int x;
                    Bitmap image_with_clouds;
                    image_with_clouds = new Bitmap(pictureBox1.Image);
                    Bitmap[] mybitmaps;
                    mybitmaps = (ImagesComparison.get_images_with_clouds(image_with_clouds));
                    for (x = 0; x < mybitmaps.Length; x++)
                    {
                        mybitmaps[x].Save(@"e:\testings\" + x.ToString("D6") + ".jpg");
                    }
                    
            }

    ImagesCompariso n.get_images_wi th_clouds(image _with_clouds) This function contain the 15 images return 15 images. so mybitmaps have the 15 images.


    Now in the new form wich is pb1_fs = new Picturebox1_Ful lscreen(); i mean the new form name is Picturebox1_Ful lscreen

    The code there is:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Picturebox1_Fullscreen : Form
        {
             
            FileInfo[] file_info_mouse_wheel;
            string radar_images_download_directory;
            private FileInfo[] file_info_trackBar2;
    
    
            public Picturebox1_Fullscreen()
            {
                InitializeComponent();
                radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
                DirectoryInfo dir1 = new DirectoryInfo(radar_images_download_directory);
                file_info_mouse_wheel = dir1.GetFiles("*.jpg");
    
                trackBar1.Minimum = 0;
                trackBar1.Maximum = file_info_mouse_wheel.Length - 1;
                trackBar1.Value = file_info_mouse_wheel.Length - 1;
                
              DirectoryInfo dir2 = new DirectoryInfo(@"e:\testings\");
                if (dir2.GetFiles().Length > 0)
                    file_info_trackBar2 = dir2.GetFiles();
                else
                    trackBar2.Enabled = false;
                
    
    
    
               
                
            }
    
            private void splitContainer1_Panel1_DoubleClick(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void splitContainer1_Panel2_DoubleClick(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void pictureBox1_DoubleClick(object sender, EventArgs e)
            {
                this.Close();
                
            }
    
            private bool LoadPictureAt(int nIndex, object c)
            {
    
                bool bRet = false;
    
                if (nIndex >= 0 && nIndex < file_info_mouse_wheel.Length)
                {
                    if (c.Equals(trackBar1))
                        pictureBox1.Load(file_info_mouse_wheel[nIndex].FullName);
                    bRet = true;
                }
                if (nIndex >= 0 && nIndex < file_info_trackBar2.Length)
                {
                    if (c.Equals(trackBar2))
                        pictureBox1.Load(file_info_trackBar2[nIndex].FullName);
                    bRet = true;
                }
    
                return bRet;
    
            }
    
            
    
    
    
    
    
    
            private void trackBar2_Scroll(object sender, EventArgs e)
            {
    
                LoadPictureAt(trackBar2.Value, sender);
                pictureBox1.Refresh();
    
            }
    
            private void trackBar1_Scroll(object sender, EventArgs e)
            {
    
                LoadPictureAt(trackBar1.Value, sender);
                pictureBox1.Refresh();
    
            }
    
    
            public PictureBox picturebox1(PictureBox pb1)
            {
                pictureBox1.Image = pb1.Image;
                return pictureBox1;
            }
    
            public string picturebox1_lastFile(string last_file)
            {
                pictureBox1.Load(last_file);
                return last_file;
            }
        }
    }
    So what i want to do in codes is when i click double click in pictureBox1 if im using the trackbar2 it will move between the 15 images im getting in return and show the 15 images in pictureBox1 of the new form!
    And if im using trackBar1 it will move between the images im getting from Form1 pictureBox1.

    So the part with trackBar1 is ok. The problem is with trackBar2.

    The for that make it slow when the new form is loading show up.
    And the second problem is i want to trackbar2 will get 15 images from the current image in trackbar1 and not like now . now its getting the 15 images from pictureBox1.Ima ge in Form1.


    Thanks.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I don't know how large your images are, but big images take time to process, that could be part of your slow down. Also, I don't know what this line does: imagesCompariso n.get_images_wi th_clouds, so it might also be part of your slow down. If you want to look into what's causing the most work to be done, perhaps look at using a StopWatch object to do the timings. You might also want to consider using a separate thread to load your images, providing delegates to update the form when the load is finished. This way you can do the work of loading the image in another thread and your GUI will operate more smoothly.

    For making your images load into memory and not leave an open handle to the hard drive, you'll actually need to load it from the drive, copy it to a memory stream, load an image from that, then close the file version. Have a look at Image.FromStrea m or even Image.FromHBitm ap.

    Play around with that and see how it goes :)

    Comment

    Working...