【发布时间】: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 字节的磁盘空间,同时保留所有信息。我不知道为什么这对你来说并不明显。
-
我明白,但是这种质量损失是如此之大。通常,当我在某些应用程序中缩放图片或照片时,这张图片看起来很相似。这真的是巨大的质量损失,就像一些变形一样。
-
您不是在处理照片,它在像素之间具有平滑的颜色过渡。你正在处理锋利的边缘。缩放锐利边缘会产生更明显的图像质量下降。由于您使用的是点采样(与双线性或双三次过滤相比),因此更是如此。