晒一下c#截屏对象code

昨天,因为需要一个截屏的功能所以自己就写了一下。第一版是一个单纯的利用.net类库来实现的屏幕方式。

        static public void PrintSystemScreen (string file) {

            
if (string.IsNullOrEmpty(file)) {
                
// Create a random file name by GUID and set the saved 
                
// directory at current directory.
                file = Guid.NewGuid().ToString();
            }


            
// Create a bitmap for save
            Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                    Screen.PrimaryScreen.Bounds.Height,
                                    PixelFormat.Format32bppPArgb);

            
// Create a graphic for drawing from bmp
            Graphics screenG = Graphics.FromImage(bmp);
            screenG.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                   Screen.PrimaryScreen.Bounds.Y,
                                   
00,
                                   Screen.PrimaryScreen.Bounds.Size,
                                   CopyPixelOperation.SourceCopy);

            ImageParams imgParam 
= new ImageParams(file);
            bmp.Save(imgParam.FilePath, imgParam.CodeInfo, imgParam.EncoderParam);
        }


第二版,我加入了根据给出的应用程序主窗口去截取界面的功能,使用Win32 Api来实现:
    /// <summary>
    
/// GeneralScreen include serval functions.
    
/// Capture single screenshot.
    
/// Capture application screenshot.
    
/// !Capture web page screenshot.
    
/// </summary>

    static public class GeneralScreen {

        
/// <summary>
        
/// Capturing the current screen operation then save
        
/// image to the indicated file.
        
/// </summary>
        
/// <param name="file"></param>

        static public void PrintSystemScreen (string file) {
            IntPtr screenHandle 
= NativeMethods.GetDesktopWindow();
            Capture(screenHandle, file);
        }


        
/// <summary>
        
/// Create a random file name by GUID under the runtime directory.
        
/// </summary>

        static public void PrintSystemScreen () {
            PrintSystemScreen(
null);
        }


        
/// <summary>
        
/// Capturing the indicated application window operation then save
        
/// image to the indicated file. If the file parameter not set, function will
        
/// create a random file name by GUID under the runtime directory.
        
/// </summary>
        
/// <param name="handle">window's handle</param>
        
/// <param name="file">target image file</param>

        static public void PrintApplicationScreen (IntPtr handle, string file) {
            
if (NativeMethods.SetForegroundWindow(handle)) {
                Capture(handle, file);
            }

        }



        
Helper
    }



    
/// <summary>
    
/// The nativeMethods includes the win32 api for the others 
    
/// class calling.
    
/// </summary>

    internal static class NativeMethods {
        
// Methods
        [DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true)]
        
internal static extern int BitBlt (IntPtr destDC, int xDest, int yDest, int width, int height, IntPtr sourceDC, int xSource, int ySource, uint rasterOperation);
        [DllImport(
"gdi32.dll", SetLastError = true, ExactSpelling = true)]
        
internal static extern IntPtr CreateCompatibleBitmap (IntPtr dc, int width, int height);
        [DllImport(
"gdi32.dll", SetLastError = true, ExactSpelling = true)]
        
internal static extern IntPtr CreateCompatibleDC (IntPtr dc);
        [DllImport(
"gdi32.dll", EntryPoint = "CreateDCW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        
internal static extern IntPtr CreateDC (string driver, IntPtr device, IntPtr output, IntPtr devMode);
        [DllImport(
"gdi32.dll", ExactSpelling = true)]
        
internal static extern int DeleteDC (IntPtr dc);
        [DllImport(
"gdi32.dll", ExactSpelling = true)]
        
internal static extern int DeleteObject (IntPtr gdiObject);
        [DllImport(
"gdi32.dll", SetLastError = true, ExactSpelling = true)]
        
internal static extern IntPtr SelectObject (IntPtr dc, IntPtr gdi);

        [DllImport(
"user32.dll")]
        [
return: MarshalAs(UnmanagedType.Bool)]
        
internal static extern bool GetWindowRect (IntPtr hWnd, out RECT lpRect);
        [StructLayout(LayoutKind.Sequential)]
        
public struct RECT {
            
public int Left;
            
public int Top;
            
public int Right;
            
public int Bottom;
        }


        [DllImport(
"user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        [
return: MarshalAs(UnmanagedType.Bool)]
        
internal static extern bool SetForegroundWindow (IntPtr hWnd);

        [DllImport(
"user32.dll")]
        
public static extern IntPtr GetDesktopWindow ();
    }
在这里我用了一个辅助的类ImageParam,这个类可以根据给出的文件名自己识别需要生成什么格式的图片文件。
完整的Demo工程从这里下载
posted @ 2008-03-06 09:38 moonz-wu 阅读(405) 评论(3)  编辑 收藏 所属分类: .Net阶进之路

  回复  引用    
#1楼 2008-04-23 15:00 | boy5d [未注册用户]
好文
  回复  引用    
#2楼 2008-04-26 16:05 | 在线代理 [未注册用户]
不知道是纯C#的好,还是调用了win32 api的好。
望指点一二,
  回复  引用  查看    
#3楼 [楼主]2008-04-27 07:53 | 木子文武      
@在线代理
如果需要的功能,通过纯的C#代码能实现,我建议还是使用c#,这样可以保证移植和一致性,但是在很多情况下,.net的框架好像没有全部的封装Wins API,这样我们也就只能通过调用API来实现了。

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: