这是我为 WPF BitmapSource 和 GDI 位图(用于我自己的项目)之间的别名(共享内存)编写的一些代码*
显然,您需要根据自己的需要对其进行调整,最终可能会产生一种不那么“hacky”的感觉。
class AliasedBitmapSource : BitmapSource {
private Bitmap source;
public AliasedBitmapSource(Bitmap source) {
this.source = source;
this.pixelHeight = source.Height;
this.pixelWidth = source.Width;
this.dpiX = source.HorizontalResolution;
this.dpiY = source.VerticalResolution;
}
public override event EventHandler DownloadCompleted;
public override event EventHandler<ExceptionEventArgs> DownloadFailed;
public override event EventHandler<ExceptionEventArgs> DecodeFailed;
protected override Freezable CreateInstanceCore() {
throw new NotImplementedException();
}
private readonly double dpiX;
public override double DpiX {
get {
return dpiX;
}
}
private readonly double dpiY;
public override double DpiY {
get {
return dpiY;
}
}
private readonly int pixelHeight;
public override int PixelHeight {
get {
return pixelHeight;
}
}
private readonly int pixelWidth;
public override int PixelWidth {
get {
return pixelWidth;
}
}
public override System.Windows.Media.PixelFormat Format {
get {
return PixelFormats.Bgra32;
}
}
public override BitmapPalette Palette {
get {
return null;
}
}
public unsafe override void CopyPixels(Int32Rect sourceRect, Array pixels, int stride, int offset) {
BitmapData sourceData = source.LockBits(
sourceRect.ToRectangle(),
ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
fixed (byte* _ptr = &((byte[])pixels)[0]) {
byte* dstptr = _ptr;
byte* srcptr = (byte*)sourceData.Scan0;
for (int i = 0; i < pixels.Length; ++i) {
*dstptr = *srcptr;
++dstptr;
++srcptr;
}
}
source.UnlockBits(sourceData);
}
}
public static class Extensions {
public static Rectangle ToRectangle(this Int32Rect me) {
return new Rectangle(
me.X,
me.Y,
me.Width,
me.Height);
}
public static Int32Rect ToInt32Rect(this Rectangle me) {
return new Int32Rect(
me.X,
me.Y,
me.Width,
me.Height);
}
}
*“写”是指“10 分钟内拼凑在一起”