将错误的纸张尺寸发送到打印机 [英] Sending wrong paper size to printer

查看:114
本文介绍了将错误的纸张尺寸发送到打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经启动了一个打印项目,但是在告诉打印机选择哪种纸张尺寸时,我们完全陷于困境。

We have started a project for printing, however we are completely stuck when it comes to telling the printer what paper size is selected.

每次我们选择纸张尺寸并点击打印,尽管我们打开了打印首选项,选择了正确的尺寸,但打印机预览每次都显示A4,而不是我们选择的尺寸。

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.

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期望如果我们选择A5,它将告诉打印驱动程序打印A5,

I expect if we select A5, it tells the print driver to print A5,

如果我们选择自定义的用户定义纸张尺寸,它将告诉打印机选择了哪种尺寸。而且不是每次都在A4上解决这个问题

If we select a custom "user defined" paper size, it tells the printer which size is selected. And not fixing this at A4 everytime

我们似乎无法在打印对话框之外设置纸张尺寸。

We cant seem to set the paper size outside the print dialog.

推荐答案

我已经遍历了您的代码,

I have walk through your code,

我认为前端(XAML)在特定用例上引发了一些事件,即覆盖 Properties.Settings.Default中的实际值。

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代码。

It would be better to resolve if you provide a XAML code for this issue.

我可以研究一下,并将为您提供更好的解决方案。

I can look into it and will give you better solution.

您可以在此处分享我的代码,这是我的skype:shsakariya

You can share me code here is my skype : shsakariya

这篇关于将错误的纸张尺寸发送到打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆