StorageItemThumbnail 中的位图在 GridView 中显示错误的尺寸
Bitmap from StorageItemThumbnail shows wrong dimensions in GridView
我目前正在构建一个应用程序,但我似乎无法弄清楚的一个错误是:
这些图像是 PDF 页面快照,实际上是纵向格式。不知何故,它不会正确显示。我实际上可以看到错误的尺寸和它应该显示的尺寸。
有谁知道它为什么会这样做?
转换器代码:
public object Convert(object value, Type targetType, object parameter, string language)
{
BitmapImage image = null;
if (value != null)
{
if (value.GetType() != typeof(StorageItemThumbnail))
{
throw new ArgumentException("Expected a StorageItemThumbnail as binding input.");
}
if (targetType != typeof(ImageSource))
{
throw new ArgumentException("Expected ImageSource as binding output.");
}
if ((StorageItemThumbnail)value == null)
{
System.Diagnostics.Debug.WriteLine("Thumbnail is null.");
return image;
}
image = new BitmapImage();
using (var thumbNailClonedStream = ((StorageItemThumbnail)value).CloneStream())
{
image.DecodePixelType = DecodePixelType.Logical;
_ = image.SetSourceAsync(thumbNailClonedStream);
}
}
return image;
}
缩略图检索(destinationFile = StorageFile):
StorageItemThumbnail thumbnail = await destinationFile.GetThumbnailAsync(ThumbnailMode.PicturesView, 500, ThumbnailOptions.UseCurrentScale);
CurrentPages.Add(new AppPage
{
ImageFile = destinationFile,
OriginalFile = file,
Thumbnail = thumbnail
});
网格视图XAML:
<GridView Name="pagesViewer" ItemsSource="{x:Bind CurrentPages, Mode=OneWay}" Margin="0,0,0,40" Grid.Row="1" CanDrag="True" CanDragItems="True" CanReorderItems="True" Background="White" AllowDrop="True" Drop="pagesViewer_Drop" DragOver="pagesViewer_DragOver">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:AppPage">
<StackPanel Height="280" Width="200" Margin="12" AutomationProperties.Name="{x:Bind PageNumber}" ToolTipService.ToolTip="{x:Bind OriginalFile.Name}" Background="WhiteSmoke" IsRightTapEnabled="True">
<StackPanel.ContextFlyout>
<MenuFlyout>
<MenuFlyout.Items>
<MenuFlyoutItem x:Name="DeletePage" Text="Remove Page" />
<MenuFlyoutItem x:Name="AddBlankBefore" Text="Add Blank Before" />
<MenuFlyoutItem x:Name="AddBlankAfter" Text="Add Blank After" />
</MenuFlyout.Items>
</MenuFlyout>
</StackPanel.ContextFlyout>
<Image Source="{x:Bind Thumbnail, Converter={StaticResource ResourceKey=ThumbnailConverter}}" Height="240" Width="200" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<StackPanel Margin="0,12">
<TextBlock Text="{x:Bind PageNumber, Mode=OneWay}" Foreground="Black" FontWeight="Light" FontSize="15" TextAlignment="Center" />
</StackPanel>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="10" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
Bitmap from StorageItemThumbnail shows wrong dimensions in GridView
请参考GetThumbnailAsync
document, The requested size, in pixels, of the longest edge of the thumbnail. Windows uses the requestedSize
as a guide and tries to scale the thumbnail image without reducing the quality of the image. When you set ThumbnailMode
as PicturesView
并像下面这样调用GetThumbnailAsync方法
await destinationFile.GetThumbnailAsync(ThumbnailMode.PicturesView, 500, ThumbnailOptions.UseCurrentScale);
将图片裁剪为19 / 13,则宽度限制为500,高度限制为342。
针对您的场景,我们建议您使用 ThumbnailMode.SingleItem
替换上面并将图像拉伸 属性 设置为 Fill
StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, 500, ThumbnailOptions.ResizeThumbnail);
我目前正在构建一个应用程序,但我似乎无法弄清楚的一个错误是:
这些图像是 PDF 页面快照,实际上是纵向格式。不知何故,它不会正确显示。我实际上可以看到错误的尺寸和它应该显示的尺寸。
有谁知道它为什么会这样做?
转换器代码:
public object Convert(object value, Type targetType, object parameter, string language)
{
BitmapImage image = null;
if (value != null)
{
if (value.GetType() != typeof(StorageItemThumbnail))
{
throw new ArgumentException("Expected a StorageItemThumbnail as binding input.");
}
if (targetType != typeof(ImageSource))
{
throw new ArgumentException("Expected ImageSource as binding output.");
}
if ((StorageItemThumbnail)value == null)
{
System.Diagnostics.Debug.WriteLine("Thumbnail is null.");
return image;
}
image = new BitmapImage();
using (var thumbNailClonedStream = ((StorageItemThumbnail)value).CloneStream())
{
image.DecodePixelType = DecodePixelType.Logical;
_ = image.SetSourceAsync(thumbNailClonedStream);
}
}
return image;
}
缩略图检索(destinationFile = StorageFile):
StorageItemThumbnail thumbnail = await destinationFile.GetThumbnailAsync(ThumbnailMode.PicturesView, 500, ThumbnailOptions.UseCurrentScale);
CurrentPages.Add(new AppPage
{
ImageFile = destinationFile,
OriginalFile = file,
Thumbnail = thumbnail
});
网格视图XAML:
<GridView Name="pagesViewer" ItemsSource="{x:Bind CurrentPages, Mode=OneWay}" Margin="0,0,0,40" Grid.Row="1" CanDrag="True" CanDragItems="True" CanReorderItems="True" Background="White" AllowDrop="True" Drop="pagesViewer_Drop" DragOver="pagesViewer_DragOver">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:AppPage">
<StackPanel Height="280" Width="200" Margin="12" AutomationProperties.Name="{x:Bind PageNumber}" ToolTipService.ToolTip="{x:Bind OriginalFile.Name}" Background="WhiteSmoke" IsRightTapEnabled="True">
<StackPanel.ContextFlyout>
<MenuFlyout>
<MenuFlyout.Items>
<MenuFlyoutItem x:Name="DeletePage" Text="Remove Page" />
<MenuFlyoutItem x:Name="AddBlankBefore" Text="Add Blank Before" />
<MenuFlyoutItem x:Name="AddBlankAfter" Text="Add Blank After" />
</MenuFlyout.Items>
</MenuFlyout>
</StackPanel.ContextFlyout>
<Image Source="{x:Bind Thumbnail, Converter={StaticResource ResourceKey=ThumbnailConverter}}" Height="240" Width="200" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<StackPanel Margin="0,12">
<TextBlock Text="{x:Bind PageNumber, Mode=OneWay}" Foreground="Black" FontWeight="Light" FontSize="15" TextAlignment="Center" />
</StackPanel>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="10" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
Bitmap from StorageItemThumbnail shows wrong dimensions in GridView
请参考GetThumbnailAsync
document, The requested size, in pixels, of the longest edge of the thumbnail. Windows uses the requestedSize
as a guide and tries to scale the thumbnail image without reducing the quality of the image. When you set ThumbnailMode
as PicturesView
并像下面这样调用GetThumbnailAsync方法
await destinationFile.GetThumbnailAsync(ThumbnailMode.PicturesView, 500, ThumbnailOptions.UseCurrentScale);
将图片裁剪为19 / 13,则宽度限制为500,高度限制为342。
针对您的场景,我们建议您使用 ThumbnailMode.SingleItem
替换上面并将图像拉伸 属性 设置为 Fill
StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, 500, ThumbnailOptions.ResizeThumbnail);