你如何延长Bitmap类 [英] How can you extend the Bitmap class
本文介绍了你如何延长Bitmap类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图扩展位图类,这样我能将我自己的效果的图像。当我用这个code:
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?
感谢。
推荐答案
您不能从位图,因为它是密封的派生。
You can't derive from Bitmap because it's sealed.
如果你想添加一个方法Bitmap类,写一个扩展方法:
If you want to add a method to the Bitmap class, write an extension method:
// Class containing our Bitmap extension methods.
public static class BitmapExtension
{
public void ToBlackAndWhite(this Bitmap bitmap)
{
...
}
}
然后使用它是这样的:
Then use it like this:
Bitmap bitmap = ...;
bitmap.ToBlackAndWhite();
这篇关于你如何延长Bitmap类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文