Skip to content

Commit

Permalink
Workaround for the noise texture low-res DPI scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jul 16, 2017
1 parent fde2bcd commit 3ef26eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
22 changes: 17 additions & 5 deletions UICompositionAnimations/Helpers/Win2DImageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -102,6 +103,8 @@ public static async Task<IEnumerable<CompositionBrush>> ClearCacheAsync()
try
{
// Load the bitmap with the appropriate settings
DisplayInformation display = DisplayInformation.GetForCurrentView();
float dpi = display.LogicalDpi;
switch (dpiMode)
{
case BitmapDPIMode.UseSourceDPI:
Expand All @@ -111,11 +114,9 @@ public static async Task<IEnumerable<CompositionBrush>> ClearCacheAsync()
bitmap = await CanvasBitmap.LoadAsync(creator, uri, 96);
break;
case BitmapDPIMode.CopyDisplayDPISetting:
float dpi = DisplayInformation.GetForCurrentView().LogicalDpi;
bitmap = await CanvasBitmap.LoadAsync(creator, uri, dpi);
break;
case BitmapDPIMode.CopyDisplayDPISettingsWith96AsLowerBound:
dpi = DisplayInformation.GetForCurrentView().LogicalDpi;
bitmap = await CanvasBitmap.LoadAsync(creator, uri, dpi >= 96 ? dpi : 96);
break;
default:
Expand All @@ -128,17 +129,28 @@ public static async Task<IEnumerable<CompositionBrush>> ClearCacheAsync()
DirectXPixelFormat.B8G8R8A8UIntNormalized, DirectXAlphaMode.Premultiplied);

// Calculate the surface size
Size size = bitmap.Size;
CanvasComposition.Resize(surface, size);
Size
size = bitmap.Size,
sizeInPixels = new Size(bitmap.SizeInPixels.Width, bitmap.SizeInPixels.Height);
CanvasComposition.Resize(surface, sizeInPixels);

// Draw the image on the surface and get the resulting brush
using (CanvasDrawingSession session = CanvasComposition.CreateDrawingSession(surface))
using (CanvasDrawingSession session = CanvasComposition.CreateDrawingSession(surface, new Rect(0, 0, sizeInPixels.Width, sizeInPixels.Height), dpi))
{
// Fill the target surface
session.Clear(Color.FromArgb(0, 0, 0, 0));
session.DrawImage(bitmap, new Rect(0, 0, size.Width, size.Height), new Rect(0, 0, size.Width, size.Height));
session.EffectTileSize = new BitmapSize { Width = (uint)size.Width, Height = (uint)size.Height };

// Setup the effect brush to use
CompositionSurfaceBrush brush = surface.Compositor.CreateSurfaceBrush(surface);
brush.Stretch = CompositionStretch.None;
double pixels = display.RawPixelsPerViewPixel;
if (pixels > 1)
{
brush.Scale = new Vector2((float)(1 / pixels));
brush.BitmapInterpolationMode = CompositionBitmapInterpolationMode.NearestNeighbor;
}
return brush;
}
}
Expand Down
2 changes: 1 addition & 1 deletion UICompositionAnimations/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.9.0.0")]
[assembly: AssemblyVersion("2.9.1.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]
4 changes: 2 additions & 2 deletions UICompositionAnimations/UICompositionAnimations.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<package >
<metadata>
<id>Sergio0694.UWP.UICompositionAnimations</id>
<version>2.9.0.0</version>
<version>2.9.1.0</version>
<title>UICompositionAnimations</title>
<description>A wrapper UWP PCL to work with Windows.UI.Composition and XAML animations, and Win2D effects</description>
<authors>Sergio Pedri</authors>
<owners>Sergio Pedri</owners>
<projectUrl>https://github.com/Sergio0694/UICompositionAnimations</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<releaseNotes>New APIs available, bug fixes and code refactoring</releaseNotes>
<releaseNotes>Workaround for the noise texture low-res DPI scaling</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>uwp composition animations xaml csharp windows winrt universal app ui win2d graphics</tags>
</metadata>
Expand Down

0 comments on commit 3ef26eb

Please sign in to comment.