- private void Form1_Load(object sender, EventArgs e)
- {
- PrintDocument document = new PrintDocument();
- document.PrintPage += new PrintPageEventHandler(document_PrintPage);
- PrintPreviewDialog ppDialog = new PrintPreviewDialog();
- ppDialog.Document = document;
- ppDialog.Show();
- }
- void document_PrintPage(object sender, PrintPageEventArgs e)
- {
- PrintDocument document = (PrintDocument)sender;
- Graphics g = e.Graphics;
- Image image = Image.FromFile("path to image");
- int imgWidth = image.Width;
- int imgHeight = image.Height;
- int pageWidth = document.DefaultPageSettings.PaperSize.Width;
- int pageHeight = document.DefaultPageSettings.PaperSize.Height;
- if (imgWidth > pageWidth)
- {
- int diff = imgWidth - pageWidth;
- imgWidth = pageWidth;
- imgHeight -= diff;
- }
- Rectangle rect = new Rectangle(0,0,imgWidth,imgHeight);
- g.DrawImage(image, rect );
- }
C# - Display An Image In PrintPreviewDialog
This code snippet shows how to resize and display an image in a PrintPreviewDialog.
No comments:
Post a Comment