namespace ScreenShot_Grab { static class Program { private static MainForm WinForm; /// <summary> /// . /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); _hookID = SetHook(_proc); Application.Run(new MainForm()); UnhookWindowsHookEx(_hookID); } private const int WH_KEYBOARD_LL = 13; //private const int WH_KEYBOARD_LL = 13; private const int VK_F1 = 0x70; private static LowLevelKeyboardProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; 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) { Keys number = (Keys)Marshal.ReadInt32(lParam); //MessageBox.Show(number.ToString()); if (number == Keys.PrintScreen) { if (wParam == (IntPtr)261 && Keys.Alt == Control.ModifierKeys && number == Keys.PrintScreen) { // Alt+PrintScreen } else if (wParam == (IntPtr)257 && number == Keys.PrintScreen) { // PrintScreen } } } return CallNextHookEx(IntPtr.Zero, 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); } }
class ScreenCapturer { public enum CaptureMode { Screen, Window } [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect); [StructLayout(LayoutKind.Sequential)] public struct Rect { public int Left; public int Top; public int Right; public int Bottom; } public Bitmap Capture(CaptureMode screenCaptureMode = CaptureMode.Window) { Rectangle bounds; if (screenCaptureMode == CaptureMode.Screen) { bounds = Screen.GetBounds(Point.Empty); CursorPosition = Cursor.Position; } else { var handle = GetForegroundWindow(); var rect = new Rect(); GetWindowRect(handle, ref rect); bounds = new Rectangle(rect.Left, rect.Top, rect.Right, rect.Bottom); //CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top); } var result = new Bitmap(bounds.Width, bounds.Height); using (var g = Graphics.FromImage(result)) { g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size); } return result; } public Point CursorPosition { get; protected set; } }
private void save_Click(object sender, EventArgs e) { if (lastres == null) { return; } // base36 Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var FileName = base_convert(unixTimestamp.ToString(), 10, 36); lastres.Save(spath + FileName); }
// bitmap byte[] private Byte[] BitmapToArray(Bitmap bitmap) { if (bitmap == null) return null; using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImgFormat[Properties.Settings.Default.format]); return stream.ToArray(); } } private void upload_Click(object sender, EventArgs e) { using (var client = new WebClient()) { client.Headers.Add("Content-Type", "application/octet-stream"); try { var response = client.UploadData(svurl, BitmapToArray(lastres); var result = Encoding.UTF8.GetString(response); if (result.StartsWith("http")) { System.Diagnostics.Process.Start(result); } } catch { } } }
<?php $file = file_get_contents("php://input"); $id = base_convert(time(), 10, 36); file_put_contents("img/".$id.".png",$file); echo "http://".$_SERVER['SERVER_NAME']."/img/".$id.".png"; ?>
private void edit_Click(object sender, EventArgs e) { if (lastres == null) return; if (lastfile == "") save_Click(sender, e); Process.Start("mspaint.exe", "\"" + lastfile + "\""); }
private void PreviewForm_Load(object sender, EventArgs e) { if (form1.lastfile!="") { img.Image = Image.FromFile(form1.lastfile); } else { img.Image = form1.lastres; } ClientSize = new Size(img.Image.Width + 10, img.Image.Height + 10); img.Width = img.Image.Width+10; img.Height = img.Image.Height+10; if (img.Image.Width >= Screen.PrimaryScreen.Bounds.Width || img.Image.Height >= Screen.PrimaryScreen.Bounds.Height) { WindowState = FormWindowState.Maximized; } CenterToScreen(); }
// private ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return codec; } } return null; } internal void SaveFile(string FilePath, ImageFormat format) { var curimg = lastres; if (format == ImageFormat.Jpeg) { System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; ImageCodecInfo Encoder = GetEncoder(format); EncoderParameters myEncoderParameters = new EncoderParameters(1); myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, Properties.Settings.Default.quality); curimg.Save(stream, Encoder, myEncoderParameters); } else { curimg.Save(FilePath, format); } }
internal ResourceManager LocM = new ResourceManager("ScreenShot_Grab.Resources.WinFormStrings", typeof(MainForm).Assembly); LocM.GetString("key_name");
private void ChangeLanguage(string lang) { foreach (Form frm in Application.OpenForms) { localizeForm(frm); } } private void localizeForm(Form frm) { var manager = new ComponentResourceManager(frm.GetType()); manager.ApplyResources(frm, "$this"); applyResources(manager, frm.Controls); } private void applyResources(ComponentResourceManager manager, Control.ControlCollection ctls) { foreach (Control ctl in ctls) { manager.ApplyResources(ctl, ctl.Name); Debug.WriteLine(ctl.Name); applyResources(manager, ctl.Controls); } } private void language_SelectedIndexChanged(object sender, EventArgs e) { var lang = ((ComboboxItem)language.SelectedItem).Value; if (Properties.Settings.Default.language == lang) return; UpdateLang(lang); } private void UpdateLang(string lang) { Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang); ChangeLanguage(lang); Properties.Settings.Default.language = lang; Properties.Settings.Default.Save(); form1.OnLangChange(); } private void Form2_Load(object sender, EventArgs e) { language.Items.Clear(); foreach (CultureInfo item in GetSupportedCulture()) { var lc = item.TwoLetterISOLanguageName; var citem = new ComboboxItem(item.NativeName, lc); //Debug.WriteLine(item.NativeName); // if (item.Name == CultureInfo.InvariantCulture.Name) { lc = "ru"; citem = new ComboboxItem("", lc); } language.Items.Add(citem); if (Properties.Settings.Default.language == lc) { language.SelectedItem = citem; } } } private IList<CultureInfo> GetSupportedCulture() { //Get all culture CultureInfo[] culture = CultureInfo.GetCultures(CultureTypes.AllCultures); //Find the location where application installed. string exeLocation = Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)); //Return all culture for which satellite folder found with culture code. IList<CultureInfo> cultures = new List<CultureInfo>(); foreach(var cultureInfo in culture) { if (Directory.Exists(Path.Combine(exeLocation, cultureInfo.Name))) { cultures.Add(cultureInfo); } } return cultures; }
[DllImport(@"dwmapi.dll")] private static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out Rect pvAttribute, int cbAttribute); public Bitmap Capture(CaptureMode screenCaptureMode = CaptureMode.Window, bool cutborder = true) { ... var handle = GetForegroundWindow(); var rect = new Rect(); // Win XP if (Environment.OSVersion.Version.Major < 6) { GetWindowRect(handle, ref rect); } else { var res = -1; try { res = DwmGetWindowAttribute(handle, 9, out rect, Marshal.SizeOf(typeof(Rect))); } catch { } if (res<0) GetWindowRect(handle, ref rect); } ...
private void uploadfile(bool bitmap = true) { byte[] data; if (bitmap && !imgedit) { data = BitmapToArray(lastres); } else { if (!File.Exists(lastfile)) { MessageBox.Show(LocM.GetString("file_nf"), LocM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return; } data = File.ReadAllBytes(lastfile); } HttpContent bytesContent = new ByteArrayContent(data); using (var client = new HttpClient()) using (var formData = new MultipartFormDataContent()) { ... formData.Add(bytesContent, "image", "image"); try { var response = client.PostAsync(url, formData).Result; if (!response.IsSuccessStatusCode) { MessageBox.Show(response.ReasonPhrase, LocM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); lastlabel.Text = LocM.GetString("error"); lastlabel.Enabled = false; } else { ... }
using (var client = new WebClient()) { var pdata = new NameValueCollection(); ... pdata.Add("image", Convert.ToBase64String(data)); try { var response = client.UploadValues(url, "POST", pdata); var result = Encoding.UTF8.GetString(response); ...
$file = base64_decode($_POST["image"]);
<startup> <supportedRuntime version="v4.0"/> <supportedRuntime version="v2.0.50727"/> </startup>
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls; try { System.Net.ServicePointManager.SecurityProtocol |= (SecurityProtocolType)3072; //SecurityProtocolType.Tls12; } catch { }
private static bool pressed = false; private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { Keys number = (Keys)Marshal.ReadInt32(lParam); //MessageBox.Show(number.ToString()); if (number == Keys.PrintScreen) { if (pressed && wParam == (IntPtr)261 && Keys.Alt == Control.ModifierKeys && number == Keys.PrintScreen) { var res = Scr.Capture(ScreenCapturer.CaptureMode.Window, Properties.Settings.Default.cutborder); WinForm.OnGrabScreen(res, false, true); pressed = false; } else if (pressed && wParam == (IntPtr)257 && number == Keys.PrintScreen) { var res = Scr.Capture(ScreenCapturer.CaptureMode.Screen); WinForm.OnGrabScreen(res); pressed = false; } else if (wParam == (IntPtr)256 || wParam == (IntPtr)260) { pressed = true; // fix for win xp double press } } } return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam); }
Source: https://habr.com/ru/post/316012/
All Articles