无论原始控件大小如何,我都试图以各种大小拍摄 WPF 控件的快照。假设控件大小是 300 像素的宽度和高度,我正在尝试拍摄各种尺寸的快照,例如 1500 像素的高度和宽度、100 像素的高度和宽度等等。
这是快照片段
objFramework.LayoutTransform =
new ScaleTransform(actualWidth / requiredWidth, actualHeight / requiredHeight);
renderBitmap = new RenderTargetBitmap(requiredWidth, requiredHeight, 96d, 96d, PixelFormats.Pbgra32);
var dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
var vb = new VisualBrush(objFramework);
ctx.DrawRectangle(vb, null, new Rect(0, 0, requiredWidth, requiredHeight));
}
renderBitmap.Render(dv);
我在找什么:
我不希望很少的子控件被缩放。即使发生缩放,内部的 TextBlock 或 Grid Row 的高度之一也是恒定的。我的示例控制结构
<Grid>
<Border x:Name="brdTiles" VerticalAlignment="Center" HorizontalAlignment="Center" BorderThickness="1" BorderBrush="Black" >
<local:GridExtension x:Name="grdTileHolder" Background="#FFFFFF" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Grid.Column="1" x:Name="TileColumnHeader" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<ComboBox x:Name="grd1" Grid.Column="0" FontSize="10" Height="20" >
<ComboBoxItem Content="Item 1" />
<ComboBoxItem Content="Item 2" />
<ComboBoxItem Content="Item 3" />
</ComboBox>
<TextBlock x:Name="txt1" Grid.Column="1" Text="20" FontSize="10" Height="20"/>
<TextBlock x:Name="txt2" Grid.Column="2" Text="30" FontSize="10" Height="20"/>
<TextBlock x:Name="txt3" Grid.Column="3" Text="40" FontSize="10" Height="20"/>
</Grid>
<Grid Grid.Column="0" Grid.Row="1" x:Name="TileRowHeader" >
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="100" Grid.Row="0" >
<ComboBox.LayoutTransform>
<RotateTransform Angle="90" />
</ComboBox.LayoutTransform>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" IsItemsHost="True">
<StackPanel.LayoutTransform>
<RotateTransform Angle="270" />
</StackPanel.LayoutTransform>
</StackPanel>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="0" />
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBoxItem>Hello</ComboBoxItem>
<ComboBoxItem>World</ComboBoxItem>
<ComboBoxItem>Foo</ComboBoxItem>
<ComboBoxItem>Check</ComboBoxItem>
</ComboBox>
<TextBlock Text="2" Grid.Row="1" />
<TextBlock Text="3" Grid.Row="2" />
<TextBlock Text="4" Grid.Row="3" />
</Grid>
<Grid Grid.Row="1" Grid.Column="1" Margin="2" Name="TileViewGrid" Background="#FFFFFF" ShowGridLines="False" HorizontalAlignment="Center" VerticalAlignment="Center" />
</local:GridExtension>
</Border>
</Grid>
为什么我想要这样
当拍摄各种尺寸的快照时,里面的文字看起来很模糊。所以我们在重新渲染控件时需要一个高质量的文本。