c# 截屏代码

//方法一
       public static void GetScreen1()
        {
            //截取屏幕内容
            Size screen = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Bitmap memoryImage = new Bitmap(screen.Width, screen.Height);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            memoryGraphics.CopyFromScreen(0, 0, 0, 0, screen, CopyPixelOperation.MergePaint);
            //memoryImage
            //memoryImage.Save(@"screen.jpg",ImageFormat.Jpeg);
            MemoryStream data = new MemoryStream();
            memoryImage.Save(data, ImageFormat.Png);
        }
//方法二
    public class ScreenCapture
    {
        /// <summary>
        /// Creates an Image object containing a screen shot of the entire desktop
        /// </summary>
        /// <returns></returns>
        public static Image CaptureScreen()
        {
            return CaptureWindow(User32.GetDesktopWindow());
        }

        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        private static Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr 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
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY | GDI32.CAPTUREBLT);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return img;
        }

        /// <summary>
        /// Helper class containing Gdi32 API functions
        /// </summary>
        private class GDI32
        {
            public const int CAPTUREBLT = 1073741824;
            public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
            [DllImport("gdi32.dll")]
            public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
                int nWidth, int nHeight, IntPtr hObjectSource,
                int nXSrc, int nYSrc, int dwRop);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
                int nHeight);
            [DllImport("gdi32.dll")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteDC(IntPtr hDC);
            [DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            [DllImport("gdi32.dll")]
            public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
        }

        /// <summary>
        /// Helper class containing User32 API functions
        /// </summary>
        private class User32
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }
            [DllImport("user32.dll")]
            public static extern IntPtr GetDesktopWindow();
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowDC(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
        }
    }

c# 截屏代码

时间: 2024-11-08 21:05:34

c# 截屏代码的相关文章

ios截屏代码[转]

http://www.cnblogs.com/chenxiangxi/p/3547974.html 这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员很有帮助 http://www.2cto.com/kf/201310/250228.html 我将其改为以下代码: 1 #pragma mark -=====自定义截屏位置大小的逻辑代码=====- 2 static int ScreenshotIndex=0; //这里的逻辑直接采用上面博主的逻辑

iOS截屏代码

/** *截图功能 */ -(void)screenShot{ UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0); //设置截屏大小 [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGrap

Android新姿势:截屏代码整理

今天做项目要用到android截屏功能,一开始我还庆幸看过一些博客的文章,自信能轻松解决...- - 结果坑了一天才搞了个差不多的交差...哎! 关于android截屏的代码,大致有3种方法,有兴趣的看下去吧. 方法一: 网上看了很多文章,大多用的是这样的方法,直接把一个View转换成Bitmap,然后保存到sd卡. /** * 根据view来生成bitmap图片,可用于截图功能 */ public static Bitmap getViewBitmap(View v) { v.clearFoc

ios摇一摇截屏代码

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //1.添加一个视图 UIView *greenView=[[UIView alloc]init]; greenView.frame=CGRectMake(100, 100, 100, 100); greenView.b

截屏代码

命名空间: using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; 相关代码 //获得当前屏幕的分辨率 Screen scr = Screen.PrimaryScreen; Rectangle rc = scr.Bounds; int iWidth = rc.Width; int iHeight = rc.Height; //创建一个和屏幕一样大的Bitmap Image myImage =

C# winform 截屏代码

try { Image image = new Bitmap(width, height); Graphics g = Graphics.FromImage(image); g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height)); string hour = DateTime.Now.Minute.ToString(); string second = DateTime.Now.Second.ToString();

android源码解析(二十六)--&gt;截屏事件流程

今天这篇文章我们主要讲一下Android系统中的截屏事件处理流程.用过android系统手机的同学应该都知道,一般的android手机按下音量减少键和电源按键就会触发截屏事件(国内定制机做个修改的这里就不做考虑了).那么这里的截屏事件是如何触发的呢?触发之后android系统是如何实现截屏操作的呢?带着这两个问题,开始我们的源码阅读流程. 我们知道这里的截屏事件是通过我们的按键操作触发的,所以这里就需要我们从android系统的按键触发模块开始看起,由于我们在不同的App页面,操作音量减少键和电

截屏 多难未遂

看到未遂,就知道这只是一篇分享感受的文章,但是,或多或少,有过同样过程的人至少会有一点同感..或有一点思路以及灵感. 其实网上关于截屏的demo很多,以及思路也很多.我也不知道自己是哪里出了问题,总是未遂. 但是还是有几个值得注意的: 不管什么FrameBuffer呀,还是Android源码里面截屏的ScreenShot..还是ScreenControll...and...这里要去掏Android更深,更底层或是最原始的都是需要root的...但是,本人root真不是很在行. 开始,我问了很多大

代码截屏

//代码截屏//    UIGraphicsBeginImageContext(view.frame.size);//    //对哪个视图截图给定大小的图片//    [ view.layer renderInContext:UIGraphicsGetCurrentContext()];//    //    //获取截图的图片对象//    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();//    //结束绘制图片