c# - 如何打印全宽窗口窗体C#。?
问题描述
我正在尝试打印完整的表格,但它打印的文档没有覆盖整个宽度。我尝试了不同的方法,但没有得到想要的结果。我已从此链接 https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-print-a-windows-form获得帮助
请告诉我如何打印完整的表格。提前致谢
解决方案
我不确定您所关注的链接,即使在尝试打印整个表单一次时尝试了多个代码后,我也遇到了同样的错误。然后我在互联网上的某个地方找到了下面的代码,它打印了整个表格。
private System.IO.Stream streamToPrint;
string streamType;
PrintDocument printDoc = new PrintDocument();
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt
(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = image.Width;
int height = image.Height;
if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
{
width = e.MarginBounds.Width;
height = image.Height * e.MarginBounds.Width / image.Width;
}
else
{
height = e.MarginBounds.Height;
width = image.Width * e.MarginBounds.Height / image.Height;
}
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
}
private void btnPrintDoc_Click(object sender, EventArgs e)
{
Graphics g1 = this.CreateGraphics();
System.Drawing.Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(@"D:\PrintPage.jpg", ImageFormat.Jpeg);
FileStream fileStream = new FileStream(@"D:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
StartPrint(fileStream, "Image");
fileStream.Close();
}
public void StartPrint(Stream streamToPrint, string streamType)
{
this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
this.streamToPrint = streamToPrint;
this.streamType = streamType;
System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
PrintDialog1.AllowSomePages = true;
PrintDialog1.ShowHelp = true;
PrintDialog1.Document = printDoc;
DialogResult result = PrintDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDoc.Print();
//docToPrint.Print();
}
}
推荐阅读
- git - 如何列出所有提交比 HEAD 更多的分支?
- html - 如何正确对齐占据中型及以上设备一半屏幕的引导模式
- python - 如何使用 seaborn violinplot 绘制值的总和(而不是计数)?
- vba - OpenRecordset 未应用查询参数
- prometheus - Prometheus remote_read 返回 405 Method Not Allowed
- java - 为什么我不能在我的项目中使用 JPA 创建 EntityManager?
- lua - 如何让 math.random 打印一个字符串
- r - 有没有办法更改 mlr3 中的默认公式?
- python - 尝试导入 numpy
- python - 如何用前几年相同时间范围的权重替换特定时间范围的值?