简体   繁体   English

将错误的纸张尺寸发送到打印机

[英]Sending wrong paper size to printer

We have started a project for printing, however we are completely stuck when it comes to telling the printer what paper size is selected. 我们已经启动了一个打印项目,但是在告诉打印机选择哪种纸张尺寸时,我们完全陷入了困境。

Everytime we select the paper size and hit print, the printer preview is showing A4 everytime and not our selected size although if we open the print preferences the correct size is selected. 每次选择纸张尺寸并进行打印时,打印机预览每次都会显示A4,而不是我们选择的尺寸,尽管如果我们打开打印首选项会选择正确的尺寸。

namespace CPrint
{
    /// <summary>
    /// Логика взаимодействия для ucPrint.xaml
    /// </summary>
    public partial class ucPrint : UserControl
    {
        bool SystemChange = false;
        double? PaperHeight = null;
        double? PaperWidth = null;
        public ucPrint()
        {
            InitializeComponent();
            App.Localization.AddControls(this, new string[]
             {
                    "cHeader", "lPrinter", "lCopies","lLayout", "bPrintSettings","lColorManagement","lPrinterProfile", "lPositionSize", "cCenter", "lTop", "lLeft"
             });
        }

        public static BitmapSource ConvertColorProfile(BitmapSource bitmapSource, ColorContext sourceProfile, ColorContext destinationProfile)
        {
            var bitmapConverted = new ColorConvertedBitmap();
            bitmapConverted.BeginInit();
            bitmapConverted.Source = bitmapSource;
            //bitmapConverted.SourceColorContext = new ColorContext(PixelFormats.Pbgra32);//  bitmapSourceFrame.ColorContexts == null ? sourceProfile : bitmapSourceFrame.ColorContexts[0];
            bitmapConverted.SourceColorContext = sourceProfile;
            bitmapConverted.DestinationColorContext = destinationProfile;
            bitmapConverted.DestinationFormat = PixelFormats.Bgra32;
            bitmapConverted.EndInit();
            return bitmapConverted;
        }

        private void BPrint_Click(object sender, RoutedEventArgs e)
        {
            if (cPrinter.SelectedItem == null) { MessageBox.Show("Printer not set"); return; }
            if (cPaperSize.SelectedItem == null) { MessageBox.Show("Paper size not set"); return; }


            double marging = 30;

            if (App.CurrentTemplateControl != null)
            {
                var img = App.CurrentTemplateControl.GetImage(true);
                if (img == null) return;
                var image = new Image() { Source = img };

                if (cColorProfile != null && cColorProfile.SelectedItem != null && cColorProfile.SelectedIndex > 0)
                {
                    Uri sourceProfileUri = new Uri((cColorProfile.SelectedItem as FileInfo).FullName);
                    image.Source = ConvertColorProfile(image.Source as BitmapSource, new ColorContext(PixelFormats.Pbgra32), new ColorContext(sourceProfileUri));
                }

                if (cMirror.IsChecked == true)
                {
                    var transformGroup = new TransformGroup();
                    transformGroup.Children.Add(new ScaleTransform(-1, 1, img.Width / 2, img.Height / 2));
                    image.RenderTransform = transformGroup;
                }
                PrintDialog printDialog2 = new PrintDialog();
                Size size = (Size)(cPaperSize.SelectedItem as ComboBoxItem).DataContext;
                printDialog2.PrintQueue = new PrintQueue(new PrintServer(), cPrinter.Text);


                //if (printDialog2.ShowDialog() == true)
                //{
                //Size size = new Size(printDialog2.PrintableAreaWidth, printDialog2.PrintableAreaHeight);

                printDialog2.PrintTicket = new PrintTicket()
                {
                    PageMediaSize = new PageMediaSize(size.Width, size.Height)
                };
                //printDialog2.PrintTicket
                Canvas canvas = new Canvas()
                {
                    //Height = PrintContext.ToPx(size.Height),
                    //Width = PrintContext.ToPx(size.Width),
                    Height = size.Height,
                    Width = size.Width,
                    Background = Brushes.White
                };
                canvas.Children.Add(image);

                double scaleW = (size.Width - marging * 2) / img.Width;
                double scaleH = (size.Height - marging * 2) / img.Height;
                if (scaleW < 1 || scaleH < 1)
                {
                    Canvas.SetLeft(image, marging);
                    Canvas.SetTop(image, marging);
                    double scale = scaleW > scaleH ? scaleH : scaleW;
                    var transformGroup = new TransformGroup();
                    transformGroup.Children.Add(new ScaleTransform(scale, scale, 0, 0));
                    image.RenderTransform = transformGroup;
                }
                else if (cCenter.IsChecked == true)
                {
                    Canvas.SetLeft(image, size.Width / 2 - img.Width / 2);
                    Canvas.SetTop(image, size.Height / 2 - img.Height / 2);
                }
                else
                {
                    Canvas.SetLeft(image, marging);
                    Canvas.SetTop(image, marging);
                }
                printDialog2.PrintVisual(canvas, "Print");
                //}
            }


            return;

        }

        private void CPrinter_DropDownOpened(object sender, EventArgs e)
        {
            SystemChange = true;
            var lastPrinterName = cPrinter.Text;
            cPrinter.Items.Clear();
            int index = -1;
            cPrinter.SelectedIndex = index;
            foreach (string strPrinter in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                index++;
                cPrinter.Items.Add(strPrinter);
                if (strPrinter == lastPrinterName)
                    cPrinter.SelectedIndex = index;
            }
            SystemChange = false;
        }

        private void CPrinter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0 && SystemChange == false)
            {
                var printer = new System.Drawing.Printing.PrinterSettings();
                printer.PrinterName = e.AddedItems[0].ToString();
                var lastPaperName = cPaperSize.Text;
                cPaperSize.Items.Clear();
                int index = -1;
                cPaperSize.SelectedIndex = index;
                foreach (System.Drawing.Printing.PaperSize paper in printer.PaperSizes)
                {
                    index++;
                    cPaperSize.Items.Add(new ComboBoxItem() { Content = paper.PaperName, DataContext = new Size(paper.Width, paper.Height) });
                    if (paper.PaperName == lastPaperName)
                        cPaperSize.SelectedIndex = index;
                }
                Properties.Settings.Default.DefaultDirectPrinter = printer.PrinterName;
                Properties.Settings.Default.Save();
            }
        }

        private void CPaperSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Properties.Settings.Default.DefaultDirectPaper = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
                Properties.Settings.Default.Save();
            }
        }

        public void UpdateControls()
        {
            SystemChange = true;

            if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultDirectPrinter))
            {
                SystemChange = true;
                var lastPrinterName = cPrinter.Text;
                cPrinter.Items.Clear();
                int index = -1;
                cPrinter.SelectedIndex = index;
                foreach (string strPrinter in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
                {
                    index++;
                    cPrinter.Items.Add(strPrinter);
                    if (strPrinter == Properties.Settings.Default.DefaultDirectPrinter)
                        cPrinter.SelectedIndex = index;
                }
                SystemChange = false;

                if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultDirectPaper))
                {
                    var printer = new System.Drawing.Printing.PrinterSettings();
                    printer.PrinterName = Properties.Settings.Default.DefaultDirectPrinter;
                    string lastPaperName = Properties.Settings.Default.DefaultDirectPaper;
                    cPaperSize.Items.Clear();
                    int indexP = -1;
                    cPaperSize.SelectedIndex = indexP;
                    foreach (System.Drawing.Printing.PaperSize paper in printer.PaperSizes)
                    {
                        indexP++;
                        cPaperSize.Items.Add(new ComboBoxItem() { Content = paper.PaperName, DataContext = new Size(paper.Width, paper.Height) });
                        if (paper.PaperName == lastPaperName)
                            cPaperSize.SelectedIndex = indexP;
                    }
                }
            }

            if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultDirectColorProfile))
            {
                var lastValue = Properties.Settings.Default.DefaultDirectColorProfile;
                cColorProfile.Items.Clear();
                int index = -1;
                cColorProfile.SelectedIndex = index;
                cColorProfile.Items.Add("");
                index++;
                foreach (var file in App.Icc.items)
                {
                    index++;
                    cColorProfile.Items.Add(file);
                    if (file.FullName == lastValue)
                        cColorProfile.SelectedIndex = index;
                }
            }
            SystemChange = false;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void CColorProfile_DropDownOpened(object sender, EventArgs e)
        {
            var lastValue = cColorProfile.Text;
            cColorProfile.Items.Clear();
            int index = -1;
            cColorProfile.SelectedIndex = index;
            cColorProfile.Items.Add("");
            index++;
            foreach (var file in App.Icc.items)
            {
                index++;
                cColorProfile.Items.Add(file);
                if (file.Name == lastValue)
                    cColorProfile.SelectedIndex = index;
            }
        }

        private void CColorProfile_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!SystemChange)
            {
                Properties.Settings.Default.DefaultDirectColorProfile = (cColorProfile.SelectedItem as FileInfo)?.FullName;
                Properties.Settings.Default.Save();
            }
        }
    }
}

I expect if we select A5, it tells the print driver to print A5, 我希望如果我们选择A5,它会告诉打印驱动程序打印A5,

If we select a custom "user defined" paper size, it tells the printer which size is selected. 如果我们选择自定义的“用户定义”纸张尺寸,它将告诉打印机选择了哪种尺寸。 And not fixing this at A4 everytime 而且不是每次都将其固定在A4上

We cant seem to set the paper size outside the print dialog. 我们似乎无法在打印对话框之外设置纸张尺寸。

I believe; 我相信; following this and this MSDN article; 此MSDN文章之后; you are going to want to do something along the lines of: 您将要按照以下方式进行操作:

System.Drawing.Printing.PaperSize paperSize = new System.Drawing.Printing.PaperSize("custom", width, height);
PrintDocument printDoc = new PrintDocument();
printDoc.DefaultPageSettings.PaperSize = paperSize;

I have walk through your code, 我已经遍历了您的代码,

I think some event raise by front end (XAML) on specific use cases that is the overriding the actual value in "Properties.Settings.Default." 我认为前端(XAML)在特定用例上会引发一些事件,这是“ Properties.Settings.Default”中的实际值的替代。

It would be better to resolve if you provide a XAML code for this issue. 如果您为此问题提供XAML代码,则最好解决。

I can look into it and will give you better solution. 我可以调查一下,并将为您提供更好的解决方案。

You can share me code here is my skype : shsakariya 您可以在这里分享我的代码是我的Skype:shsakariya

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM