PDA

View Full Version : پرینت فرم در وسط صفحه



Nikpendar1
دوشنبه 17 اردیبهشت 1397, 10:49 صبح
دوستان عزیز سلام
با استفاده از کد زیر میتونم از فرم پرینت بگیرم و مشکلی نداره ولی پرینت رو گوشه صفحه میندازه و من میخوام پرینت در وسط صفحه قرار بگیره.
لطفا راهنمایی کنید.

کد پرینت فرم:
Bitmap memoryImage;
public void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
private void PrintButton_Click(object sender, EventArgs e)
{

CaptureScreen();


PrintDialog printDlg = new PrintDialog();
printDlg.AllowSelection = true;
printDlg.AllowSomePages = true;
//Call ShowDialog
if (printDlg.ShowDialog() == DialogResult.OK)
{
printDocument2.PrinterSettings = printDlg.PrinterSettings;
printDocument2.Print();

}

}
private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}

Mahmoud.Afrad
دوشنبه 24 اردیبهشت 1397, 23:25 عصر
نقطه آغاز رو اصلاح کنید
private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int x = (e.PageBounds.Right - memoryImage.Width) / 2;
int y = (e.PageBounds.Bottom - memoryImage.Height) / 2;
e.Graphics.DrawImage(memoryImage, x, y);
}