Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Screen Capture
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Capture
{
static class Program
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static Resident resi = null;
[STAThread]
static void Main()
{
resi = new Resident();
_hookID = SetHook(_proc);
Application.Run();
UnhookWindowsHookEx(_hookID);
}
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
// 44(Print Screen)のときのみ印刷処理を行う
if (vkCode == 44)
{
resi.Print();
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
public partial class Resident : Form
{
public Resident()
{
this.ShowInTaskbar = false;
this.SetComponents();
}
/// <summary>
/// タスクトレイのメニュー処理(アプリの終了)
/// </summary>
/// <param name="sendar"></param>
/// <param name="e"></param>
private void Close_Click(object sendar, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// アプリの設定
/// </summary>
private void SetComponents()
{
NotifyIcon icon = new NotifyIcon();
icon.Icon = Properties.Resources.Icon1;
icon.Visible = true;
icon.Text = "アプリテスト";
ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Text = "&終了";
menuItem.Click += new EventHandler(Close_Click);
menu.Items.Add(menuItem);
icon.ContextMenuStrip = menu;
}
/// <summary>
/// 印刷処理の実施
/// </summary>
public void Print()
{
CaptureScreen();
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(ReadImage);
pd.OriginAtMargins = false;
pd.DefaultPageSettings.Landscape = true;
pd.Print();
}
/// <summary>
/// キャプチャ画像を作る
/// </summary>
private void CaptureScreen()
{
Bitmap bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bm);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), bm.Size);
bm.Save("image.bmp", ImageFormat.Bmp);
g.Dispose();
}
/// <summary>
/// 印刷する画像の縮尺を行う
/// </summary>
/// <param name="sendar"></param>
/// <param name="e"></param>
private void ReadImage(object sendar, PrintPageEventArgs e)
{
//拡大率を指定.
float zoom = 1;
float padding = 20;
Image image = Image.FromFile("image.bmp");
if (image.Width > e.Graphics.VisibleClipBounds.Width)
{
zoom = e.Graphics.VisibleClipBounds.Width / image.Width;
}
if ((image.Height + padding) * zoom > e.Graphics.VisibleClipBounds.Height)
{
zoom = e.Graphics.VisibleClipBounds.Height / (image.Height + padding);
}
e.Graphics.DrawImage(image, 0, padding, image.Width * zoom, image.Height * zoom);
image.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.