Skip to content

Commit

Permalink
Make D2D backend match Skia.
Browse files Browse the repository at this point in the history
Skia doesn't support getting DPI from loaded bitmaps and always returns 96. Do the same in D2D.
  • Loading branch information
grokys committed Dec 20, 2019
1 parent 37d0078 commit de31680
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public D2DBitmapImpl(Bitmap d2DBitmap)
_direct2DBitmap = d2DBitmap ?? throw new ArgumentNullException(nameof(d2DBitmap));
}

public override Vector Dpi => _direct2DBitmap.DotsPerInch.ToAvaloniaVector();
public override Vector Dpi => new Vector(96, 96);
public override PixelSize PixelSize => _direct2DBitmap.PixelSize.ToAvalonia();

public override void Dispose()
Expand Down Expand Up @@ -58,3 +58,4 @@ public override void Save(Stream stream)
}
}
}
;
14 changes: 6 additions & 8 deletions src/Windows/Avalonia.Direct2D1/Media/Imaging/WicBitmapImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public WicBitmapImpl(string fileName)
using (BitmapDecoder decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, fileName, DecodeOptions.CacheOnDemand))
{
WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnDemand);
Dpi = new Vector(96, 96);
}
}

Expand All @@ -39,6 +40,7 @@ public WicBitmapImpl(Stream stream)
_decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, stream, DecodeOptions.CacheOnLoad);

WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, _decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnLoad);
Dpi = new Vector(96, 96);
}

/// <summary>
Expand All @@ -62,6 +64,7 @@ public WicBitmapImpl(PixelSize size, Vector dpi, APixelFormat? pixelFormat = nul
pixelFormat.Value.ToWic(),
BitmapCreateCacheOption.CacheOnLoad);
WicImpl.SetResolution(dpi.X, dpi.Y);
Dpi = dpi;
}

public WicBitmapImpl(APixelFormat format, IntPtr data, PixelSize size, Vector dpi, int stride)
Expand All @@ -70,6 +73,8 @@ public WicBitmapImpl(APixelFormat format, IntPtr data, PixelSize size, Vector dp
WicImpl.SetResolution(dpi.X, dpi.Y);

PixelFormat = format;
Dpi = dpi;

using (var l = WicImpl.Lock(BitmapLockFlags.Write))
{
for (var row = 0; row < size.Height; row++)
Expand All @@ -82,14 +87,7 @@ public WicBitmapImpl(APixelFormat format, IntPtr data, PixelSize size, Vector dp
}
}

public override Vector Dpi
{
get
{
WicImpl.GetResolution(out double x, out double y);
return new Vector(x, y);
}
}
public override Vector Dpi { get; }

public override PixelSize PixelSize => WicImpl.Size.ToAvalonia();

Expand Down

0 comments on commit de31680

Please sign in to comment.