UWP 应用中的图像平滑
Image smoothing in UWP app
我有一个小型 UWP 应用程序,它以两种方式加载相同的图像:
var folder = Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("TestImage.png");
var bitmapImage1 = new BitmapImage();
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
await bitmapImage1.SetSourceAsync(stream);
}
var bitmapImage2 = new BitmapImage(new Uri(BaseUri, "TestImage.png"));
_image.Source = bitmapImage1;
//_image.Source = bitmapImage2;
问题是图像控件以不同的方式显示相同的图像。对于 bitmapImage1 图像没有平滑处理,但是对于 bitmapImage2 没问题。 How it looks like。我需要在显示之前对图像进行一些操作(更改一些像素),但之后我还需要对图像进行平滑处理。你能帮我解决一下吗?
我还使用 WriteableBitmap 更改了一些像素并得到了相同的结果(未平滑)。看起来我需要告诉 Image 控件如何绘制它。
这里是link to project了解更多信息
我在我这边测试了你的代码。而且你会发现如果你不设置图像控件的Height
和Width
属性,图片看起来都一样。所以一个看起来光滑而另一个看起来不光滑的原因是图像控件缩放图片。不缩放图片看起来不会有什么不同。我猜这可能是有铅的,但矢量图像和位图图像之间存在差异。
你应该可以解决这个问题,方法是创建一个你只想在应用程序中显示的大小的平滑图片,并将图像控件设置为与图片相同的大小或不设置 Height
和 Width
属性,这可能会解决。
我的结果:
我在另一个线程上找到了解决方案。
如果更改代码行的顺序,它会有所帮助:
var folder = Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("TestImage.png");
var bitmapImage1 = new BitmapImage();
_image.Source = bitmapImage1;
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
await bitmapImage1.SetSourceAsync(stream);
}
所以我们应该分配图像源,而不是将源流设置为位图。
It seems, Image control decodes the picture quite flexibly so that it can suit for acutal UI size. If you set the BitmapImage to Image.Source first, the picture is to be smoothed properly like the second one.
我有一个小型 UWP 应用程序,它以两种方式加载相同的图像:
var folder = Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("TestImage.png");
var bitmapImage1 = new BitmapImage();
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
await bitmapImage1.SetSourceAsync(stream);
}
var bitmapImage2 = new BitmapImage(new Uri(BaseUri, "TestImage.png"));
_image.Source = bitmapImage1;
//_image.Source = bitmapImage2;
问题是图像控件以不同的方式显示相同的图像。对于 bitmapImage1 图像没有平滑处理,但是对于 bitmapImage2 没问题。 How it looks like。我需要在显示之前对图像进行一些操作(更改一些像素),但之后我还需要对图像进行平滑处理。你能帮我解决一下吗?
我还使用 WriteableBitmap 更改了一些像素并得到了相同的结果(未平滑)。看起来我需要告诉 Image 控件如何绘制它。
这里是link to project了解更多信息
我在我这边测试了你的代码。而且你会发现如果你不设置图像控件的Height
和Width
属性,图片看起来都一样。所以一个看起来光滑而另一个看起来不光滑的原因是图像控件缩放图片。不缩放图片看起来不会有什么不同。我猜这可能是有铅的,但矢量图像和位图图像之间存在差异。
你应该可以解决这个问题,方法是创建一个你只想在应用程序中显示的大小的平滑图片,并将图像控件设置为与图片相同的大小或不设置 Height
和 Width
属性,这可能会解决。
我的结果:
我在另一个线程上找到了解决方案。 如果更改代码行的顺序,它会有所帮助:
var folder = Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("TestImage.png");
var bitmapImage1 = new BitmapImage();
_image.Source = bitmapImage1;
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
await bitmapImage1.SetSourceAsync(stream);
}
所以我们应该分配图像源,而不是将源流设置为位图。
It seems, Image control decodes the picture quite flexibly so that it can suit for acutal UI size. If you set the BitmapImage to Image.Source first, the picture is to be smoothed properly like the second one.