Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why i need to wait so long.....

Why i need to wait so long.....

Scheduled Pinned Locked Moved C#
data-structuresdebugging
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V VisualLive

    HI all, i'm trying to populate a panel with an array of picture but when i debug the program is so slow to download the picture in the panel. i have this code snippet :

    public partial class ArtLabel : Form
    {
    private System.Windows.Forms.PictureBox[] imgArray; //Declaring array of PictureBox
    public static string ImageToShow;
    private int NumOfFiles;
    private string[] imgExtension;

        public ArtLabel()
        {
            InitializeComponent();
            panel1.VerticalScroll.Visible = true;
            panel1.VerticalScroll.Enabled = true;
          //  panel1.VerticalScroll.Maximum = 1000;
            panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;
    
        }
    
        private void ArtLabel\_Load(object sender, EventArgs e)
        {
           GetPicture4(@"D:\\\\Music\\\\");
        }
    
        private bool ThumbnailCallback()
        {
            return false;
        }
    
        private void ARR(int cNumber, string exc)
        {
         int Xpos = 8;
            int Ypos = 8;
            Image img;
            Image.GetThumbnailImageAbort myCallback =
            new Image.GetThumbnailImageAbort(ThumbnailCallback);
            imgArray = new System.Windows.Forms.PictureBox\[cNumber\]; // assign number array 
            for (int i = 0; i < cNumber; i++)
            {
                imgArray\[i\] = new System.Windows.Forms.PictureBox(); // Initialize one variable
               if (Xpos > 432) // six images in a line
                {
                    Xpos = 8; // leave eight pixels at Left 
                    Ypos = Ypos + 72;  // height of image + 8
                }
    
                        imgArray\[i\].Left = Xpos;
                        imgArray\[i\].Top = Ypos;
                        imgArray\[i\].Width = 64;
                        imgArray\[i\].Height = 64;
                        imgArray\[i\].Visible = true;
                        imgArray\[i\].SizeMode = PictureBoxSizeMode.StretchImage;
                        img = Image.FromFile(exc);
                        imgArray\[i\].Tag = exc;
                        imgArray\[i\].Click += new System.EventHandler(ClickImage);
                        imgArray\[i\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
                        panel1.Controls.Add(imgArray\[i\]);
                        Application.DoEvents();
                        Xpos = Xpos + 72;  
    
            }
    
        }
    
        private List<stri
    
    L Offline
    L Offline
    Luc 648011
    wrote on last edited by
    #2

    ascotravel wrote:

    Do you have any advice where i wrong in my code?

    yes. 1. it is better to use Image.FromStream() rather than Image.FromFile() since the latter keeps the file locked. This is however not affecting performance AFAIK. 2. it is much better to dispose of all objects you don't need any longer provided their class offers a public Dispose() method. Image class does, so don't just abandon img by assigning a new image to it, first do a if(img!null) img.Dispose(); That will reduce your memory consumption, reducing the work of the garbage collector. 3. the basic idea of thumbnail images is you don't calculate them over and over; e.g. have your program create thumbnail files (with a modified filename), and look for them when loading. That will avoid the file load and the computation. For small images, avoid compressed formats such as JPEG; use BMP or GIF instead. BTW: although some image formats support a built-in thumbnail, these are not always present. Furthermore the quality of GetThumbnailImage() is rumored to be not good. :)

    1 Reply Last reply
    0
    • V VisualLive

      HI all, i'm trying to populate a panel with an array of picture but when i debug the program is so slow to download the picture in the panel. i have this code snippet :

      public partial class ArtLabel : Form
      {
      private System.Windows.Forms.PictureBox[] imgArray; //Declaring array of PictureBox
      public static string ImageToShow;
      private int NumOfFiles;
      private string[] imgExtension;

          public ArtLabel()
          {
              InitializeComponent();
              panel1.VerticalScroll.Visible = true;
              panel1.VerticalScroll.Enabled = true;
            //  panel1.VerticalScroll.Maximum = 1000;
              panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;
      
          }
      
          private void ArtLabel\_Load(object sender, EventArgs e)
          {
             GetPicture4(@"D:\\\\Music\\\\");
          }
      
          private bool ThumbnailCallback()
          {
              return false;
          }
      
          private void ARR(int cNumber, string exc)
          {
           int Xpos = 8;
              int Ypos = 8;
              Image img;
              Image.GetThumbnailImageAbort myCallback =
              new Image.GetThumbnailImageAbort(ThumbnailCallback);
              imgArray = new System.Windows.Forms.PictureBox\[cNumber\]; // assign number array 
              for (int i = 0; i < cNumber; i++)
              {
                  imgArray\[i\] = new System.Windows.Forms.PictureBox(); // Initialize one variable
                 if (Xpos > 432) // six images in a line
                  {
                      Xpos = 8; // leave eight pixels at Left 
                      Ypos = Ypos + 72;  // height of image + 8
                  }
      
                          imgArray\[i\].Left = Xpos;
                          imgArray\[i\].Top = Ypos;
                          imgArray\[i\].Width = 64;
                          imgArray\[i\].Height = 64;
                          imgArray\[i\].Visible = true;
                          imgArray\[i\].SizeMode = PictureBoxSizeMode.StretchImage;
                          img = Image.FromFile(exc);
                          imgArray\[i\].Tag = exc;
                          imgArray\[i\].Click += new System.EventHandler(ClickImage);
                          imgArray\[i\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
                          panel1.Controls.Add(imgArray\[i\]);
                          Application.DoEvents();
                          Xpos = Xpos + 72;  
      
              }
      
          }
      
          private List<stri
      
      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #3

      Replace your ARR method with this:

      private void ARR(int cNumber, string exc)
      {
      int Xpos = 8;
      int Ypos = 8;
      Image img;
      Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
      imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array
      imgArray[cNumber] = new System.Windows.Forms.PictureBox(); // Initialize one variable
      if (Xpos > 432) // six images in a line
      {
      Xpos = 8; // leave eight pixels at Left
      Ypos = Ypos + 72; // height of image + 8
      }

      imgArray\[cNumber\].Left = Xpos;
      imgArray\[cNumber\].Top = Ypos;
      imgArray\[cNumber\].Width = 64;
      imgArray\[cNumber\].Height = 64;
      imgArray\[cNumber\].Visible = true;
      imgArray\[cNumber\].SizeMode = PictureBoxSizeMode.StretchImage;
      img = Image.FromFile(exc);
      imgArray\[cNumber\].Tag = exc;
      imgArray\[cNumber\].Click += new System.EventHandler(ClickImage);
      imgArray\[cNumber\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
      panel1.Controls.Add(imgArray\[cNumber\]);
      Xpos = Xpos + 72;  
      
      }
      

      I've removed the loop, and the Application.DoEvents call. You were effectively creating the same PictureBoxes many times over

      V 1 Reply Last reply
      0
      • 0 0x3c0

        Replace your ARR method with this:

        private void ARR(int cNumber, string exc)
        {
        int Xpos = 8;
        int Ypos = 8;
        Image img;
        Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
        imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array
        imgArray[cNumber] = new System.Windows.Forms.PictureBox(); // Initialize one variable
        if (Xpos > 432) // six images in a line
        {
        Xpos = 8; // leave eight pixels at Left
        Ypos = Ypos + 72; // height of image + 8
        }

        imgArray\[cNumber\].Left = Xpos;
        imgArray\[cNumber\].Top = Ypos;
        imgArray\[cNumber\].Width = 64;
        imgArray\[cNumber\].Height = 64;
        imgArray\[cNumber\].Visible = true;
        imgArray\[cNumber\].SizeMode = PictureBoxSizeMode.StretchImage;
        img = Image.FromFile(exc);
        imgArray\[cNumber\].Tag = exc;
        imgArray\[cNumber\].Click += new System.EventHandler(ClickImage);
        imgArray\[cNumber\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
        panel1.Controls.Add(imgArray\[cNumber\]);
        Xpos = Xpos + 72;  
        
        }
        

        I've removed the loop, and the Application.DoEvents call. You were effectively creating the same PictureBoxes many times over

        V Offline
        V Offline
        VisualLive
        wrote on last edited by
        #4

        Hi Thanks to reply me so fast, for Luc i will follow your advice as i'm a novice i need wise advice..Thanks :) @Computafreak - Thanks to post the code but not luck . When i debug the code in this line code : <pre> imgArray[cNumber] = new System.Windows.Forms.PictureBox(); appear this error:"Index was outside the bounds of the array.". I'm trying to work out it if i can i will post my solution. Thanks a lot for your help. Nice Regards :)

        L 0 2 Replies Last reply
        0
        • V VisualLive

          Hi Thanks to reply me so fast, for Luc i will follow your advice as i'm a novice i need wise advice..Thanks :) @Computafreak - Thanks to post the code but not luck . When i debug the code in this line code : <pre> imgArray[cNumber] = new System.Windows.Forms.PictureBox(); appear this error:"Index was outside the bounds of the array.". I'm trying to work out it if i can i will post my solution. Thanks a lot for your help. Nice Regards :)

          L Offline
          L Offline
          Luc 648011
          wrote on last edited by
          #5

          Hi, If you remove the for loop from the ARR method you have to remove imgArray = new System.Windows.Forms.PictureBox[cNumber]; there as well, and allocate the array at the caller. :)

          V 1 Reply Last reply
          0
          • L Luc 648011

            Hi, If you remove the for loop from the ARR method you have to remove imgArray = new System.Windows.Forms.PictureBox[cNumber]; there as well, and allocate the array at the caller. :)

            V Offline
            V Offline
            VisualLive
            wrote on last edited by
            #6

            Hi Luc, sorry i'm shame for my poor knowledge but i removed the loop For and i followed your advices but i receive always the same error . What i do of wrong??? :^) EDIT : if i remove <code>imgArray = new System.Windows.Forms.PictureBox[cNumber]; and after i debug in this code line

            imgArray[cNumber] = new System.Windows.Forms.PictureBox();

            i receive this error "Object reference not set to an instance of an object." :(

            modified on Saturday, April 11, 2009 2:22 PM

            1 Reply Last reply
            0
            • V VisualLive

              HI all, i'm trying to populate a panel with an array of picture but when i debug the program is so slow to download the picture in the panel. i have this code snippet :

              public partial class ArtLabel : Form
              {
              private System.Windows.Forms.PictureBox[] imgArray; //Declaring array of PictureBox
              public static string ImageToShow;
              private int NumOfFiles;
              private string[] imgExtension;

                  public ArtLabel()
                  {
                      InitializeComponent();
                      panel1.VerticalScroll.Visible = true;
                      panel1.VerticalScroll.Enabled = true;
                    //  panel1.VerticalScroll.Maximum = 1000;
                      panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;
              
                  }
              
                  private void ArtLabel\_Load(object sender, EventArgs e)
                  {
                     GetPicture4(@"D:\\\\Music\\\\");
                  }
              
                  private bool ThumbnailCallback()
                  {
                      return false;
                  }
              
                  private void ARR(int cNumber, string exc)
                  {
                   int Xpos = 8;
                      int Ypos = 8;
                      Image img;
                      Image.GetThumbnailImageAbort myCallback =
                      new Image.GetThumbnailImageAbort(ThumbnailCallback);
                      imgArray = new System.Windows.Forms.PictureBox\[cNumber\]; // assign number array 
                      for (int i = 0; i < cNumber; i++)
                      {
                          imgArray\[i\] = new System.Windows.Forms.PictureBox(); // Initialize one variable
                         if (Xpos > 432) // six images in a line
                          {
                              Xpos = 8; // leave eight pixels at Left 
                              Ypos = Ypos + 72;  // height of image + 8
                          }
              
                                  imgArray\[i\].Left = Xpos;
                                  imgArray\[i\].Top = Ypos;
                                  imgArray\[i\].Width = 64;
                                  imgArray\[i\].Height = 64;
                                  imgArray\[i\].Visible = true;
                                  imgArray\[i\].SizeMode = PictureBoxSizeMode.StretchImage;
                                  img = Image.FromFile(exc);
                                  imgArray\[i\].Tag = exc;
                                  imgArray\[i\].Click += new System.EventHandler(ClickImage);
                                  imgArray\[i\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
                                  panel1.Controls.Add(imgArray\[i\]);
                                  Application.DoEvents();
                                  Xpos = Xpos + 72;  
              
                      }
              
                  }
              
                  private List<stri
              
              L Offline
              L Offline
              Luc 648011
              wrote on last edited by
              #7

              Hi, you need a imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; inside GetPicture4() and none of it inside ARR(). And I strongly suggest you buy a tutorial book on C# and study it. It is the only efficient way to make real progress. :)

              V 1 Reply Last reply
              0
              • L Luc 648011

                Hi, you need a imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; inside GetPicture4() and none of it inside ARR(). And I strongly suggest you buy a tutorial book on C# and study it. It is the only efficient way to make real progress. :)

                V Offline
                V Offline
                VisualLive
                wrote on last edited by
                #8

                I tried this code snippet:

                private List<string> GetPicture4(string Folder)
                {
                imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; // here i add the imaArray
                DirectoryInfo dir = new DirectoryInfo(Folder);
                List<string> str = new List<string>();
                FileInfo[] files = dir.GetFiles("*.jpg", SearchOption.AllDirectories);
                NumOfFiles = files.Count();
                imgExtension = new string[NumOfFiles];

                        for (int i = 0; i &lt; NumOfFiles; i++)
                        {
                            ARR(i, files\[i\].FullName); 
                            str.Add(files\[i\].FullName);
                        }
                        return str;
                
                    }
                

                and after i remode this line

                imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; // here i add the imaArray

                from the Arr Method but i receive always this error "Index was outside the bounds of the array." :doh: Really sorry maybe i misundertood your advice if yes please be patient i try my best. Thanks a lot P.S. i will buy a book as soon as possible,i need improve my English too :)

                L 1 Reply Last reply
                0
                • V VisualLive

                  Hi Thanks to reply me so fast, for Luc i will follow your advice as i'm a novice i need wise advice..Thanks :) @Computafreak - Thanks to post the code but not luck . When i debug the code in this line code : <pre> imgArray[cNumber] = new System.Windows.Forms.PictureBox(); appear this error:"Index was outside the bounds of the array.". I'm trying to work out it if i can i will post my solution. Thanks a lot for your help. Nice Regards :)

                  0 Offline
                  0 Offline
                  0x3c0
                  wrote on last edited by
                  #9

                  Whoops. I missed that one. Follow the instructions at this link

                  1 Reply Last reply
                  0
                  • V VisualLive

                    I tried this code snippet:

                    private List<string> GetPicture4(string Folder)
                    {
                    imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; // here i add the imaArray
                    DirectoryInfo dir = new DirectoryInfo(Folder);
                    List<string> str = new List<string>();
                    FileInfo[] files = dir.GetFiles("*.jpg", SearchOption.AllDirectories);
                    NumOfFiles = files.Count();
                    imgExtension = new string[NumOfFiles];

                            for (int i = 0; i &lt; NumOfFiles; i++)
                            {
                                ARR(i, files\[i\].FullName); 
                                str.Add(files\[i\].FullName);
                            }
                            return str;
                    
                        }
                    

                    and after i remode this line

                    imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; // here i add the imaArray

                    from the Arr Method but i receive always this error "Index was outside the bounds of the array." :doh: Really sorry maybe i misundertood your advice if yes please be patient i try my best. Thanks a lot P.S. i will buy a book as soon as possible,i need improve my English too :)

                    L Offline
                    L Offline
                    Luc 648011
                    wrote on last edited by
                    #10

                    ascotravel wrote:

                    imgArray = new System.Windows.Forms.PictureBox[NumOfFiles];

                    What do you think is the value of NumOfFiles at this point? :|

                    V 1 Reply Last reply
                    0
                    • L Luc 648011

                      ascotravel wrote:

                      imgArray = new System.Windows.Forms.PictureBox[NumOfFiles];

                      What do you think is the value of NumOfFiles at this point? :|

                      V Offline
                      V Offline
                      VisualLive
                      wrote on last edited by
                      #11

                      Okay Luc , I work out it after read an article about array ,i removed the method ARR() and i organize the code in the GetPicture4() creating the right loop and removing the messy code wrote down in past. Thanks for your patience and sorry for my stupidity :-O Have a good work. Nice Regards

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups