C# 在单页c上打印两张图像#
我想在一页上打印两张图片。 我尝试了下面的代码,但它正在不同的页面上打印所有图像C# 在单页c上打印两张图像#,c#,winforms,printing,C#,Winforms,Printing,我想在一页上打印两张图片。 我尝试了下面的代码,但它正在不同的页面上打印所有图像 public void PD_PrintPage(object sender, PrintPageEventArgs e) { float W = e.MarginBounds.Width; float H = e.MarginBounds.Height; for (; FileCounter >= 0; FileCounter--)
public void PD_PrintPage(object sender, PrintPageEventArgs e)
{
float W = e.MarginBounds.Width;
float H = e.MarginBounds.Height;
for (; FileCounter >= 0; FileCounter--)
{
try
{
Bitmap Bmp = new Bitmap(BmpFiles[FileCounter]);
if (Bmp.Width / W < Bmp.Height / H)
W = Bmp.Width * H / Bmp.Height;
else
H = Bmp.Height * W / Bmp.Width;
e.Graphics.DrawImage(Bmp, 0, 0, W, H);
break;
}
catch
{
}
}
FileCounter -= 1;
if (FileCounter > 0)
{
e.HasMorePages = true;
}
else
{
FileCounter = BmpFiles.Length - 1;
}
}
public void PD_PrintPage(对象发送方,PrintPageEventArgs e)
{
浮动W=e.边缘边界宽度;
浮动H=e.边缘边界高度;
对于(;FileCounter>=0;FileCounter--)
{
尝试
{
位图Bmp=新位图(BmpFiles[FileCounter]);
如果(Bmp.Width/W0)
{
e、 HasMorePages=true;
}
其他的
{
FileCounter=BmpFiles.Length-1;
}
}
这将打印不同页面中的所有图像
我想要一些功能,可以打印一个图像,留下一些空间,如果空间剩余,可以在同一页中再次打印其他图像。打印文档的工作方式如下: 首先,程序到达printPage函数,读取所有代码,如果存在一行e.hasMorePages=true;,则在函数末尾,然后,程序从头开始重新进入函数,再次读取代码,将其打印到下一页,并继续执行,直到读取一行e.hasMorepages=false。。所以你不必在函数中加一个循环。您需要做的是在类中生成变量,并在打印作业完成后递增或递减变量,以生成满足e.hasMorePages=false的条件
public void PD_PrintPage(object sender, PrintPageEventArgs e)
{
float W = e.MarginBounds.Width;
// if you are calculating the whole page margin, then split it to carry 2 images
float H = e.MarginBounds.Height / 2;
// for space btwn images
H -= 5.0;
// First Image
try
{
Bitmap Bmp = new Bitmap(BmpFiles[FileCounter]);
if (Bmp.Width / W < Bmp.Height / H)
W = Bmp.Width * H / Bmp.Height;
else
H = Bmp.Height * W / Bmp.Width;
e.Graphics.DrawImage(Bmp, 0, 0, W, H);
break;
}
catch
{
}
FileCounter --;
if (FileCounter < 0) goto goDaddy;
//Second Img
try
{
Bitmap Bmp = new Bitmap(BmpFiles[FileCounter]);
if (Bmp.Width / W < Bmp.Height / H)
W = Bmp.Width * H / Bmp.Height;
else
H = Bmp.Height * W / Bmp.Width;
e.Graphics.DrawImage(Bmp, 0, H + 2.5, W, H);
break;
}
catch
{
}
FileCounter --;
goDaddy:;
e.HasMorePages = (FileCounter >= 0)
}
public void PD_PrintPage(对象发送方,PrintPageEventArgs e)
{
浮动W=e.边缘边界宽度;
//如果要计算整个页面的边距,请将其拆分为两幅图像
浮动H=e.边缘边界高度/2;
//用于空间btwn图像
H-=5.0;
//第一图像
尝试
{
位图Bmp=新位图(BmpFiles[FileCounter]);
如果(Bmp.Width/W=0)
}
我没有检查代码,只是尝试向您展示概念。在您的代码中,您每页只打印一个图像,因为您在
try
末尾使用break语句离开循环。如果可以只打印一个图像(第二个图像没有足够的空间)或两个图像(实现了您想要的),那么您应该根据决定动态地离开循环,而不是无条件地使用break
//只要有文件要打印,最多打印两个图像的for循环
对于(int i=0;i<2&&FileCounter>=0;i++)
{
//这里是您的打印代码,仅与draw调用一起指示
e、 DrawImage(Bmp,0,0,W,H);
//打印图像后,减小文件计数器
文件计数器--;
//绘制图像后,检查是否有足够的空间放置下一个图像
//如果没有足够的空间,请将循环保留为中断
如果(条件)
打破
}
目前,我没有足够的声誉在这一页上发表评论。。。所以:永远不要像“Sayka”在回答中所建议的那样使用“goto”。这真是糟糕的风格&编码我发现它工作得非常好,不会丢失我在使用内存流时体验到的任何图像质量
private void printBothGraphs_Click(object sender, EventArgs e)
{
PrintPreviewDialog custom = new PrintPreviewDialog();
custom.ClientSize = new Size(1000, 750);
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
pd.DefaultPageSettings.Color = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPageBoth);
custom.Document = pd;
custom.ShowDialog();
}
private void pd_PrintPageBoth(object sender, PrintPageEventArgs ev)
{
// Create and initialize print font
System.Drawing.Font printFont = new System.Drawing.Font("Arial", 10);
ev.PageSettings.Color = true;
// Create Rectangle structure, used to set the position of the chart Rectangle
Rectangle myRec = new System.Drawing.Rectangle(ev.MarginBounds.X, ev.MarginBounds.Y, ev.MarginBounds.Width, ev.MarginBounds.Height / 2);
Rectangle myRec2 = new System.Drawing.Rectangle(ev.MarginBounds.X, ev.MarginBounds.Height / 2 + 90, ev.MarginBounds.Width, ev.MarginBounds.Height / 2);
//dataChart and outputGraph are two mscharts in my form
dataChart.Printing.PrintPaint(ev.Graphics, myRec);
outputGraph.Printing.PrintPaint(ev.Graphics, myRec2);
}
如何检查是否有足够的空间放置下一幅图像EventArguments中有PageBounds属性。它们表示页面的总面积。你也知道自己形象的高度。其他一切都是数学。记住0;0位于左上角!使用属性的高度(或y)并减去图像的高度和一些偏移,以在图像之间留出空间。然后检查剩余区域的高度是否足以容纳您的图像。此外,你可能还应该在页面的边框上留一些空间。通常,创建打印方法的结果是,如果所有方法都符合你的要求,就可以进行尝试。这并不好,但这是把事情安排好的唯一方法。使用pdf打印机(如freepdf)对此有帮助;)
private void printBothGraphs_Click(object sender, EventArgs e)
{
PrintPreviewDialog custom = new PrintPreviewDialog();
custom.ClientSize = new Size(1000, 750);
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
pd.DefaultPageSettings.Color = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPageBoth);
custom.Document = pd;
custom.ShowDialog();
}
private void pd_PrintPageBoth(object sender, PrintPageEventArgs ev)
{
// Create and initialize print font
System.Drawing.Font printFont = new System.Drawing.Font("Arial", 10);
ev.PageSettings.Color = true;
// Create Rectangle structure, used to set the position of the chart Rectangle
Rectangle myRec = new System.Drawing.Rectangle(ev.MarginBounds.X, ev.MarginBounds.Y, ev.MarginBounds.Width, ev.MarginBounds.Height / 2);
Rectangle myRec2 = new System.Drawing.Rectangle(ev.MarginBounds.X, ev.MarginBounds.Height / 2 + 90, ev.MarginBounds.Width, ev.MarginBounds.Height / 2);
//dataChart and outputGraph are two mscharts in my form
dataChart.Printing.PrintPaint(ev.Graphics, myRec);
outputGraph.Printing.PrintPaint(ev.Graphics, myRec2);
}