如何扩展 Bitmap 类
我正在尝试扩展 Bitmap 类,以便我可以将自己的效果应用于图像。 当我使用此代码时:
namespace ImageEditor
{
public class Effects : System.Drawing.Bitmap
{
public void toBlackAndWhite()
{
System.Drawing.Bitmap image = (Bitmap)this;
AForge.Imaging.Filters.Grayscale filter = new AForge.Imaging.Filters.Grayscale();
this = filter.Apply(this);
}
}
}
我收到以下错误:
'ImageEditor.Effects': cannot derive from sealed type 'System.Drawing.Bitmap'
那么有没有办法解决这个问题,或者根本无法扩展该类?
谢谢。
I am trying to extend the Bitmap class so that I can apply my own effects to an image. When I use this code:
namespace ImageEditor
{
public class Effects : System.Drawing.Bitmap
{
public void toBlackAndWhite()
{
System.Drawing.Bitmap image = (Bitmap)this;
AForge.Imaging.Filters.Grayscale filter = new AForge.Imaging.Filters.Grayscale();
this = filter.Apply(this);
}
}
}
I get the following error:
'ImageEditor.Effects': cannot derive from sealed type 'System.Drawing.Bitmap'
So is there a way to get around this or is it simply not possible to extend the class?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法从 Bitmap 派生,因为它是密封的。
如果要向 Bitmap 类添加方法,请编写一个扩展方法:
然后像这样使用它:
You can't derive from Bitmap because it's sealed.
If you want to add a method to the Bitmap class, write an extension method:
Then use it like this:
犹大的回答+1。
另外,请考虑返回应用了滤镜的新位图,而不是修改原始位图。 从长远来看,这将为您的应用程序提供更多选择(撤消等)
+1 for Judah's answer.
Also, consider returning a new bitmap with the filter applied rather than modifying the original. This will give your app more options in the long run (undo etc)
密封类意味着作者已明确规定任何类都不能继承它。 确实没有办法解决这个问题。 如果您想向 Bitmap 类添加功能,可以查看 扩展方法。
当然,您可以编写包装类或实用程序类,但扩展方法将为您提供最类似于从
Bitmap
继承的行为。A sealed class means that the author has explicitly dictated that no classes may inherit from it. There's really no way around it. If you're looking to add functions to the Bitmap class, you could look at extension methods.
Of course you could write a wrapper class or a utility class, but extension methods will give you the behavior most like inheriting from
Bitmap
.前面的答案很好 - 扩展方法在这里很适用。 另外,我的第一个想法是继承并不是一个很好的选择,因为您并没有真正创建不同类型的位图(是一种关系),您只是想打包一些代码来操作它。 换句话说,不需要将位图派生类传递给某些东西,您只需传递修改后的位图来代替原始位图即可。
如果您确实打算用自己的方法覆盖 Bitmap 的某些方法,我会再次仔细查看您想要完成的任务 - 对于像图像处理这样复杂的事情,您可能会通过在原始方法旁边提供功能来更好地服务,而不是来取代他们。
The previous answers are good - extension methods apply nicely here. Also, my first thought was that inheritance isn't a great choice to begin with, since you're not really creating a different kind of bitmap (is-a relationship), you just want to package up some code to manipulate one. In other words there's no need to pass a bitmap-derived class to something, you can just pass the modified bitmap in place of the original.
If you really were intending to override some of Bitmap's methods with your own, I would look carefully again at what you're trying to accomplish - for something as complex as image manipulation you might be better served by supplying functionality alongside the original methods as opposed to superseding them.
如果您转向 WPF,则可以扩展 BitmapSource 类,并且添加 toblackandwhite 方法会很容易 - 但代价是必须使用 WPF 来读取位图。请参阅 自定义位图源
You can extend the BitmapSource class if you move to WPF and adding a toblackandwhite method would be easy - but the penalty is having to use WPF to read the bitmap in. See Custom BitmapSource