Skip to content
This repository
tree: 08450fb96c
Fetching contributors…

Cannot retrieve contributors at this time

file 117 lines (116 sloc) 4.357 kb
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/*
* WinLDTP 1.0
*
* Author: Nagappan Alagappan <nalagappan@vmware.com>
* Copyright: Copyright (c) 2011-12 VMware, Inc. All Rights Reserved.
* License: MIT license
*
* http://ldtp.freedesktop.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.IO;
using System.Drawing;
using System.Windows;
using System.Collections;
using CookComputing.XmlRpc;
using System.Windows.Forms;
using System.Windows.Automation;

namespace Ldtpd
{
    class Image
    {
        Utils utils;
        public Image(Utils utils)
        {
            this.utils = utils;
        }
        private void LogMessage(Object o)
        {
            utils.LogMessage(o);
        }
        public string Capture(string windowName = null,
            int x = 0, int y = 0, int width = -1, int height = -1)
        {
            Bitmap b = null;
            ScreenShot ss = null;
            AutomationElement windowHandle;
            try
            {
                ss = new ScreenShot();
                // capture entire screen, and save it to a file
                string path = Path.GetTempPath() +
                    Path.GetRandomFileName() + ".png";
                if (windowName.Length > 0)
                {
                    windowHandle = utils.GetWindowHandle(windowName);
                    if (windowHandle == null)
                    {
                        throw new XmlRpcFaultException(123,
                            "Unable to find window: " + windowName);
                    }
                    windowHandle.SetFocus();
                    Rect rect = windowHandle.Current.BoundingRectangle;
                    System.Drawing.Rectangle rectangle = new
                        System.Drawing.Rectangle((int)rect.X, (int)rect.Y,
                        (int)rect.Width, (int)rect.Height);
                    b = ss.CaptureSize(path, rectangle);
                }
                else if (width != -1 && height != -1)
                {
                    System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
                        x, y, width, height);
                    b = ss.CaptureSize(path, rectangle);
                }
                else
                {
                    b = ss.Capture(path);
                }
                string encodedText = "";
                using (FileStream fs = File.Open(path, FileMode.Open,
                    FileAccess.Read))
                {
                    Byte[] bytesToEncode = new byte[fs.Length];
                    fs.Read(bytesToEncode, 0, (int)fs.Length);
                    encodedText = Convert.ToBase64String(bytesToEncode);
                    fs.Close();
                }
                LogMessage(path);
                File.Delete(path);
                return encodedText;
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (ex is XmlRpcFaultException)
                    throw;
                throw new XmlRpcFaultException(123,
                    "Unhandled exception: " + ex.Message);
            }
            finally
            {
                if (b != null) // To avoid compilation warning
                    b = null;
                ss = null;
                windowHandle = null;
            }
        }
    }
}
Something went wrong with that request. Please try again.