0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# クリップボードの画像を、即プリンタ出力する。ユーザ名・コンピュータ名・日時も付与。

Posted at

C# クリップボードの画像を、即プリンタ出力する。
ユーザ名・コンピュータ名・日時も付与。

clip2print.cs
//c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:exe clip2print.cs
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;

class clip2print
{
	static void Main()
	{
		System.Threading.Thread t = new System.Threading.Thread(GetClipboardText);
		t.SetApartmentState(System.Threading.ApartmentState.STA);
		t.Start();
		t.Join();
	}

	private static void pd_PrintPage(object sender,
		System.Drawing.Printing.PrintPageEventArgs e)
	{
		Image img = System.Windows.Forms.Clipboard.GetImage();
		Console.WriteLine("img 高さ:{0} 幅:{1}", img.Height, img.Width);
		Rectangle r = e.MarginBounds;
		double rr = (double)r.Height / (double)r.Width;
		double ir = (double)img.Height / ((double)img.Width+1);
		Console.WriteLine("rr:{0} ir:{1}", rr, ir);
		if(rr < ir){
			if(r.Height > img.Height){
				r.Width = img.Width;
				r.Height = img.Height;
				Console.WriteLine("1");
			}else{
				double rt = (double)r.Height/(double)img.Height;
				r.Width = (int)(rt*(double)img.Width);
				r.Height = (int)(rt*(double)img.Height);
				Console.WriteLine("2");
			}
		}else{
			if(r.Width > img.Width){
				r.Width = img.Width;
				r.Height = img.Height;
				Console.WriteLine("3");
			}else{
				double rt = (double)r.Width/(double)img.Width;
				r.Width = (int)(rt*(double)img.Width);
				r.Height = (int)(rt*(double)img.Height);
				Console.WriteLine("4");
			}
		}
		Console.WriteLine("X:{0} Y:{1}", e.MarginBounds.X, e.MarginBounds.Y);
		Console.WriteLine("高さ:{0} 幅:{1}", e.MarginBounds.Height, e.MarginBounds.Width);
		Console.WriteLine("上:{0} 左:{1}", e.MarginBounds.Top, e.MarginBounds.Left);
		Console.WriteLine("rw:{0} rh:{1}", r.Width, r.Height);
		e.Graphics.DrawImage(img,r);
		
		Font fnt = new Font("MS UI Gothic", 16);
		DateTime dt = DateTime.Now;
		e.Graphics.DrawString(Environment.UserName + " " + Environment.MachineName + " " + dt, fnt, Brushes.Blue, 0, 0);
		e.HasMorePages = false;
		img.Dispose();
	}

	static void GetClipboardText()
	{
		try{
			if (System.Windows.Forms.Clipboard.GetImage() != null)
			{
				Image img = System.Windows.Forms.Clipboard.GetImage();
				Bitmap sub1 = new Bitmap(img);
				System.Drawing.Printing.PrintDocument pd =
					new System.Drawing.Printing.PrintDocument();
				pd.PrintPage +=
					new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);
				pd.Print();
			}else{
				Console.WriteLine("クリップボードに画像がない");
			}
		}
		catch(Exception ex){
			MessageBox.Show("エラー\n"+ex);
		}
	}
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?