vri*_*h88 3 c# inheritance 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
'ImageEditor.Effects': cannot derive from sealed type 'System.Drawing.Bitmap'
Run Code Online (Sandbox Code Playgroud)
那么有没有办法解决这个问题,或者根本无法扩展课程?
谢谢.
Jud*_*ngo 15
你不能从Bitmap派生,因为它是密封的.
如果要向Bitmap类添加方法,请编写扩展方法:
// Class containing our Bitmap extension methods.
public static class BitmapExtension
{
public static void ToBlackAndWhite(this Bitmap bitmap)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
Bitmap bitmap = ...;
bitmap.ToBlackAndWhite();
Run Code Online (Sandbox Code Playgroud)