Ejemplo n.º 1
0
        ///////////////////////////////////////////////////////////////////////////////
        // Eventhandler                                                              //
        ///////////////////////////////////////////////////////////////////////////////
        #region EVENTS

        ///////////////////////////////////////////////////////////////////////////////
        // Eventhandler for UI, Menu, Buttons, Toolbars etc.                         //
        ///////////////////////////////////////////////////////////////////////////////
        #region WINDOWSEVENTHANDLER
        #endregion //WINDOWSEVENTHANDLER

        ///////////////////////////////////////////////////////////////////////////////
        // Eventhandler for Custom Defined Events                                    //
        ///////////////////////////////////////////////////////////////////////////////
        #region CUSTOMEVENTHANDLER
        #endregion //CUSTOMEVENTHANDLER

        #endregion //EVENTS

        ///////////////////////////////////////////////////////////////////////////////
        // Methods and Eventhandling for Background tasks                            //
        ///////////////////////////////////////////////////////////////////////////////
        #region BACKGROUNDWORKER
        #endregion //BACKGROUNDWORKER

        ///////////////////////////////////////////////////////////////////////////////
        // Inherited methods                                                         //
        ///////////////////////////////////////////////////////////////////////////////
        #region OVERRIDES
        #endregion //OVERRIDES

        ///////////////////////////////////////////////////////////////////////////////
        // Methods for doing main class job                                          //
        ///////////////////////////////////////////////////////////////////////////////
        #region METHODS

        /// <summary>
        /// Applies a color Matrix to a given Image.
        /// E.g. transforms image to grayscale.
        /// </summary>
        /// <param name="currentImage">image to apply color matrix to</param>
        /// <param name="colorMatrix">color matrix to apply</param>
        /// <returns><strong>True</strong> if succeeded, otherwise <strong>false</strong>.</returns>
        private static bool DrawAdjustedImage(Image currentImage, ColorMatrix colorMatrix)
        {
            Bitmap          bmp            = new Bitmap(currentImage);
            Rectangle       rc             = new Rectangle(0, 0, currentImage.Width, currentImage.Height);
            Graphics        graphicsObject = Graphics.FromImage(currentImage);
            ImageAttributes imgattr        = new ImageAttributes();

            try
            {
                imgattr.SetColorMatrix(colorMatrix);
                graphicsObject.DrawImage(
                    bmp,
                    rc,
                    0,
                    0,
                    currentImage.Width,
                    currentImage.Height,
                    GraphicsUnit.Pixel,
                    imgattr);

                return(true);
            }
            catch (Exception ex)
            {
                VGExceptionMethods.HandleException(ex);

                return(false);
            }
            finally
            {
                imgattr.Dispose();
                bmp.Dispose();
                graphicsObject.Dispose();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Captures a screen shot of a specific window of given size,
        /// even if it is hidden.
        /// </summary>
        /// <param name="windowHandle">A <see cref="IntPtr"/> to the window to capture.
        /// (In windows forms, this is obtained by the Handle property)</param>
        /// <param name="size">The <see cref="Size"/> of the captured image.</param>
        /// <returns>An <see cref="Bitmap"/> with the captured window if successful,
        /// otherwise null.</returns>
        /// <exception cref="ArgumentNullException">Thrown, when size is empty,
        /// or width ot height are less than zero.</exception>
        public static Bitmap GetWindowImage(IntPtr windowHandle, Size size)
        {
            if (size.IsEmpty || size.Height < 0 || size.Width < 0)
            {
                throw new ArgumentException("The window size should not be zero");
            }

            Bitmap   bmp = new Bitmap(size.Width, size.Height);
            Graphics g   = Graphics.FromImage(bmp);
            IntPtr   dc  = g.GetHdc();

            try
            {
                User32.PrintWindow(windowHandle, dc, 0); // flags:0 means PW_CLIENTONLY
            }
            catch (Exception ex)
            {
                VGExceptionMethods.HandleException(ex);

                return(null);
            }
            finally
            {
                g.ReleaseHdc();
                g.Dispose();
            }

            return(bmp);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Saves given image into a file. Filename is requested via <see cref="SaveFileDialog"/>.
        /// </summary>
        /// <param name="image">Image to save to disk</param>
        /// <returns><strong>True</strong> if succeeded, otherwise <strong>false</strong>.</returns>
        public static bool ExportImageToFile(Image image)
        {
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Title            = "Please enter filename for image...";
            dlg.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
            dlg.Filter           = "JPEG Format - jpg|*.jpg|Bitmap Format - bmp|*.bmp|Graphics Interchange Format - gif|*.gif|Portable Networks Graphic - png|*.png|Tag Image File Format - tif|*.tif|Windows MetaFile Format - wmf|*.wmf";
            dlg.FileName         = "*.jpg";
            dlg.AddExtension     = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                ImageFormat format;
                switch (dlg.FilterIndex)
                {
                case 1:
                    format = ImageFormat.Jpeg;
                    break;

                case 2:
                    format = ImageFormat.Bmp;
                    break;

                case 3:
                    format = ImageFormat.Gif;
                    break;

                case 4:
                    format = ImageFormat.Png;
                    break;

                case 5:
                    format = ImageFormat.Tiff;
                    break;

                case 6:
                    format = ImageFormat.Wmf;
                    break;

                default:
                    format = ImageFormat.Jpeg;
                    break;
                }

                try
                {
                    image.Save(dlg.FileName, format);
                }
                catch (Exception ex)
                {
                    VGExceptionMethods.HandleException(ex);

                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">A <see cref="IntPtr"/> to the window to capture.
        /// (In windows forms, this is obtained by the Handle property)</param>
        /// <returns>An <see cref="Image"/> with the captured window if successfull,
        /// otherwise null.</returns>
        /// <remarks>This method uses the GDI32.BitBlt()
        /// method, so the window has to be visible and should not be
        /// minimized or hidden for example. In this cases use
        /// <see cref="GetWindowImage(IntPtr,Size)"/> which uses
        /// User32.PrintWindow().
        /// </remarks>
        public static Image CaptureWindow(IntPtr handle)
        {
            IntPtr hdcSrc  = IntPtr.Zero;
            IntPtr hdcDest = IntPtr.Zero;
            IntPtr bitmap  = IntPtr.Zero;
            Image  img;

            try
            {
                // get te hDC of the target window
                hdcSrc = User32.GetWindowDC(handle);

                // get the size
                User32.RECT windowRect = new User32.RECT();
                User32.GetWindowRect(handle, ref windowRect);
                int width  = windowRect.Right - windowRect.Left;
                int height = windowRect.Bottom - windowRect.Top;

                // create a device context we can copy to
                hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);

                // create a bitmap we can copy it to,
                // using GetDeviceCaps to get the width/height
                bitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

                // select the bitmap object
                IntPtr old = Gdi32.SelectObject(hdcDest, bitmap);

                // bitblt over
                Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.TernaryRasterOperations.SRCCOPY);

                // restore selection
                Gdi32.SelectObject(hdcDest, old);

                // get a .NET image object for it
                img = Image.FromHbitmap(bitmap);
            }
            catch (Exception ex)
            {
                VGExceptionMethods.HandleException(ex);

                return(null);
            }
            finally
            {
                // clean up
                Gdi32.DeleteDC(hdcDest);
                User32.ReleaseDC(handle, hdcSrc);

                // free up the Bitmap object
                Gdi32.DeleteObject(bitmap);
            }

            return(img);
        }