⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 打印图像.txt

📁 打印图像,装入图片,然后打印
💻 TXT
字号:
using System.Drawing.Printing;

private DocumentBase _document;

public class DocumentBase:PrintDocument
{
      public DialogResult ShowPrintPreviewDialog()
      {
        PrintPreviewDialog dialog=new PrintPreviewDialog();
	dialog.Document=this;
 	return dialog.ShowDialog();
      }
}

public class ImageDocument:DocumentBase
{
      private Image _image;
      public Image Image
      {
	get
	{
	   return _image;
	}
	set
	{
	  _image=value;
	}
      }

     public ImageDocument()
    {
    }

    public ImageDocument(Image image)
    {
       this.Image=image;
    }

    protected override void OnPrintPage(PrintPageEventArgs e)
    {
	if(Image==null)
	throw new InvalidOperationException();
	e.Graphics.DrawImage(Image,e.MarginBounds);
    }

}

//第一个按钮打开一幅图片
private void button1_Click(object sender, System.EventArgs e)
{
	if(openFileDialog1.ShowDialog(this)==DialogResult.OK)
	{
	   try
           {
             pictureBox1.Image=Image.FromFile(openFileDialog1.FileName);
	     _document=new ImageDocument(pictureBox1.Image);
           } 
           catch(Exception ex)
           {
             MessageBox.Show("无法打开此图片!"+ex.Message);
	   }
        }
}
//第二个按钮打印预览
private void button2_Click(object sender, System.EventArgs e)
{
       if(_document==null)
       {
          MessageBox.Show("必须先打开一幅图片!");
          return;
       }
       _document.ShowPrintPreviewDialog();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -