在 C# 中显示可编辑图像

Displaying editable image in c#

我想显示一张图像(.tif,灰度值 16 位),用户可以通过滑块对其进行编辑。显示的图像应该直接对变化做出反应,这样用户就知道他对图像做了什么。

目前我似乎只是在每次更改时创建新文件并显示最新的文件,这是一个糟糕的解决方案。

我的想法是加载原始像素数据,将其放入某种不会保存也不会保存的临时文件中,然后保存滑块的参数并在点击保存时将参数应用到原始图像(因为最终滤镜用于整组图像)。

图像不是文件。如果可能是您显示文件中的图像,但是一旦您将文件加载到图像对象中,就不要再使用该文件,直到需要将编辑后的图像保存到文件中。

你说你想展示一张图片。当您使用 winforms 时,我假设您使用的是 PictureBox 控件。如果没有,您有一个 class 用于显示图像。

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

GetImageTodisplay 将读取文件并return将其作为图像

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

显然你也有滑块。您没有提到滑块的 class ,但显然它是一个通知您的表单有关新滑块值的对象。

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

直到现在我还没有看到新文件的创建。所以这应该在 ProcessSliderChange 中。幸运的是,这是您自己创建的功能。您所要做的就是根据新的滑块值更改图像。不要对文件处理做任何事情,你不会得到一个新文件

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

不幸的是,图像 class 没有很多编辑图像的功能,我假设您有一个库,其中包含作用于 System.Drawing.Image 对象的功能。只需使用这些功能来更改您的图像。