我想从表单窗口浏览图像。我还创建了一个类并创建了一些过滤器。我可以从表单中读取此图像。
我的目标是在我的课堂上宣布它。并在任何地方使用此图像。但我不知道我该怎么做。
private void btn_BROWSE_Click(object sender, EventArgs e)
{
OpenFileDialog imge = new OpenFileDialog();
imge.Filter = "Extensions |*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff|"
+ "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"
+ "Zip Files|*.zip;*.rar";
imge.ShowDialog();
string imgepath = imge.FileName;
pBox_SOURCE.ImageLocation = imgepath;//i'm browsing an image
}
private void sliderKernel_MouseUp(object sender, MouseEventArgs e)
{
Bitmap OriginalImage = new Bitmap(pBox_SOURCE.Image);
}
class Filters
{
// (i would like to initialize my image in here not in form :) )
}
我会定义一个抽象类 Filter 并将每个过滤器实现为该类的继承人。
public abstract class Filter
{
public Bitmap Image { get; set; }
public abstract void Apply();
}
一个实现是:
public class SliderKernel : Filter
{
public overrides void Apply()
{
//manipulates the Image property
}
}
如果您想在任何地方使用该图像,您应该将其声明为类的静态成员:
public static class ImageContainer
{
public static Bitmap Image { get; set; }
}
您可以在表单代码中使用所有这些,如下所示:
private void btn_BROWSE_Click(object sender, EventArgs e)
{
OpenFileDialog imge = new OpenFileDialog();
imge.Filter = "Extensions |*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff|"
+ "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"
+ "Zip Files|*.zip;*.rar";
imge.ShowDialog();
string imgepath = imge.FileName;
pBox_SOURCE.ImageLocation = imgepath;//i'm browsing an image
//save the image to the container
ImageContainer.Image = new Bitmap(pBox_SOURCE.Image);
}
private void sliderKernel_MouseUp(object sender, MouseEventArgs e)
{
Filter filter = new SliderKernel () { Image = ImageContainer.Image };
filter.Apply();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句