Я написал тестовый проект для печати на C # с предварительно напечатанной формой - System.Drawing.Printing.PrintDocument.Цель этого - напечатать изображение из файла на бумаге нестандартного размера.Перед печатью изображение уменьшается в соответствии с размером бумаги.
public PrintDocument printDoc = new PrintDocument();
.....
private void PrintButton_Click(object sender, EventArgs e)
{
string FileName = "D:\\temp\\testprint.png";
try
{
if (string.IsNullOrWhiteSpace(FileName)) return; // Prevents execution of below statements if filename is not selected.
PrintDocument pd = new PrintDocument();
PaperSize paperSize = new PaperSize("TEST PAPER SIZE", 50, 50);
paperSize.RawKind = (int)PaperKind.Custom;
//Disable the printing document pop-up dialog shown during printing.
PrintController printController = new StandardPrintController();
pd.PrintController = printController;
//For testing only: Hardcoded set paper size to particular paper.
pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
pd.DefaultPageSettings.PaperSize = paperSize;
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrintPage += (sndr, args) =>
{
System.Drawing.Image i = System.Drawing.Image.FromFile(FileName);
args.Graphics.PageUnit = System.Drawing.GraphicsUnit.Millimeter;
//Adjust the size of the image to the page to print the full image without loosing any part of the image.
System.Drawing.Rectangle m = args.MarginBounds;
//Logic below maintains Aspect Ratio.
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
//Calculating optimal orientation.
pd.DefaultPageSettings.Landscape = m.Width > m.Height;
//Putting image in center of page.
m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2);
m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2);
args.Graphics.DrawImage(i, m);
};
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Масштабирование изображения работает нормально, но размер бумаги не меняется, оно всегда остается на уровне A4.Я много гуглил и применил некоторые параметры, но все еще не могу установить пользовательские свойства бумаги.Может ли кто-нибудь помочь мне выяснить, в чем проблема?Или хотя бы указать мне, куда копать?
Заранее спасибо!