C#PrintVisual可缩放至分辨率

时间:2018-11-20 17:41:56

标签: c# wpf printing

我有一个WPF窗口,上面有很多信息,它模制成设定的尺寸,正好是6 x 4英寸。

如果我尝试从本地PC打印它,则它可以正常工作,并且可以打印整页,并且一切正常。

问题是,我们使用一些手动支持的终端(摩托罗拉符号),这些终端设置为使用240x320分辨率。

每当我尝试使用终端打印视觉效果时,我得到的页面只会被部分渲染,其余的打印则完全空白。当我调用“显示”然后打印它时,似乎Windows尝试使WPF窗口适合上述分辨率。

该窗口的WPF是

<Window x:Class="Propuestas.UI.Andrei.PrintReport"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Propuestas.UI.Andrei" ResizeMode="NoResize"
    mc:Ignorable="d" WindowStyle="None" Background="White"
    Title="PrintReport" Height="601" Width="424">
<Grid Margin="5,10,0,0" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="auto" />
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="105" />
    </Grid.ColumnDefinitions>
    <GroupBox Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.ColumnSpan="2" BorderThickness="0">
        <Label Background="{StaticResource logo}" HorizontalAlignment="Center" Width="275" Height="30"
                   Margin="0" />
    </GroupBox>

    <GroupBox Grid.Row="2" Grid.Column="0" BorderBrush="Black" Grid.ColumnSpan="2" Background="White">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="11" />
                <RowDefinition Height="11" />
                <RowDefinition Height="11" />
                <RowDefinition Height="11" />
                <RowDefinition Height="11" />
            </Grid.RowDefinitions>
            <Label Name="lblDIR1" Content="QUESERIA LAFUENTE S.A." Grid.Row="0" 
                   FontFamily="Verdana" FontSize="10" Padding="0" />
            <Label Content="ENTREGAR EN LA OFICINA" Grid.Row="1" Name="lblDIR2" 
                   FontFamily="Verdana" FontSize="10" Padding="0" />
            <Label Content="CTRA COLUNGA S/N" Grid.Row="2" Name="lblDIR3" Background="White" 
                   FontFamily="Verdana" FontSize="10" Padding="0" />
            <Label Content="ARRIONDAS" Grid.Row="3" Padding="0" Name="lblDIR4"  FontFamily="Verdana" FontSize="10" />
            <Label Content="33540       ASTURIAS"  Padding="0" Grid.Row="4" Name="lblDIR5" 
                   FontFamily="Verdana" FontSize="10"  />
        </Grid>
    </GroupBox>
    <GroupBox Grid.Row="1" Grid.Column="0" BorderBrush="Black" Grid.ColumnSpan="2" Width="378" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
            <Label Content="Embalaje: " Grid.Row="0" Grid.Column="0" FontFamily="Verdana" VerticalAlignment="Center"  VerticalContentAlignment="Center"/>
            <Label Content="Ref.: " Grid.Row="1" Grid.Column="0" FontFamily="Verdana" />
            <Label Content="Transportista: " Grid.Row="2" Grid.Column="0" FontFamily="Verdana" VerticalAlignment="Center"  VerticalContentAlignment="Center"/>
            <Label Name="lblNUMDOC" Content="2018999999 / 9999" Grid.Row="0" Grid.Column="0" Margin="65,0,0,0" FontFamily="Verdana" FontWeight="Bold" FontSize="14" />
            <Label Name="lblREF" Content="12314214211" Grid.Row="1" Grid.Column="0" Margin="35,0,0,0" FontFamily="Verdana" FontWeight="Bold" FontSize="14" />
            <Label Name="lblTRANSP" Content="METALUX ORIENTE - ORIENTE" Grid.Row="2" Grid.Column="0" Margin="85,0,0,0" VerticalAlignment="Center" VerticalContentAlignment="Center"
                   FontFamily="Verdana" FontSize="10.667" />
            <Label Content="47" Name="lblNUM" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="0" Grid.RowSpan="2" Grid.Column="2" Margin="35,0,0,0"
               FontWeight="Bold" FontSize="21.333" FontFamily="Verdana" Grid.ColumnSpan="2"  />
        </Grid>
    </GroupBox>
    <Grid Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <DataGrid Name="dgLineas1" Grid.Column="0" AutoGenerateColumns="False" BorderBrush="Black"
                  Background="White" CanUserAddRows="False" HorizontalGridLinesBrush="Transparent" VerticalGridLinesBrush="Transparent" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Lin."  Binding="{Binding LINDOC}" TextBlock.TextAlignment="Center" />
                <DataGridTextColumn Header="Referencia" Binding="{Binding REFART}" Width="100" TextBlock.TextAlignment="Center" />
                <DataGridTextColumn Header="Cant." Binding="{Binding CANTIDAD}"  Width="60" TextBlock.TextAlignment="Center" />
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid Name="dgLineas2" Grid.Column="1" AutoGenerateColumns="False" BorderBrush="Black"
                  Background="White" CanUserAddRows="False" HorizontalGridLinesBrush="Transparent" VerticalGridLinesBrush="Transparent" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Lin."  Binding="{Binding LINDOC}" TextBlock.TextAlignment="Center" />
                <DataGridTextColumn Header="Referencia" Binding="{Binding REFART}" Width="100" TextBlock.TextAlignment="Center" />
                <DataGridTextColumn Header="Cant." Binding="{Binding CANTIDAD}"  Width="60" TextBlock.TextAlignment="Center" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
    <GroupBox Grid.Row="4" Grid.Column="0" Width="250" FontFamily="Code 128" BorderBrush="Transparent" Background="White" Height="120" Grid.ColumnSpan="2" >
        <StackPanel Orientation="Vertical">
        <Viewbox >
            <Label Content="230054800747" Name="lblCb" HorizontalAlignment="Center" Background="White"  VerticalAlignment="Center" FontFamily="PrecisionID C128 22 DEMO" FontSize="14" Width="219" Height="80" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" Margin="10,0,9,0" RenderTransformOrigin="0.5,0.5"/>
        </Viewbox>
        <Label Name="lblCODEMB" Content="230054800747" HorizontalAlignment="Center" Grid.Row="1"
                   FontWeight="Bold" FontFamily="Verdana" FontSize="14" Margin="-5,0,-7,0" Width="131" />
        </StackPanel>
    </GroupBox>
</Grid>

以及我用来打印的代码

var test = new PrintReport(NUMDOC, REF, TRANSP, NUM, DIR1, DIR2, DIR3, DIR4, DIR5, CODEMB, dt);
        var printDlg = new PrintDialog();
        test.Show();
        test.Close();
        var ip = UtilExpedicion.ImpresoraEtiquetas;
        foreach (var queue in new PrintServer().GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections }))
        {
            if (queue.QueuePort.Name.Contains(ip))
            {

                var ticket = queue.DefaultPrintTicket;
                ticket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4);
                ticket.OutputQuality = OutputQuality.Photographic;
                queue.DefaultPrintTicket = ticket;
                queue.UserPrintTicket = ticket;
                printDlg.PrintQueue = queue;

                printDlg.PrintVisual(test, "");
            }
        }

我认为它与Windows有关,基本上不是在视觉上创建它假定无法以设置的分辨率显示的内容。我已经将WindowStyle设置为None,并将ResizeMode设置为NoResize,并将窗口的固定高度和宽度固定了。

在这种情况下,我尝试使用视觉效果,因为我们已经拥有了视觉效果,这将节省我一些时间,而不必在Crystal Reports中创建所有内容(尽管我仍然不确定是否也可以工作)

非常感谢您的帮助。

以后的编辑:在ViewBox中添加所有内容会使所有内容变得更小,只有大小的四分之一,但其中的所有元素都在其中。因此,这是一个解决问题。现在,我只需要模拟一个更高的分辨率。 F#我。

1 个答案:

答案 0 :(得分:0)

我会尝试rendertargetbitmap。

下面的代码将控件的图片保存为磁盘文件。 修改它以返回rtb, 然后将内存映像控件的源设置为此。 然后,您可能必须测量并排列图像以将其强制为正确的尺寸。 但是我认为应该允许您从网格及其内容中生成具有所需分辨率和大小的图像。 我将全部包括在内,这样我就不会错过任何内容。

        public void GeneratePng(Grid mapGrid, string pngURI)
        {
            double dpi = App.mvm.SelectedDPI;
            double scale = dpi / 96;
            Rect bounds = VisualTreeHelper.GetDescendantBounds(mapGrid);
            RenderTargetBitmap rt = new RenderTargetBitmap((int)(bounds.Width * scale),
                                                           (int)(bounds.Height * scale),
                                                            dpi,
                                                            dpi,
                                                            PixelFormats.Pbgra32);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(mapGrid);
                ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
            }
            rt.Render(dv);
            PngBitmapEncoder png = new PngBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(rt));
            using (Stream streamPng = File.Create(pngURI))
            {
                png.Save(streamPng);
            }
        }

我想我是从网上找到的一些代码改编而成的。