Image vs Bitmap class

前端 未结 3 602
半阙折子戏
半阙折子戏 2020-12-08 01:53

I have trouble understanding the differences between the Image class and the Bitmap class. Now, I know that the Bitmap inherits from t

3条回答
  •  醉话见心
    2020-12-08 02:07

    This is a clarification because I have seen things done in code which are honestly confusing - I think the following example might assist others.

    As others have said before - Bitmap inherits from the Abstract Image class

    Abstract effectively means you cannot create a New() instance of it.

        Image imgBad1 = new Image();        // Bad - won't compile
        Image imgBad2 = new Image(200,200); // Bad - won't compile
    

    But you can do the following:

        Image imgGood;  // Not instantiated object!
        // Now you can do this
        imgGood = new Bitmap(200, 200);
    

    You can now use imgGood as you would the same bitmap object if you had done the following:

        Bitmap bmpGood = new Bitmap(200,200);
    

    The nice thing here is you can draw the imgGood object using a Graphics object

        Graphics gr = default(Graphics);
        gr = Graphics.FromImage(new Bitmap(1000, 1000));
        Rectangle rect = new Rectangle(50, 50, imgGood.Width, imgGood.Height); // where to draw
        gr.DrawImage(imgGood, rect);
    

    Here imgGood can be any Image object - Bitmap, Metafile, or anything else that inherits from Image!

提交回复
热议问题