C#-如何打印纵横比/整页

C#-如何打印纵横比/整页,c#,C#,我正在单击按钮打印图表控件: chart1.SaveImage(ms, ChartImageFormat.Bmp); Bitmap bm = new Bitmap(ms); PrintDocument doc = new PrintDocument(); doc.PrintPage += (s, ev) => { ev.Graphics.DrawImage(bm, Point.Empty); // adjust this to put the image elsewhere

我正在单击按钮打印图表控件:

chart1.SaveImage(ms, ChartImageFormat.Bmp);
Bitmap bm = new Bitmap(ms);

PrintDocument doc = new PrintDocument();
doc.PrintPage += (s, ev) =>
{
    ev.Graphics.DrawImage(bm, Point.Empty); // adjust this to put the image elsewhere
    ev.HasMorePages = false;
};
doc.DefaultPageSettings.Landscape = true;

doc.Print();

如何强制打印控件以使其适合页面大小(保留纵横比)?

至少有两种不同的方法,都包括缩放要打印的图像以适合所选打印机的页面大小:

1) 使用设施(我自己还没有测试过,从这里提升):

2) 来自GDI和平面GDI的调用(这要复杂得多,但速度更快,您可以传递位图文件的纯字节数组(将文件读取为byte[]),如果需要此代码,请向我发送电子邮件):


这是我为让它工作所做的。令人恼火的是,图表控件只会将其内容绘制为与屏幕上的大小相同的大小。我发现解决这个问题的唯一方法是手动调整大小。然后,由于它可能不喜欢在表单上调整大小,因此这还需要临时将其从表单中删除

因此,所有这些,我最终得到了这样的结果:

Chart ChartBox { get; private set; }

...

var oldParent = ChartBox.Parent;
var oldSize = ChartBox.Size;

ChartBox.Parent = null;
ChartBox.Size = new Size(width, height);

ChartBox.Printing.Print();

ChartBox.Parent = oldParent;
ChartBox.Size = oldSize;

根据您的表单,您可能还需要保存和恢复图表控件的其他属性。

@ivo这很好,但太复杂了,我认为可能已经有了某种方法print.fullpage()或类似的方法that@i我是一个女孩,当我不得不这么做的时候,我的研究结论是没有使用.Net或win32提供的API。我知道这是令人惊讶的-你可以在.Net中做很多事情,但是像这样愚蠢的事情需要爬山,但我很确定这是真的。可能有第三方LIB可以做到这一点,但我没有时间进行研究。具体来说,你需要保留外观是一个逻辑问题,所以我猜这就是为什么微软从来没有提供简单的功能来实现它,因为它是一个处理图像大小调整的功能,它可以是任何大小,这个例子就是其中之一cases@i我是一个女孩,所以是的,我也找了很长时间的printFullPage()功能:)但没有,所以我必须写我自己的,给我电子邮件,可以发送给你,因为它太长和专有,所以我不想在这里发布它(还必须允许我到明天剥离一些逻辑定制,这是我公司现在的财产)。但是第一种方法,我认为,如果有效的话,也不算太坏,是吗?很短,试试看。@ivo非常感谢你愿意帮助frumrecordsGMAIL@i我是一个女孩,检查。PS:我不知道为什么约翰·桑德斯编辑了这个问题的标题(标签是的),但为什么要编辑这个标题?联系他,告诉他你根本没有指定GDI+,我认为他的编辑是错误的。我没有编辑自己的特权
  private static extern bool ClosePrinter(IntPtr hPrinter);
  private static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
  private static extern int SetJob(IntPtr hPrinter, int JobId, int Level, ref byte pJob, int Command_Renamed);
  private static extern int GdiplusStartup(out IntPtr token, ref StartupInput input, out StartupOutput output);
  private static extern int GdiplusShutdown(IntPtr token);
  internal static extern int GdipLoadImageFromStream([In, MarshalAs(UnmanagedType.Interface)]IStream stream, out IntPtr image);
  internal static extern int GdipDisposeImage(IntPtr image);
  static internal extern int GdipCreateFromHDC2(IntPtr hDC, IntPtr hDevice, out IntPtr graphics);
  static internal extern int GdipDeleteGraphics(IntPtr graphics);
  static internal extern int GdipReleaseDC(IntPtr graphics, IntPtr hdc);
  internal static extern int GdipGetImageDimension(IntPtr image, out float width, out float height);
  internal static extern int GdipGetDpiX(IntPtr graphics, out float dpi);
  internal static extern int GdipGetDpiY(IntPtr graphics, out float dpi);
  static internal extern int GdipDrawImageRectI(IntPtr graphics, IntPtr image, int x, int y, int width, int height);
  private static extern IntPtr CreateDC([MarshalAs(UnmanagedType.LPStr)] string lpszDriver, [MarshalAs(UnmanagedType.LPStr)] string lpszDevice, [MarshalAs(UnmanagedType.LPStr)] string lpszOutput, IntPtr lpInitData);
  private static extern bool DeleteDC(IntPtr hdc);
  private static extern int StartDoc(IntPtr hdc, DOCINFO lpdi);
  private static extern int EndDoc(IntPtr hdc);
  private static extern int StartPage(IntPtr hdc);
  private static extern int EndPage(IntPtr hdc);
  private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
Chart ChartBox { get; private set; }

...

var oldParent = ChartBox.Parent;
var oldSize = ChartBox.Size;

ChartBox.Parent = null;
ChartBox.Size = new Size(width, height);

ChartBox.Printing.Print();

ChartBox.Parent = oldParent;
ChartBox.Size = oldSize;