【问题标题】:UWP - scaled image is damagedUWP - 缩放的图像已损坏
【发布时间】:2016-07-28 10:57:37
【问题描述】:

我无法放置按比例缩小的图像。图像被光栅化了,我不知道找到正确放置它的方法。

是否可以在不损失质量的情况下放置图片?

代码:

public async Task<BitmapImage> BitmapTransform(string filePath, uint width)
        {

            StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);
            if (file == null)
                return null;

            // create a stream from the file and decode the image
            var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);


            // create a new stream and encoder for the new image
            InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
            BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);


            double ration = 
            enc.BitmapTransform.ScaledWidth = width;
            enc.BitmapTransform.ScaledHeight = (uint)(((double)decoder.PixelHeight / (double)decoder.PixelWidth) * (double)width);


            // write out to the stream
            try
            {
                await enc.FlushAsync();
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
            }

            // render the stream to the screen
            BitmapImage bImg = new BitmapImage();
            bImg.SetSource(ras);
            return bImg;

        }

【问题讨论】:

  • 您不能同时缩放图像和缩放图像。缩放图像总是会影响图像质量,不影响图像质量的唯一方法是不缩放图像。这应该是显而易见的。
  • 但它不会放大到超过 100%,它会根据屏幕分辨率调整到大约 50%。
  • 缩小图像意味着丢失信息。这导致图像质量下降。如果不是这种情况,我们都只是压缩所有文件以占用 0 字节的磁盘空间,同时保留所有信息。我不知道为什么这对你来说并不明显。
  • 我明白,但是这种质量损失是如此之大。通常,当我在某些应用程序中缩放图片或照片时,这张图片看起来很相似。这真的是巨大的质量损失,就像一些变形一样。
  • 您不是在处理照片,它在像素之间具有平滑的颜色过渡。你正在处理锋利的边缘。缩放锐利边缘会产生更明显的图像质量下降。由于您使用的是点采样(与双线性或双三次过滤相比),因此更是如此。

标签: c# windows xaml uwp


【解决方案1】:

您可以通过 BitmapTransform.InterporationMode 属性选择调整大小的方式 - Nearest Neighbor、Linear、Cubic、Fant。 你试过了吗?

https://msdn.microsoft.com/en-us/library/windows/apps/windows.graphics.imaging.bitmaptransform.interpolationmode

        double ration = 
        enc.BitmapTransform.ScaledWidth = width;
        enc.BitmapTransform.ScaledHeight = (uint)(((double)decoder.PixelHeight / (double)decoder.PixelWidth) * (double)width);
        enc.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Cubic;  // <----- Set the Interporation mode here

但结果的质量各不相同。这取决于原始图像。

【讨论】:

  • 这不能正常工作。图片宽度超过 4000 像素,我需要将图片缩小到 1366 像素等等......只需为每个屏幕分辨率做好准备。
猜你喜欢
  • 1970-01-01
  • 2014-10-03
  • 2023-03-13
  • 2017-06-23
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
相关资源
最近更新 更多