aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.RemoteDesktop/CaptureMethods/BitBltScreenCapture.cs
blob: d8f1e214c2532711313f5b3942696674cd5f0d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Tango.RemoteDesktop.CaptureMethods
{
    /// <summary>
    /// Represents a BitBlt screen capture method.
    /// </summary>
    /// <seealso cref="Tango.RemoteDesktop.ICaptureMethod" />
    public class BitBltScreenCapture : ICaptureMethod
    {
        #region Win32 API Screen shot calls

        // Win32 API calls necessary to support screen capture
        [DllImport("gdi32", EntryPoint = "BitBlt", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int BitBlt(int hDestDC, int x, int y, int nWidth, int nHeight, int hSrcDC, int xSrc,
                                         int ySrc, int dwRop);

        [DllImport("user32", EntryPoint = "GetDC", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int GetDC(int hwnd);

        [DllImport("user32", EntryPoint = "ReleaseDC", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int ReleaseDC(int hwnd, int hdc);

        #endregion

        /// <summary>
        /// Gets the desktop bitmap.
        /// </summary>
        /// <param name="region">The capture region.</param>
        /// <returns></returns>
        public Bitmap GetDesktopBitmap(CaptureRegion region)
        {
            const int SRCCOPY = 13369376;

            Bitmap bitmap = new Bitmap(region.Width, region.Height);

            using (Graphics g = Graphics.FromImage(bitmap))
            {
                // Get a device context to the windows desktop and our destination  bitmaps
                int hdcSrc = GetDC(0);
                IntPtr hdcDest = g.GetHdc();

                // Copy what is on the desktop to the bitmap
                BitBlt(hdcDest.ToInt32(), 0, 0, region.Width, region.Height, hdcSrc, 0, 0, SRCCOPY);

                // Release device contexts
                g.ReleaseHdc(hdcDest);
                ReleaseDC(0, hdcSrc);
            }

            return bitmap;
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        public void Dispose()
        {
            //Do nothing.
        }
    }
}
pan class="p">.ParseExact(dateString, "dd-MM-yyyy_HH-mm-ss", CultureInfo.InvariantCulture); logFiles.Add(new LogFile() { DateTime = date, File = file, PartOfSet = indexOfFile > 0, }); } } catch (Exception ex) { LogManager.Default.Log(ex, $"Could not load application log file {Path.GetFileName(file)}"); } } return logFiles; } public List<LogItemBase> Parse(LogFile logFile) { List<LogItemBase> logItems = new List<LogItemBase>(); List<LogFile> logFiles = new List<LogFile>(); FileLogger logger = LogManager.Default.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger)) as FileLogger; if (logFile.PartOfSet) { string fileName = Path.GetFileNameWithoutExtension(logFile.File); string extension = Path.GetExtension(logFile.File); int indexPos = fileName.IndexOf(FileLogger.FILE_SET_EXTENSION); if (indexPos > 0) { fileName = fileName.Substring(0, indexPos); } string[] fileEntries = Directory.GetFiles(FileLogger.DefaultLogsFolder, $"{fileName}*{extension}").Where(x => Path.GetFileName(x).StartsWith(logger.Tag) && x != logger.LogFile).OrderBy(x => x).ToArray(); foreach (var file in fileEntries) { Parse(file, logFile.DateTime, ref logItems); } } else { Parse(logFile.File, logFile.DateTime, ref logItems); } return logItems; } public List<LogItemBase> Parse(String file, DateTime fileDate) { List<LogItemBase> logs = new List<LogItemBase>(); Parse(file, fileDate, ref logs); return logs; } private void Parse(string file, DateTime datetime, ref List<LogItemBase> logItems) { if(File.Exists(file)) { String text = File.ReadAllText(file); var logs = Regex.Split(text, @"(\[\d{2}:\d{2}:\d{2}.\d{2}\])"); for (int i = 1; i < logs.Length; i += 2) { try { DateTime date = DateTime.ParseExact(logs[i].Replace("[", "").Replace("]", ""), "HH:mm:ss.ff", CultureInfo.InvariantCulture); String rest = logs[i + 1]; var matches = Regex.Matches(rest, @"(?<=\[)(.*?)(?=\])"); MessageLogItem item = new MessageLogItem(); item.TimeStamp = new DateTime(datetime.Year, datetime.Month, datetime.Day, date.Hour, date.Minute, date.Second, date.Millisecond); item.Category = (LogCategory)Enum.Parse(typeof(LogCategory), matches[0].ToString()); item.CallerFile = matches[1].ToString(); item.CallerMethodName = matches[2].ToString(); item.CallerLineNumber = int.Parse(matches[3].ToString()); int messageStartIndex = matches[3].Index + matches[3].Length + 2; item.Message = rest.Substring(messageStartIndex, rest.Length - messageStartIndex); logItems.Add(item); } catch (Exception ex) { LogManager.Default.Log(ex, "Could not parse log line: " + logs[i]); } } } } } }