0

I have a WinUI app:

<Image x:Name="PixelSurface" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Image.RenderTransform>
        <ScaleTransform ScaleX="4" ScaleY="4" />
    </Image.RenderTransform>
</Image>

It works, but the pixels get all fuzzy. Probably because it's doing some smoothing. Can I scale it up without any filters being added.

Thanks.

5
  • "Use the RenderAtScale property to render the BitmapCache at a multiple of the normal bitmap size." learn.microsoft.com/en-us/dotnet/api/… Commented Aug 7 at 5:16
  • Hi @GerrySchmitz. That's for WPF and won't work for WinUI. Commented Aug 7 at 7:51
  • Hi @Anders. Are you open to use Win2d? Commented Aug 7 at 7:52
  • Yes, if it is possible to do there Commented Aug 7 at 8:35
  • If you want blocks for each pixel instead of a smooth image you can usually specify "nearest neighbor interpolation" when rendering. But this tend to look terrible at non integer scaling factors, and I have no idea how to specify interpolation mode in WinUI. Commented Aug 7 at 9:53

1 Answer 1

0
  1. The approach used for scaling image will not work with WinUI. It is for WPF.

  2. Below snippet will scale the image without getting fuzzy, You need to invoke the method in your required events.

private async Task LoadAndScaleImageAsync(StorageFile file, double scaleFactor)
{
   using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
   {
       BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
       using (InMemoryRandomAccessStream scaledStream = new InMemoryRandomAccessStream())
       {
           BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(scaledStream, decoder);
           BitmapTransform transform = new BitmapTransform();
           transform.InterpolationMode = BitmapInterpolationMode.NearestNeighbor;
           transform.ScaledWidth = (uint)(decoder.PixelWidth * scaleFactor);
           transform.ScaledHeight = (uint)(decoder.PixelHeight * scaleFactor);
           encoder.BitmapTransform = transform;
           await encoder.FlushAsync();
           BitmapImage bitmapImage = new BitmapImage();
           scaledStream.Seek(0);
           await bitmapImage.SetSourceAsync(scaledStream);
           PixelSurface.Source = bitmapImage;
       }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.