Form1.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace WindowsFormsPrintPreviewControl
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. printPreviewControl1.Document = printDocument1;
  22. }
  23. private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
  24. {
  25. //获取应用程序的起始路径
  26. string str = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
  27. //读取路径下的img.jpg文件
  28. str += @"\img.jpg";
  29. //将读取的图片绘制到画布上
  30. e.Graphics.DrawImage(Image.FromFile(str), 10, 10, 607, 452);
  31. }
  32. }
  33. }