Search code examples
c#pictureboxtrackbar

Using PictureBox as a TrackBar in C#


I'm making a trakbar with a picture in the pictureBox. By clicking that trackbar, there must be a black vertical line. But there is a problem shown below.

http://j1307.hizliresim.com/1c/8/q0rh1.png


Solution

  • Your problem is that shoe-horning a DateTime into a string won't give you a supported filename for the image to save to.

    As example:

    String fileName = "C:\\" + DateTime.Now + ".bmp";
    File.Create(fileName);
    

    will throw an error because fileName gives you a path of C:\08/07/2013 12:41:39.bmp - which is not a valid file path.

    To fix this you would format the DateTime potion of your string to something more palatable, like

    String formattedDateTime = DateTime.Now.ToString("s").Replace(":","-");
    String fileName = String.Format(@"C:\{0}.bmp", formattedDateTime);
    File.Create(fileName);
    

    This would give you a filename like C:\2013-07-08T12-48-57.bmp which will not only save but is sort-able as well.

    So finally, to apply this to your code, you would use

    String formattedDateTime = DateTime.Now.ToString("s").Replace(":","-") ;
    String fileName = String.Format(@"C:\{0}.bmp", formattedDateTime);
    img.Save(fileName);