C# 在UWP中使用模糊效果的绘图图像未正确设置图像的大小
我想使用滑块模糊图像。为此,我使用了Win2D模糊效果,在CanvasControl中绘制图像,并在实际图像上添加了画布 下载样本 步骤。 1.在按钮单击中添加画布控件。它将在实际元素上向网格添加一个子元素 2.更改滑块以应用模糊 3.问题:图像拉伸,尺寸太大,看起来像被裁剪过的。不在给定的大小(500400) [XAML]C# 在UWP中使用模糊效果的绘图图像未正确设置图像的大小,c#,xaml,uwp,uwp-xaml,win2d,C#,Xaml,Uwp,Uwp Xaml,Win2d,我想使用滑块模糊图像。为此,我使用了Win2D模糊效果,在CanvasControl中绘制图像,并在实际图像上添加了画布 下载样本 步骤。 1.在按钮单击中添加画布控件。它将在实际元素上向网格添加一个子元素 2.更改滑块以应用模糊 3.问题:图像拉伸,尺寸太大,看起来像被裁剪过的。不在给定的大小(500400) [XAML] <Grid> <Grid.ColumnDefinitions> <ColumnDefi
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="7*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid x:Name="imageGrid">
<Image x:Name="image" Source="Flower1.jpg" Width="500" Height="400"
HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
<StackPanel Grid.Column="1" Orientation="Vertical">
<Button Content="AddCanvas" Width="100" x:Name="addCanvas"
Click="AddCanvas_Click"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Blur" Width="100"/>
<Slider x:Name="slider" Minimum="0" Maximum="5" Width="200"
ValueChanged="Slider_ValueChanged"/>
</StackPanel>
</StackPanel>
</Grid>
bool isBlurred;
GaussianBlurEffect blur = new GaussianBlurEffect();
CanvasControl canv = new CanvasControl();
CanvasBitmap cbi;
public MainPage()
{
this.InitializeComponent();
}
private void AddCanvas_Click(object sender, RoutedEventArgs e)
{
canv.HorizontalAlignment = HorizontalAlignment.Left;
canv.VerticalAlignment = VerticalAlignment.Top;
canv.Draw += Canv_Draw;
canv.CreateResources += Canv_CreateResources;
canv.Height = 400;
canv.Width = 500;
imageGrid.Children.Add(canv);
}
private void Canv_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args)
{
args.DrawingSession.Units = CanvasUnits.Pixels;
if (isBlurred)
{
args.DrawingSession.DrawImage(blur, new Rect(0, 0, 500, 400), new Rect(0, 0, 500, 400));
}
}
private void Canv_CreateResources(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}
async Task CreateResourcesAsync(CanvasControl sender)
{
cbi = await CanvasBitmap.LoadAsync(sender, "Flower1.jpg");
}
private void Slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
isBlurred = true;
blur.Source = cbi;
blur.BlurAmount = (float)e.NewValue;
canv.Invalidate();
}
我想应用模糊效果的图像使用滑块动态。但我不想将实际图像更改为模糊。因此,我在实际图像上使用画布,并使用模糊效果进行绘制
问候,
巴拉蒂
在UWP中使用模糊效果的绘图图像未正确设置图像的大小
问题在于CanvasControl
的大小不适合图像源(图像:960*640 CanvasControl:500*400)。如果要使CanvasControl显示完整图像,请在调用CanvasBitmap.LoadAsync
方法时设置可用的dpi。对于我的测试,185适合您的场景
async Task CreateResourcesAsync(CanvasControl sender)
{
cbi = await CanvasBitmap.LoadAsync(sender, "Flower1.jpg",185);
}
更新
//var dpi = DisplayInformation.GetForCurrentView().LogicalDpi;
var display = DisplayInformation.GetForCurrentView();
// calculate your monitor's dpi
var dpi = Math.Sqrt(Math.Pow(display.ScreenWidthInRawPixels, 2) + Math.Pow(display.ScreenHeightInRawPixels, 2)) / display.DiagonalSizeInInches;
// get the CanvasControl inch
var inch = Math.Sqrt(Math.Pow(500, 2) + Math.Pow(400, 2)) / dpi;
// calculate last dpi with the image size
var lastdpi = Math.Sqrt(Math.Pow(960, 2) + Math.Pow(640, 2)) / inch;
async Task CreateResourcesAsync(CanvasControl sender)
{
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Flower1.jpg"));
cbi = await CanvasBitmap.LoadAsync(sender, await ResizeImage(file, 500, 400));
}
更新1
If还可以使用图像大小设置CanvasControl
size,以避免计算Dpi
更新2
您也可以调整图像大小以适应画布控制,请参考以下代码
public async Task<IRandomAccessStream> ResizeImage(StorageFile file, int reqWidth, int reqHeight)
{
var memStream = new MemoryStream();
using (Stream stream = await file.OpenStreamForReadAsync())
{
stream.CopyTo(memStream);
}
IRandomAccessStream imageStream = memStream.AsRandomAccessStream();
var decoder = await BitmapDecoder.CreateAsync(imageStream);
if (decoder.PixelHeight > reqHeight || decoder.PixelWidth > reqWidth)
{
using (imageStream)
{
var resizedStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder);
double widthRatio = (double)reqWidth / decoder.PixelWidth;
double heightRatio = (double)reqHeight / decoder.PixelHeight;
double scaleRatio = Math.Min(widthRatio, heightRatio);
if (reqWidth == 0)
scaleRatio = heightRatio;
if (reqHeight == 0)
scaleRatio = widthRatio;
uint aspectHeight = (uint)Math.Floor(decoder.PixelHeight * scaleRatio);
uint aspectWidth = (uint)Math.Floor(decoder.PixelWidth * scaleRatio);
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;
encoder.BitmapTransform.ScaledHeight = aspectHeight;
encoder.BitmapTransform.ScaledWidth = aspectWidth;
await encoder.FlushAsync();
return resizedStream;
}
}
return imageStream;
}
感谢您提供解决方案。如果我设置185,它工作正常。但是,我会上传不同大小的图片。所以,我无法确定图像的大小。请告诉我,一般如何计算dpi值。我用的是下线。但它不起作用。在我的系统中,将dpi值返回为13.99。请给我一些建议。var dpi=DisplayInformation.GetForCurrentView().DiagonalSizeInnches;cbi=wait CanvasBitmap.LoadAsync(发送方,“Flower1.jpg”,(float)dpi);并且,这将在不同的系统中使用。因此,我需要根据具体情况计算dpi值system@Bharathi,您也可以调整图像大小以适应画布控制。我尝试了dpi和lastdpi值,但在我的机器中无法工作。如果我使用184.5,它在高度(500400)下工作得非常完美。请让我知道我在这件事上犯了什么错误。cbi=wait CanvasBitmap.LoadAsync(发送方,“Flower1.jpg”,(float)lastdpi);现在,我从Image SizeChanged事件中删除了静态大小(500400)并设置大小。这是实时场景。请从下面的链接下载示例。更新2有效吗?