Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to print aspect ratio / full page

Tags:

c#

I am printing the CHART control on button click:

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();

How do I force it to print the control so that it fits to the size of the page (preserving the aspect ratio)?

like image 866
Alex Gordon Avatar asked Nov 17 '10 16:11

Alex Gordon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

There are at least two different ways to do it, both include scaling the image to be printed to fit the page size of the selected printer:

1) using .NET facilities (haven't tested it myself, lifted from this post):

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Image i = pictureBox1.Image;

        float newWidth = i.Width * 100 / i.HorizontalResolution;
        float newHeight = i.Height * 100 / i.VerticalResolution;

        float widthFactor = newWidth / e.MarginBounds.Width;
        float heightFactor = newHeight / e.MarginBounds.Height;

        if(widthFactor>1 | heightFactor > 1)
        {
            if(widthFactor > heightFactor)
            {
                newWidth = newWidth / widthFactor;
                newHeight = newHeight / widthFactor;
            }
            else
            {
                newWidth = newWidth / heightFactor;
                newHeight = newHeight / heightFactor;
            }
        }
        e.Graphics.DrawImage(i, 0, 0, (int)newWidth, (int)newHeight);
    }
}

2) P/Invoke'ing Windows API calls from the GDI and flat GDI (this is much more complex but faster and you can pass a plain byte array of a bitmap file (read file as byte[]), provide an email to me if need this code):

  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);
like image 69
user44298 Avatar answered Oct 08 '22 07:10

user44298