/* Copyright (c) Citrix Systems Inc. * All rights reserved. * * Redistribution and use in source and binary forms, * with or without modification, are permitted provided * that the following conditions are met: * * * Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Runtime.InteropServices; namespace DotNetVnc { public class GraphicsUtils { private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); [DllImport("kernel32.dll")] private extern static short QueryPerformanceCounter(ref long x); [DllImport("kernel32.dll")] private extern static short QueryPerformanceFrequency(ref long x); private static long ctr1 = 0, ctr2 = 0, freq = 0; public static void startTime() { QueryPerformanceCounter(ref ctr1); } public static double endTime(String msg) { QueryPerformanceCounter(ref ctr2); QueryPerformanceFrequency(ref freq); double time = (ctr2 - ctr1) * 1.0 / freq; Log.Info(msg + " time: " + time + " seconds."); ctr1 = ctr2 = freq = 0; return time; } public static void copyRect(Bitmap src, int x, int y, int width, int height, Graphics dest, int dx, int dy) { IntPtr pTarget = dest.GetHdc(); IntPtr pSource = CreateCompatibleDC(pTarget); IntPtr pOrig = SelectObject(pSource, src.GetHbitmap()); BitBlt(pTarget, dx, dy, width, height, pSource, x, y, (uint)TernaryRasterOperations.SRCCOPY); IntPtr pNew = SelectObject(pSource, pOrig); DeleteObject(pNew); DeleteDC(pSource); dest.ReleaseHdc(pTarget); } [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] private static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] private static extern bool DeleteDC(IntPtr hdc); [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] private static extern bool DeleteObject(IntPtr hObject); /// /// Performs a bit-block transfer of the color data corresponding to a /// rectangle of pixels from the specified source device context into /// a destination device context. /// /// Handle to the destination device context. /// The leftmost x-coordinate of the destination rectangle (in pixels). /// The topmost y-coordinate of the destination rectangle (in pixels). /// The width of the source and destination rectangles (in pixels). /// The height of the source and the destination rectangles (in pixels). /// Handle to the source device context. /// The leftmost x-coordinate of the source rectangle (in pixels). /// The topmost y-coordinate of the source rectangle (in pixels). /// A raster-operation code. /// /// true if the operation succeeded, false otherwise. /// [DllImport("gdi32.dll")] private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop); /// /// Specifies a raster-operation code. These codes define how the color data for the /// source rectangle is to be combined with the color data for the destination /// rectangle to achieve the final color. /// private enum TernaryRasterOperations : uint { /// dest = source SRCCOPY = 0x00CC0020, /// dest = source OR dest SRCPAINT = 0x00EE0086, /// dest = source AND dest SRCAND = 0x008800C6, /// dest = source XOR dest SRCINVERT = 0x00660046, /// dest = source AND (NOT dest) SRCERASE = 0x00440328, /// dest = (NOT source) NOTSRCCOPY = 0x00330008, /// dest = (NOT src) AND (NOT dest) NOTSRCERASE = 0x001100A6, /// dest = (source AND pattern) MERGECOPY = 0x00C000CA, /// dest = (NOT source) OR dest MERGEPAINT = 0x00BB0226, /// dest = pattern PATCOPY = 0x00F00021, /// dest = DPSnoo PATPAINT = 0x00FB0A09, /// dest = pattern XOR dest PATINVERT = 0x005A0049, /// dest = (NOT dest) DSTINVERT = 0x00550009, /// dest = BLACK BLACKNESS = 0x00000042, /// dest = WHITE WHITENESS = 0x00FF0062 } } }