为什么这是可能的?:
BitmapSource i = Imaging.CreateBitmapSourceFromHBitmap(...);
我正在编写一个应用程序,发现了这行代码,但我感到困惑,因为MSDN说BitmapSource是一个抽象类。
为什么这是可能的?:
BitmapSource i = Imaging.CreateBitmapSourceFromHBitmap(...);
我正在编写一个应用程序,发现了这行代码,但我感到困惑,因为MSDN说BitmapSource是一个抽象类。
BitmapSource
是一个抽象类,因此无法直接创建,但是 Imaging.CreateBitmapSourceFromHBitmap
返回了一些继承自 BitmapSource
的具体类,因此可以将其“强制转换”为 BitmapSource
。
这类似于拥有一个抽象的 Animal
类,但有一个继承自它的具体的 Giraffe
类:
Animal a = new Animal(); // illegal
Animal a = Zoo.CreateAnimalFromName("Giraffe"); // valid - returns a Giraffe instance
Imaging.CreateBitmapSourceFromHBitmap
返回一个继承自 BitmapSource
的具体类,因此这个调用并不会创建抽象类 BitmapSource
的实例。你只是对此感到困惑。Animal an1 = DogFactory.createDog();
Animal an2 = CatFactory.createCat();
Animal
是一个抽象类,而 Cat
和 Dog
则是继承自 Animal
的具体类。BitmapSource
是一个抽象类,但是 Imaging.CreateBitmapSourceFromHBitmap
创建了一个具体子类 InteropBitmap
的对象,你可以在 .NET reference source 中看到:
unsafe public static BitmapSource CreateBitmapSourceFromHBitmap(
IntPtr bitmap,
IntPtr palette,
Int32Rect sourceRect,
BitmapSizeOptions sizeOptions)
{
SecurityHelper.DemandUnmanagedCode();
// CR: [....] (1681459)
return CriticalCreateBitmapSourceFromHBitmap(bitmap, palette, sourceRect, sizeOptions, WICBitmapAlphaChannelOption.WICBitmapUseAlpha);
}
unsafe internal static BitmapSource CriticalCreateBitmapSourceFromHBitmap(
IntPtr bitmap,
IntPtr palette,
Int32Rect sourceRect,
BitmapSizeOptions sizeOptions,
WICBitmapAlphaChannelOption alphaOptions)
{
if (bitmap == IntPtr.Zero)
{
throw new ArgumentNullException("bitmap");
}
return new InteropBitmap(bitmap, palette, sourceRect, sizeOptions, alphaOptions); // use the critical version
}
你可以将InteropBitmap
分配给BitmapSource
类型的变量,因为它是其基类(直接或间接),就像这样:
interface ISomeInterface { };
abstract class SomeBaseClass : ISomeInterfac { };
class SomeClass : SomeBaseClass { };
ISomeInterface var1 = new SomeClass();
或者:
SomeBaseClass var2 = new SomeClass();
class SomeFactoryClass
{
public SomeBaseClass CreateObject() { return new SomeClass(); }
}
SomeBaseClass var3 = SomeFactoryClass.CreateObject();
除了来自.NET参考源代码外,完全与上述内容相同。