CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    viewing images in a picturebox P2

    I have some code that some one help me with. Its just that theres a new part that I added to my software. And the code I have doesn't want to work right.

    What I'm trying to do is, I have whats called a Terrain. The Terrain has 3 parts (Images) A color image. B&W image and a 3D image.
    C = Color
    D = 3d
    H = B&W

    I have a 3 buttons that says C H D. What I want to do is scroll threw the color images and when I come to a Terrain that I want to view the D and H images. I just click the D and H buttons.
    The problem is that they roll back one (1) or two (2) images, So you not really viewing the right image.

    The code I have is below. I tried to explain this as best as I can.
    VC# 2008 in NET 3.5.

    I have a textbox1 that displays the file name. with a C D H buttons and a Next and Prev Bottons on one (1) form.

    Code:
     public partial class Terrain : Form
        {
            public Terrain()
            {
                InitializeComponent();
            }
    
            public List<string> list;
            public int count;
            public int index = 0;
    
            private void Terrain_Load(object sender, EventArgs e)
            {
                string[] fl = Directory.GetFiles(@"images\ViewC\");
                list = new List<string>();
                foreach (string str in fl)
                {
                    if (str.EndsWith(".jpg"))
                    {
                        list.Add(str);
                    }
                }
                count = list.Count;
                if (count != 0)
                {
                   this.pictureBox1.ImageLocation = list[0];
                  index = 0;
                   textBox1.Text = list[0];
               }
            }
    
            private void btnC_Click(object sender, EventArgs e)
            {
                string[] fl = Directory.GetFiles(@"images\ViewC\");
                list = new List<string>();
                foreach (string str in fl)
                {
                    if (str.EndsWith(".jpg"))
                    {
                        list.Add(str);
                    }
                }
                count = list.Count;
                if (count != 0)
                {
                    this.pictureBox1.ImageLocation = list[0];
                    index = 0;
                }
            }
    
            private void btnNext_Click(object sender, EventArgs e)
            {
                if (count > 1)
                {
                    if (index == 0)
                    {
                        index = count - 1;
                    }
                    else
                    {
                        index--;
                    }
    
                }
    
                this.pictureBox1.ImageLocation = list[index];
                //textBox1.Text = 
                textBox1.Text = list[index];
            }
    
            private void btnPrev_Click(object sender, EventArgs e)
            {
                if (count > 1)
                {
                    if (index == count - 1)
                    {
                        index = 0;
                    }
                    else
                    {
                        index++;
                    }
                }
    
    
                this.pictureBox1.ImageLocation = list[index];
                //textBox1.Text = 
                textBox1.Text = list[index];
            }
    
            private void btnD_Click(object sender, EventArgs e)
            {
                string[] fl = Directory.GetFiles(@"images\ViewD\");
                list = new List<string>();
                foreach (string str in fl)
                {
                    if (str.EndsWith(".jpg"))
                    {
                        list.Add(str);
                    }
                }
                count = list.Count;
                if (count != 0)
                {
                    this.pictureBox1.ImageLocation = list[0];
                    index = 0;
                }
    
            }
    
            private void btnH_Click(object sender, EventArgs e)
            {
                string[] fl = Directory.GetFiles(@"images\ViewH\");
                list = new List<string>();
                foreach (string str in fl)
                {
                    if (str.EndsWith(".jpg"))
                    {
                        list.Add(str);
                    }
                }
                count = list.Count;
                if (count != 0)
                {
                    this.pictureBox1.ImageLocation = list[0];
                    index = 0;
                }
            }
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  2. #2
    Join Date
    Feb 2009
    Location
    Atlanta, GA
    Posts
    17

    Re: viewing images in a picturebox P2

    What do you mean by "it doesnt work right"?
    Scott Knake
    Custom Software Development
    Apex Software, Inc.

  3. #3
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: viewing images in a picturebox P2

    Just what I said. When you see a color image that your like. I wanted to add 2 More images. 1 is a black and white image and a 3D image. from the same color Image. When I click in the H or D buttons the code seems to reset it self to the next or prev image. So you not really looking at the right image.
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: viewing images in a picturebox P2

    What happens when you step through the code in a debugger? Have you tried using Debug.Write statements to log the index value to the Output window when you click the various buttons?

    A couple of suggestions...
    Rather than just allocate a new list each time you populate it, I recommend that you call list.Clear( ). In fact, you have a lot of redundant code, so why don't you clean that up by creating some helper methods? Lastly, may I suggest that you prefix your class field names with an underscore (i.e. '_')? It makes it easier to determine if it's a class field or a method variable.

    Code:
    private List<string> _fileList = new List<string>();
    private public int _index = 0; 
     
    // NOT REQUIRED private int count;
     
    ///
    /// Button handlers
    ///
     
    private void btnD_Click(object sender, EventArgs e)
    {
      PopulateList( Directory.GetFiles(@"images\ViewD\") );
    }
     
    private void btnH_Click(object sender, EventArgs e)
    {
      PopulateList( Directory.GetFiles(@"images\ViewH\") );
    }
    
    etc.
     
    private void btnNext_Click(object sender, EventArgs e)
    {
      IncrementIndex( );
      SetImage( );
    }
    
    private void btnPrev_Click(object sender, EventArgs e)
    {
      DecrementIndex( );
      SetImage( );
    }
     
    ///
    /// Helper methods
    ///
    
    private void DecrementIndex( )
    {
      int count = _fileList.Count;
     
      if( 0 == count )
      {
        _index = -1;
        return;
      }
     
      if( 0 == _index ) // wrap index
      {
        _index = _count - 1;
      }
      else
      {
        _index--;
      }
    }
     
    private void IncrementIndex( )
    {
      int count = _fileList.Count;
      if( 0 == count )
      {
        _index = -1;
        return;
      }
     
      if( _index == _count - 1 ) // wrap index
      {
        _index = 0;
      }
      else
      {
        _index++;
      }
    }
     
    private void PopulateList( string[] files )
    {
      _fileList.Clear( );
      _index = -1;
     
      if( null == files )
      {
        foreach (string str in files)
        {
          if (str.EndsWith(".jpg"))
          {
            _fileList.Add( str );
          }
        }
      }
    
      if( 0 < _fileList.Count )  
      {
        _index = 0;
      }
    
        SetImage( );
    }
     
    private void SetImage( )
    {
      // NOTE: double check that the picturebox is cleared by setting image location to null
      pictureBox1.ImageLocation = ( -1 < _index ) ? _fileList[ _index ] : null;
      textBox1.Text = ( -1 < _index ) ? list[ _index ] : String.Empty;
    }
    Last edited by Arjay; July 19th, 2009 at 10:39 AM.

  5. #5
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: viewing images in a picturebox P2

    Well If I under stand you right. More then likely I don't Thats not what I'm trying to do.

    Ok, I have a terrain called "DBF1"
    The terrain has 3 files (images) for just this one (1) terrain.
    C, D and H.
    The C = the color terrain
    The D = The 3D terrain
    and
    the H = Is Black and white image of the terrain.

    I have it setup so that it scrolls thew the color (C) terrains. What I want to do is when the file name of the current terrain is in the textbox1 is that I have 2 buttons. One called H and one called D. When I click on one if the buttons. It displays that image from that terrain.

    Sorry I have no other way of explaining this so you can under stand
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: viewing images in a picturebox P2

    Quote Originally Posted by bigjoe11a View Post
    Well If I under stand you right. More then likely I don't Thats not what I'm trying to do.
    What my code did is remove your redundant code. If what you had code wasn't close to what you need, then obviously my code isn't code to help.

    Also, I think we've been through this before, but please answer the questions that I or anyone else ask when replying to your posts.

    I've asked two questions that you haven't answered:
    What happens when you step through the code in a debugger? Have you tried using Debug.Write statements to log the index value to the Output window when you click the various buttons?
    I have another question for you. Are the terrain files named the same in each of the terrain directories? In other words, there are 3 files name "DBF1[.jpg]" in each of the C, D, and H directories. Is this correct?

  7. #7
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: viewing images in a picturebox P2

    Quote Originally Posted by Arjay View Post
    What my code did is remove your redundant code. If what you had code wasn't close to what you need, then obviously my code isn't code to help.

    Also, I think we've been through this before, but please answer the questions that I or anyone else ask when replying to your posts.

    I've asked two questions that you haven't answered:


    I have another question for you. Are the terrain files named the same in each of the terrain directories? In other words, there are 3 files name "DBF1[.jpg]" in each of the C, D, and H directories. Is this correct?
    That's what I thought. Ok, Debugger won't tell me how to write code. So since the program doesn't crash or error out. I don't see how that will help.

    I have it setup so that it writes a index
    [list] or a list[0] to textbox1. You should see that in my code.



    textBox1.Text = list[0];




    The code above is what I'm using now. Well all I'm trying to do is find out how I can get the current file name from this and use that
    the Output window when you click the various buttons? I have another question for you. Are the terrain files named the same in each of the terrain directories? In other words, there are 3 files name "DBF1[.jpg]" in each of the C, D, and H directories. Is this correct? Today 12:45 PM
    Yes. and No. The Color terrains are in same folder called terrains. and each color terrain has 2 files related to it. So that makes 3 files for each terrain. I have each D and H images in a terrains/DBF1/DBF1.jpg folder. Each folder has only 2 images in it. The D and H images.

    I hope that helps
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  8. #8
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: viewing images in a picturebox P2

    I don't think I explain my last post. I attached a image of the folders and what I have in each one.

    I have it setup to view the color terrains with no problem. When I have a color terrain selected in the textbox1. I want to view the other 2 images related to the color terrain
    Attached Images Attached Images
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: viewing images in a picturebox P2

    As far as the earlier debugging questions. Please answer the questions even if you don't think it's applicable. Debugging can be useful even if a program doesn't crash. You can take a look at the values in the program to see why the wrong image is being displayed.

    At any rate, what I am trying to understand is the relationship between the files (if there is one).

    I understand that there is a folder that contains the 'C' files and there are two child folders that contain the 'D' and 'H' files.

    Can you list the files in each folder?

    For example:
    Code:
     
    \Terrain folder>
     DFB3.JPG
     DFD1.JPG
     DFD2.JPG
     DFD3.JPG
     DFD4.JPG
     DFG1.JPG
     DFG3.JPG
     DFG4.JPG
     DFG6.JPG
     DGM1.JPG
     L Dfolder
       What files are here?
     L Hfolder
        What files are here?
    Last edited by Arjay; July 19th, 2009 at 08:31 PM.

  10. #10
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: viewing images in a picturebox P2

    Quote Originally Posted by Arjay View Post
    As far as the earlier debugging questions. Please answer the questions even if you don't think it's applicable. Debugging can be useful even if a program doesn't crash. You can take a look at the values in the program to see why the wrong image is being displayed.

    At any rate, what I am trying to understand is the relationship between the files (if there is one).

    I understand that there is a folder that contains the 'C' files and there are two child folders that contain the 'D' and 'H' files.

    Can you list the files in each folder?

    For example:
    Code:
     
    root folder
    L Dfolder
     What files are here?
    L Hfolder
     What files are here?
    DFB3.JPG
    DFD1.JPG
    DFD2.JPG
    DFD3.JPG
    DFD4.JPG
    DFG1.JPG
    DFG3.JPG
    DFG4.JPG
    DFG6.JPG
    DGM1.JPG
    Only in the terrain folder. That's where the code from my 1st post comes from. It lets me view the color terrains. and in the folders.
    ViewH and ViewD Theres one (1) image is each folder that relates to one (1) image in the terrain folder.

    maybe this mite help

    /terrain
    DGM1.JPG // color image of teerain
    /terrain/viewH
    DGM1.JPG // 3D image of the color terrian
    /terain/ViewD
    DGM1.JPG //Black and white image of the color terrain

    I can view the color images for the terrains just fine.
    I'm trying to think of a way to view the other images too.
    that relate to the color image.
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured