Search code examples
c#winformstiffimaginglibtiff

Displaying editable image in c#


I want to display an image (.tif, gray value 16 bit) which is editable for the user via sliders. The displayed image should directly react to the changes, so the user knows what he's doing to the image.

Currently I only seem to be creating new files with every change and displaying the latest one, which is a terrible solution.

My idea would be to load the original pixeldata, put it in some sort of temporary file which isn't and won't be saved, then saving the parameters of the sliders and when hitting save applying the parameters to the original image (since in the end the filter is used on an intire set of images).


Solution

  • An Image is not a File. If might be that you display the Image that is in a file, but once you've loaded the file into an Image object don't ever use the file again, until the edited image needs to be saved in a file.

    You say you want to display an image. As you are using winforms, I assume you are using a PictureBox control. If not, you have a class that you order to display an Image.

    Image imageToDisplay = GetImageToDisplay();
    this.PictureBox1.Image = imageToDisplay; 
    

    GetImageTodisplay will read the file and return it as an Image

    System.Drawing.Image GetImageToDisplay()
    {
         FileStream imageStream = new FileStream(this.GetImageFileName(), FileMode.Open);
         System.Drawing.Image image = new Image(imageStream);
         return image;
    }
    

    Apparently you also have sliders. You don't mention the class of your sliders, but apparently it is an object that notifies your form about a new slider value.

    void OnSliderValueChanged(object sender, int sliderValue)
    {
        Image displayedImage = this.PictureBox1.Image;
        this.ProcessSliderChanged(image, sliderValue);
    }
    

    Until now I don't see the creation of a new file. So this should be in ProcessSliderChange. Luckily that is a function you created yourself. All you have to do is change the image according to your new slider value. Don't do anything with file handling and you won't get a new file

    void ProcessSliderChange(System.Drawing.Image image, int sliderValue)
    {   // what do you want to do with the image?
        // make it darker? move it? change contrast?
    }
    

    Unfortunately the Image class doesn't have a lot of functions to edit images, I assume you have a library with functions that act on System.Drawing.Image objects. Just use those functions to change your Image.