Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/Magick.NET.AvaloniaMediaImaging/IMagickImageExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,34 @@ namespace ImageMagick;
public static partial class IMagickImageExtentions
{
/// <summary>
/// Converts this instance to a <see cref="WriteableBitmap"/>.
/// Converts this instance to a <see cref="WriteableBitmap"/> with a default DPI of 96x96.
/// </summary>
/// <param name="self">The image.</param>
/// <typeparam name="TQuantumType">The quantum type.</typeparam>
/// <returns>A <see cref="WriteableBitmap"/>.</returns>
public static unsafe WriteableBitmap ToWriteableBitmap<TQuantumType>(this IMagickImage<TQuantumType> self)
where TQuantumType : struct, IConvertible
{
Copy link
Owner

@dlemstra dlemstra Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would create a private method that has the current implementation and specify either new Vector(96, 96) or new Vector(self.Density.X, self.Density.Y) as an argument to that method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it with ToWriteableBitmapInternal and sending argument between the two.

return self.ToWriteableBitmapInternal(false);
}

/// <summary>
/// Converts this instance to a <see cref="WriteableBitmap"/>.
/// </summary>
/// <param name="self">The image.</param>
/// <typeparam name="TQuantumType">The quantum type.</typeparam>
/// <returns>A <see cref="WriteableBitmap"/>.</returns>
public static unsafe WriteableBitmap ToWriteableBitmapWithDensity<TQuantumType>(this IMagickImage<TQuantumType> self)
where TQuantumType : struct, IConvertible
{
return self.ToWriteableBitmapInternal(true);
}

private static unsafe WriteableBitmap ToWriteableBitmapInternal<TQuantumType>(this IMagickImage<TQuantumType> self, bool withDensity)
where TQuantumType : struct, IConvertible
{
var size = new PixelSize((int)self.Width, (int)self.Height);
var density = new Vector(self.Density.X, self.Density.Y);
var density = withDensity ? new Vector(self.Density.X, self.Density.Y) : new Vector(96, 96);
var bitmap = new WriteableBitmap(size, density, PixelFormats.Rgba8888, AlphaFormat.Unpremul);

using var framebuffer = bitmap.Lock();
Expand Down