How can you extend the Bitmap class

I am trying to extend the Bitmap class so that I can apply my own effects to the 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 around this or is it just impossible to extend the class?

Thanks.

+3
source share
5 answers

You cannot get from Bitmap because it is sealed.

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 static void ToBlackAndWhite(this Bitmap bitmap)
    {
        ...
    }
}

Then use it like this:

Bitmap bitmap = ...;
bitmap.ToBlackAndWhite();
+15
source

+1 for Judas answer.

, , . ( ..)

+2

BitmapSource, WPF, toblackandwhite - WPF . . BitmapSource

+1

, , . . Bitmap, .

, - , , Bitmap.

0

- . , , , (is-a relationship), , . , , , -, .

If you really intended to override some of the Bitmap methods with your own, I would carefully look at what you are trying to do - for something as complicated as image manipulation, you might be better off providing functionality next to the original methods as opposed to their replacement.

0
source

Source: https://habr.com/ru/post/1706700/


All Articles