How to use a trackbar to scroll through images in picturebox but cant make it

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

    How to use a trackbar to scroll through images in picturebox but cant make it

    I want to do few things with it:

    1. If the user is moving the trackbar the bar it self to the left wich is the start it will stop if he is using the mouse or the mouse wheel or the keyboards it will stop on the most left and when he gets to the most right side it will stop there too.

    2. I want that the user will be able to move the bar in three ways: with keyboards image by image with the mouse pointer drag it from side to side fast and by the nouse scroll button.

    So using the mouse wheel i did a;;ready and the user can scroll throught the images with the mouse wheel. And i did it that only when the user click on the picturebox and its in fullscreen only then he can scroll between the images. Now i want to add the TrackBar.

    So first this is the code of the event when the user click on the picturebox1 and make it on fullscreen:

    Code:
    private void pictureBox1_Click(object sender, EventArgs e)
             {
                 Button picturebox1button;
                 picturebox1button = new Button();
                 this.Controls.Add(picturebox1button);
                 picturebox1button.Click+=new EventHandler(picturebox1button_Click);
     
                if (button_switch == true)
                 {
                     formInitialLocation = this.Location;
                     formInitialSize = this.Size;
                     this.FormBorderStyle = FormBorderStyle.None;
                     this.Bounds = Screen.PrimaryScreen.Bounds;
                     pictureBox1.Dock = DockStyle.Fill;
                     pictureBox1.BringToFront();
                     button_switch = false;
                     this.button_legend_1.Visible = true;
                     this.button_legend_1.Size = Properties.Resources.radar_scale_heb.Size;
                     this.button_legend_1.Image = Properties.Resources.radar_scale_heb;
                     this.button_legend_1.BringToFront();
                     this.button_legend_1.Location = new Point(Screen.GetWorkingArea(this).Width - button_legend_1.Width, 24);
                     this.button_legend_1.Enabled = true;
                     this.button_legend_2.Visible = true;
                     this.button_legend_2.Width = 350;
                     this.button_legend_2.Location = new Point(Screen.GetWorkingArea(this).Width - button_legend_2.Width, button_legend_1.Height+30);
     
                    picturebox1button.Visible = true;
                     picturebox1button.BringToFront();
                     picturebox1button.Location = new Point(Screen.GetWorkingArea(this).Width - 260 , 24+button_legend_1.Height+50);
                     picturebox1button.Enabled = true;
                     picturebox1button.Width = 260;
                     picturebox1button.Text = "Save image as screen size resolution";
     
                    this.trackBar1_radar_images.Visible = true;
                     trackBar1_radar_images.Location = new Point(Screen.GetWorkingArea(this).Width - 250, 24 + button_legend_1.Height + 80);
                     trackBar1_radar_images.Width = 250;
                     trackBar1_radar_images.BringToFront();
                     trackBar1_radar_images.SmallChange = 1;
                     trackBar1_radar_images.LargeChange = 5;
                     trackBar1_radar_images.TickStyle = TickStyle.BottomRight;
                     trackBar1_radar_images.TickFrequency = 1;
                     trackBar1_radar_images.BringToFront();
                     trackBar1_radar_images.Show();
                     if (sf == null)
                     {
                     }
                     else
                     {
                         int it;
                         string Next_File;
                         it = last_image_file(); // ליישם גם על הלויין.
                         Next_File = sf + @"\radar" + it.ToString("D3") + ".jpg";
                         FileInfo myinf = new FileInfo(Next_File);
                         DateTime time = myinf.CreationTime;
                         this.button_legend_2.Text = "Last downloaded file is : " + myinf.Name + " At date : " + time;
                         this.button_legend_2.BringToFront();
                         this.button_legend_2.Enabled = true;
                     }
                 }
                 else
                 {
                     this.FormBorderStyle = FormBorderStyle.FixedSingle;
                     this.Dock = DockStyle.None;
                     pictureBox1.Dock = DockStyle.None;
                     button_switch = true;
                     this.Location = formInitialLocation;
                     this.Size = formInitialSize;
                     this.button_legend_1.Enabled = false;
                     this.button_legend_1.Visible = false;
                     this.button_legend_2.Enabled = false;
                     this.button_legend_2.Visible = false;
                     trackBar1_radar_images.Visible = false;
                 }
             }
    You can see there im using to setup the trackbar1.

    Now the code of the onmousewheel event :
    Code:
     protected override void OnMouseWheel(MouseEventArgs e)
             {
                 trackBar1_radar_images.Scroll+=new EventHandler(trackBar1_radar_images_Scroll);
                     DirectoryInfo dir1 = new DirectoryInfo(sf);
                     file_info_mouse_wheel = dir1.GetFiles("radar*.jpg");
                     file_info_mouse_wheel = file_info_mouse_wheel.OrderBy((b) => b.LastWriteTime).ToArray();
                     DirectoryInfo dir2 = new DirectoryInfo(satellite_dir);
                     file_info_mouse_wheel_satellite = dir2.GetFiles("satellite*.jpg");
                     file_info_mouse_wheel_satellite = file_info_mouse_wheel_satellite.OrderBy((b) => b.LastWriteTime).ToArray();
                     base.OnMouseWheel(e);
                     if (pictureBox1.Dock == DockStyle.Fill)
                     {
                             if (e.Delta > 0)
                             {
                                 file_indexs--;
                                 if (file_indexs <= 0)
                                   file_indexs = file_info_mouse_wheel.Length - 1;
                               /*  trackBar1_radar_images.Minimum = 0;
                                 trackBar1_radar_images.Maximum = file_info_mouse_wheel.Length;
                                 trackBar1_radar_images.Value = file_info_mouse_wheel.Length - 1;*/
     
                                LoadPictureAt(file_indexs);
                             }
                             else if (e.Delta < 0)
                             {
                                 file_indexs++;
                                 if (file_indexs >= file_info_mouse_wheel.Length)
                                     file_indexs = 0;
                               /*  trackBar1_radar_images.Value = file_indexs;
                                 trackBar1_radar_images.Minimum = 0;
                                 trackBar1_radar_images.Maximum = file_info_mouse_wheel.Length;*/
                                 
                                LoadPictureAt(file_indexs);
     
                            }
                     }
                     if (pictureBox2.Dock == DockStyle.Fill)
                     {
                         if (e.Delta > 0)
                         {
                             satellite_file_indexs--;
                             if (satellite_file_indexs <= 0)
                                 satellite_file_indexs = file_info_mouse_wheel_satellite.Length + 1;
     
                            LoadPictureAtSatellite(satellite_file_indexs);
     
                        }
                         else if (e.Delta < 0)
                         {
                             satellite_file_indexs++;
                             if (satellite_file_indexs >= file_info_mouse_wheel_satellite.Length)
                                 satellite_file_indexs = 0;
                             LoadPictureAtSatellite(satellite_file_indexs);
                         }
                     }
             }
     
            private void trackBar1_radar_images_Scroll(object sender, EventArgs e)
             {
                 trackBar1_radar_images.Value = file_indexs;
                 trackBar1_radar_images.Minimum = 0;
                 trackBar1_radar_images.Maximum = file_info_mouse_wheel.Length;
             }
    I tried to use the trackbar1 Scroll event and before it i tried to use the trackbar1 inside where i check the mouse delta e.delta but it dosent work good.

    The next two events im not sure if they are important:
    Code:
    private bool LoadPictureAt(int nIndex)
             {
                 bool bRet = false;
                 if (sf == null)
                 {
                 }
                 else
                 {
                     if (nIndex >= 0 && nIndex < file_info_mouse_wheel.Length)
                     {
                         nindex_radar = nIndex;
                         pictureBox1.Load(file_info_mouse_wheel[nIndex].FullName);
                         bRet = true;
                     }
                 }
                 return bRet;
     
            }
             private bool LoadPictureAtSatellite(int nIndexs)
             {
                 bool aRet = false;
                 if (nIndexs >= 0 && nIndexs < file_info_mouse_wheel_satellite.Length)
                 {
                     nindexs_satellite = nIndexs;
                     pictureBox2.Load(file_info_mouse_wheel_satellite[nIndexs].FullName);
                     aRet = true;
                 }
                 return aRet;
             }
    So the only things i need to do is as i mentioned above with the trackbar1

    First only on picturebox1 then i want to make the same with picturebox2

    When i mean moving the trackbar the bar to the left so its the start so it will show the first image in the index 001 and when its to the mose of the right it will show the last image wich is the last image downloaded.

    So on the most left it will be the oldest image and on the most right the most newest image.

    And if its possible to use/fix/repair/use my code the one i copied/paste here.

    Thanks a lot.
    Last edited by Niheel; Apr 20 '11, 10:50 PM. Reason: spacing
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I'm not sure how to approach this one since you've kind of asked about a lot. I'll dive into the trackbar thing, and if you're still having trouble with the other stuff we can look at that too.

    Your images, they're in a list right? Well, could you use the track bar's value property to control the index in the list, which in turn tells you what image you're displaying?

    I think if I were to do it, that's how I would start. From your code, it looks like you're kind of starting to do that, so can you maybe tie it into the track bar?

    One last thing, and maybe a bit of advice that you can take or leave. I'd consider loading your images at the start (unless there will be many, and large ones at that, or they'll change on disk frequently and you want to see updates) because every time you cycle your image you're going to be loading the image from the file over and over. It's much faster to load it once and show the cached version and it also cuts down on your disk access time. As somewhat mentioned though, that might not be desirable. Like, loading 10gb of photos into memory might not be the way to go if you're browsing a large picture folder ;)

    Comment

    • Chocolade
      New Member
      • Aug 2010
      • 69

      #3
      Gary i couldnt find out how to use the trackbar.value i tried to add something like: this.trackBar1_ radar_images.Va lue = file_indexs according to my code but its giving me error say it should be between minimum and maximum.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Yea, the minimum and maximum are values on the TrackBar object that you set. It looks like the default maximum is 10 and the default minimum is 0. If the value of file_indexs doesn't fit within that range, that's probably why you're getting the error.

        I'd suggest, when you load your image list, set the track bar's maximum to the length of the file list.

        *Edit: You might also want to consider using a ScrollBar, as it might provide a bit nicer of a view than a TrackBar... but ultimately it's up to you :D

        Comment

        Working...