martes, 18 de enero de 2011

Funciones gráficas (Captura de Pantalla utilizando C#)


En seguida un ejemplo de como usar GDI para capturar el contenido del escritorio, o una sección del mismo, en seguida el código.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using F;

namespace F.Graphics
{
    /// <summary>
    /// 
    /// </summary>
    public sealed class Common
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Bitmap CaptureScreen() {
            
            Bitmap bmp = new Bitmap (Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
            g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            
            return bmp;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Image CaptureWindow(Form frm)
        {
            
            Bitmap bmp = new Bitmap(frm.Bounds.Width , frm.Bounds.Height, PixelFormat.Format32bppArgb);

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (bmp);
            g.CopyFromScreen(new Point(frm.Bounds.Left, frm.Bounds.Top), Point.Empty, frm.Bounds.Size);

            return bmp;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        public Image CaptureWindow(IntPtr handle)
        {
             
            // get te hDC of the target window
            IntPtr hdcSrc =F.Win.User32.GetWindowDC(handle);
            // get the size
            F.Win.User32.RECT windowRect = new F.Win.User32.RECT();
            F.Win.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
            IntPtr hdcDest = F.Win.GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = F.Win.GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = F.Win.GDI32.SelectObject(hdcDest, hBitmap);
            // bitblt over
            F.Win.GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, F.Win.GDI32.SRCCOPY);
            // restore selection
            F.Win.GDI32.SelectObject(hdcDest, hOld);
            // clean up 
            F.Win.GDI32.DeleteDC(hdcDest);
            F.Win.User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            F.Win.GDI32.DeleteObject(hBitmap);
            return img;
        }

        
    }
}

Transacciones Fiori

  /UI2/CACHE Register service for UI2 cache use /UI2/CACHE_DEL Delete cache entries /UI2/CHIP Chip Registration /UI2/CUST Customizing of UI ...