.net 8 wpf
My requirement is to merge and print engineering drawings
My drawings are SolidWorks drawings
I first converted the drawings into PDF format
Then each page of the PDF was converted into an image
Then, the image was loaded using WrapPanel
Finally, use the RenderTargetBitmap virtual rendering to save the image
The lower part of the exported JPG file is grayed out,
And the Border rendered correctly, but the Image content inside the Border is grayed out
At first, I thought it might be because some content was not displayed using ScrollViewer, and there were some optimization mechanisms in WPF
As a result, I directly placed the content of RenderTarget bitmap on the Source of Image, and the result is like this
right site is a Image Control ,ImageSource is the WrapPanel visual render
this is picture attr
this is error part in picture
another error part in picture file ,but zhe Border Posion is Right
public static ImageSource ElementToBitmap(FrameworkElement element, Stream stream, BitmapEncoder? encoder = null)
{
encoder = encoder ?? new JpegBitmapEncoder() { QualityLevel = 100 };
var renderBitmap = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Pbgra32);
renderBitmap.Render(element);
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(stream);
return renderBitmap;
}
private void OutPut_Click(object sender, RoutedEventArgs e)
{
var saveFileDialog = new SaveFileDialog()
{
Filter = "Image files (*.jpg)|*.jpg"
};
if(saveFileDialog.ShowDialog() != true) return;
Task.Run(() => {
App.UIDispatcher.Invoke(() => {
using (var stream = saveFileDialog.OpenFile())
{
var source = SolidWorkManager.ElementToBitmap(RangePanel, stream);
TestImage.Source = source;
}
});
});
}
<Border Grid.Column="1" Width="900" CornerRadius="5" BorderBrush="Black" BorderThickness="2" Margin="0,0,0,20">
<ScrollViewer VerticalAlignment="Top" >
<Viewbox Width="880" Height="Auto">
<WrapPanel x:Name="RangePanel" Width="{Binding RealWidth}" Height="Auto" Margin="{Binding RangeMargin}" Background="White">
<ItemsControl ItemsSource="{Binding PicMergeItems}" Visibility="Visible">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Visibility="Visible" Margin="{Binding ItemMargin}" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="1" Padding="0">
<Image Visibility="Visible" Tag="{Binding}" Source="{Binding FullPath}" Width="{Binding Width}" Height="{Binding Height}" MouseDown="Image_MouseDown">
<Image.LayoutTransform>
<RotateTransform Angle="{Binding Angle}" />
</Image.LayoutTransform>
</Image>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Visibility="Visible"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</WrapPanel>
</Viewbox>
</ScrollViewer>
</Border>
研小艾 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1