public partial class ucPrint : UserControl
{
public delegate void StringDelegate(string value);
public event StringDelegate onPrinterSettingsOpen;
bool SystemChange = false;
double? PaperHeight = null;
double? PaperWidth = null;
PrintDialog GlobalPrintDialog = new PrintDialog();
PageOrientation? PageOrientation = System.Printing.PageOrientation.Portrait;
public ucPrint()
{
InitializeComponent();
App.Localization.AddControls(this, new string[]
{
"cHeader", "lPrinter", "lCopies","lLayout", "bPrintSettings","lColorManagement","lPrinterProfile", "lPositionSize", "cCenter", "lTop", "lLeft","lPrint"
});
}
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)
{
App.CheckUSB();
if (App.Trial != false)
{
MessageBox.Show("Not available in the trial version.");
return;
}
Print();
}
private void Print()
{
App.CheckUSB();
if (App.Trial != false)
{
MessageBox.Show("Not available in the trial version.");
return;
}
if (cPrinter.SelectedItem == null) { MessageBox.Show("Printer not set"); return; }
if (cPaperSize.SelectedItem == null) { MessageBox.Show("Paper size not set"); return; }
double marging = 0;
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;
}
Size tempSize = (Size)(cPaperSize.SelectedItem as ComboBoxItem).DataContext;
Size size = new Size(PageOrientation.Value == System.Printing.PageOrientation.Portrait ? tempSize.Width : tempSize.Height,
PageOrientation.Value == System.Printing.PageOrientation.Portrait ? tempSize.Height : tempSize.Width);
GlobalPrintDialog.PrintQueue = new PrintQueue(new PrintServer(), cPrinter.Text);
GlobalPrintDialog.PrintTicket = new PrintTicket()
{
PageMediaSize = new PageMediaSize(tempSize.Width, tempSize.Height),
PageOrientation = App.CurentPrinterSettings.DefaultPageSettings.Landscape ? System.Printing.PageOrientation.Landscape : System.Printing.PageOrientation.Portrait//,
//Collation = Collation.Collated,
//Duplexing = Duplexing.OneSided,
//InputBin = InputBin.Unknown,
//OutputColor = OutputColor.Unknown,
};
Canvas canvas = new Canvas()
{
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)
{
double scale = scaleW > scaleH ? scaleH : scaleW;
if (cCenter.IsChecked == true)
{
Canvas.SetLeft(image, size.Width / 2 - img.Width* scale / 2);
Canvas.SetTop(image, size.Height / 2 - img.Height * scale / 2);
}
else
{
Canvas.SetLeft(image, marging);
Canvas.SetTop(image, marging);
}
var transformGroup = new TransformGroup();
if (cMirror.IsChecked == true)
{
transformGroup.Children.Add(new ScaleTransform(-1, 1, img.Width / 2, img.Height / 2));
}
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);
}
for (int i = 0; i < cCopyCount.Value; i++)
{
canvas.UpdateLayout();
GlobalPrintDialog.PrintVisual(canvas, $"Print c{i}");
}
//}
}
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)
{
App.CurentPrinterSettings.PrinterName = e.AddedItems[0].ToString();
var lastPaperName = cPaperSize.Text;
var paperName = App.CurentPrinterSettings.DefaultPageSettings.PaperSize.PaperName;
foreach (System.Drawing.Printing.PaperSize paper in App.CurentPrinterSettings.PaperSizes)
{
if (paper.PaperName == lastPaperName)
paperName = lastPaperName;
}
cPaperSize.Items.Clear();
int index = -1;
cPaperSize.SelectedIndex = index;
foreach (System.Drawing.Printing.PaperSize paper in App.CurentPrinterSettings.PaperSizes)
{
index++;
cPaperSize.Items.Add(new ComboBoxItem() { Content = paper.PaperName, DataContext = new Size(paper.Width * 0.254 * 3.7795275590551, paper.Height * 0.254 * 3.7795275590551) });
if (paper.PaperName == paperName)
{
App.CurentPrinterSettings.DefaultPageSettings.PaperSize = paper;
cPaperSize.SelectedIndex = index;
}
}
Properties.Settings.Default.DefaultDirectPaper = App.CurentPrinterSettings.DefaultPageSettings.PaperSize.PaperName;
Properties.Settings.Default.DefaultDirectPrinter = App.CurentPrinterSettings.PrinterName;
Properties.Settings.Default.Save();
}
}
private void CPaperSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
var paperName = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
foreach (System.Drawing.Printing.PaperSize paper in App.CurentPrinterSettings.PaperSizes)
{
if (paper.PaperName == paperName)
App.CurentPrinterSettings.DefaultPageSettings.PaperSize = paper;
}
Properties.Settings.Default.DefaultDirectPaper = paperName;
Properties.Settings.Default.Save();
}
}
private void cCopyCount_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
int cnt=1;
int.TryParse(cCopyCount.Value.ToString(),out cnt);
App.CurentPrinterSettings.Copies = (short)cnt;
}
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;
App.CurentPrinterSettings.PrinterName = strPrinter;
}
}
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 * 0.254 * 3.7795275590551, paper.Height * 0.254 * 3.7795275590551) });
if (paper.PaperName == lastPaperName)
{
App.CurentPrinterSettings.DefaultPageSettings.PaperSize = paper;
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();
}
}
private void CPositionV_Click(object sender, RoutedEventArgs e)
{
Style unSelectedStyle = (Style)this.TryFindResource("SideButtonL");
Style selectedStyle = (Style)this.TryFindResource("SideButtonSL");
PageOrientation = System.Printing.PageOrientation.Portrait;
bPositionV.Style = selectedStyle;
bPositionH.Style = unSelectedStyle;
App.CurentPrinterSettings.DefaultPageSettings.Landscape = false;
}
private void CPositionH_Click(object sender, RoutedEventArgs e)
{
Style unSelectedStyle = (Style)this.TryFindResource("SideButtonL");
Style selectedStyle = (Style)this.TryFindResource("SideButtonSL");
PageOrientation = System.Printing.PageOrientation.Landscape;
bPositionV.Style = unSelectedStyle;
bPositionH.Style = selectedStyle;
App.CurentPrinterSettings.DefaultPageSettings.Landscape = true;
}
private void BPrintSettings_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrWhiteSpace(cPrinter.Text))
onPrinterSettingsOpen?.Invoke(cPrinter.Text);
var printer = App.CurentPrinterSettings;
Style unSelectedStyle = (Style)this.TryFindResource("SideButtonL");
Style selectedStyle = (Style)this.TryFindResource("SideButtonSL");
if (printer.DefaultPageSettings.Landscape == true)
{
PageOrientation = System.Printing.PageOrientation.Landscape;
bPositionV.Style = unSelectedStyle;
bPositionH.Style = selectedStyle;
}
else
{
PageOrientation = System.Printing.PageOrientation.Portrait;
bPositionV.Style = selectedStyle;
bPositionH.Style = unSelectedStyle;
}
var lastPaperName = printer.DefaultPageSettings.PaperSize.PaperName;
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 * 0.254 * 3.7795275590551, paper.Height * 0.254 * 3.7795275590551) });
if (paper.PaperName == lastPaperName)
cPaperSize.SelectedIndex = index;
}
cCopyCount.Value = printer.Copies;
Properties.Settings.Default.DefaultDirectPaper = lastPaperName;
Properties.Settings.Default.Save();
}
}
开始打印的代码,我相信是造成我们麻烦的是
Size tempSize = (Size)(cPaperSize.SelectedItem as ComboBoxItem).DataContext;
Size size = new Size(PageOrientation.Value == System.Printing.PageOrientation.Portrait ? tempSize.Width : tempSize.Height,
PageOrientation.Value == System.Printing.PageOrientation.Portrait ? tempSize.Height : tempSize.Width);
GlobalPrintDialog.PrintQueue = new PrintQueue(new PrintServer(), cPrinter.Text);
我希望我们选择A5,它会告诉打印驱动程序打印A5,
如果我们选择自定义的“用户定义”纸张尺寸,它将告诉打印机选择了哪种尺寸。而不是每次都将其固定在A4上
我们似乎无法在打印对话框之外设置纸张尺寸。