俗话说,一个好汉十个帮,众人拾柴火焰高等都说明一个道理,有更多的资源,更丰富的积累,都是助你走向成功,走向顶峰的推动力。作为一个有多年开发经验的技术人员或者是初级的编程菜鸟,有着一些成熟、使用熟练的公用类库,能很高程度提升自己的软件开发能力和思维能力。另外,丰富的类库也是.NET的各种知识点的汇聚,经常查看优化,也是对自己能力深层次的提升。本篇继续公用类库系列的介绍,不同于前面几篇的主题,本篇主要介绍图片相关方面的类库操作。
到目前为止,类库的优化及内容介绍、例子实现同步进行,下面是最近的类库帮助文档整理截图。

1、全屏截图辅助控件 ScreenCaptureWindow

实现效果
1)本辅助类主要是用来方便实现对屏幕进行局部截图的功能,类似QQ截图的效果。

2)截图的时候,底图是当前屏幕的图像,加了灰度背景,当鼠标单击并拉动的时候,出现局部加亮区域,这个就是要截取的图片内容。图片截图后,用户可以实现自定义的保存等操作。

实现代码
1)辅助类提供的方法接口如下所示:

///     
/// 获取或设置截图的图片对象    
///     
public Bitmap BitmapCache    

///     
/// 图片截图结束后的事件处理    
///     
public event EventHandler BitmapCropped;    

///     
/// 拖动截图开始    
///     
public Point DragStart    

///     
/// 拖动截图结束    
///     
public Point DragStop

2)辅助类ScreenCaptureWindow的使用例子代码如下所示(增加图片保存到文件的自定义操作)

public partial class Form1 : Form    
 {    
     private ScreenCaptureWindow captureWindow = null;    

     public Form1()    
     {    
         InitializeComponent();    
     }    

     private void captureWindow_BitmapCropped(object sender, EventArgs e)    
     {    
         if (captureWindow.DragStop != captureWindow.DragStart)    
         {    
             bool AutoSaveImages = false;    
             if (AutoSaveImages)    
             {    
                 string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);    
                 string fileName = \"Snap_\" + DateTime.Now.Ticks.ToString() + Guid.NewGuid().ToString().Substring(0, 2) + \".jpg\";    
                 captureWindow.BitmapCache.Save(Path.Combine(path, fileName), ImageFormat.Jpeg);    
             }    
         }    
     }    

     private void btnScreenCapture_Click(object sender, EventArgs e)    
     {    
         if (captureWindow != null)    
         {    
             captureWindow.BitmapCropped -= new EventHandler(captureWindow_BitmapCropped);    
             captureWindow.Dispose();    
             captureWindow = null;    
         }    
         if (captureWindow == null)    
         {    
             captureWindow = new ScreenCaptureWindow();    
             captureWindow.BitmapCropped += new EventHandler(captureWindow_BitmapCropped);    
         }    
         captureWindow = new ScreenCaptureWindow();    
         captureWindow.Show();    
         captureWindow.TopMost = true;    
         captureWindow.TopMost = false;    
     }    
 }

最简化的操作就是只是把截图的图片放到在剪切板里面(默认),实现操作如下所示:

private ScreenCaptureWindow captureWindow = null;    
private void btnScreenCapture_Click(object sender, EventArgs e)    
{    
    captureWindow = new ScreenCaptureWindow();    
    captureWindow.Show();    
    captureWindow.TopMost = true;    
    captureWindow.TopMost = false;    
}

2、本辅助类主要是用来方便实现获取文件、文件夹、扩展名的图标信息

IconReaderHelper**。**

实现效果 1)本辅助类主要是用来方便实现窗体的各种动画效果,包括创建、关闭、隐藏、移动状态的动画,动画支持滚动、滑动、各种方向变换、透明状态等。

实现代码
1)辅助类提供的方法接口如下所示

 ///    
/// 指定图标尺寸大小选项    
///     
public enum IconSize    

///     
/// 指定文件夹是打开还是关闭状态选项    
///     
public enum FolderType    

///     
/// 返回给定文件的图标    
///     
/// 文件路径名    
/// 大图标还是小图标    
/// 是否包含链接图标    
/// System.Drawing.Icon    
public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay)    

///     
/// 用于访问系统文件夹图标。    
///     
/// 大图标还是小图标    
/// 文件夹是打开还是关闭状态    
/// System.Drawing.Icon    
public static Icon GetFolderIcon(IconSize size, FolderType folderType)    

///     
/// 获取文件或者文件夹图标的显示名称    
///     
/// 文件或文件夹路径    
/// 是否为文件夹    
///     
public static string GetDisplayName(string name, bool isDirectory)   


#region 后缀名图标操作    

///     
/// 添加扩展名小图标对象到ImageList集合中,并返回位置;如果存在,则返回对应的位置。    
///     
///     
///     
///     
public static int GetIcon(ImageList images, string extension)    

///     
/// 添加扩展名图标对象到ImageList集合中,并返回位置;如果存在,则返回对应的位置。    
///     
/// ImageList集合    
/// 扩展名    
/// 是否大图标    
///     
public static int GetIcon(ImageList images, string extension, bool largeIcon)    

///     
/// 获取扩展名的图标    
///     
/// 扩展名    
/// 是否为大图标    
///     
public static Icon ExtractIconForExtension(string extension, bool large)    

///     
/// 获取指定文件的关联图标    
///     
/// 指定的文件路径    
/// 是否为大图标    
///     
public static Icon GetAssociatedIcon(string stubPath, bool large)   

#endregion

2)辅助类IconReaderHelper的使用例子代码如下所示

private void btnGetIcon_Click(object sender, EventArgs e)    
{    
    if (!string.IsNullOrEmpty(this.txtFilPath.Text))    
    {    
        //获取文件的图标    
        Icon ico = IconReaderHelper.GetFileIcon(this.txtFilPath.Text,     
            IconReaderHelper.IconSize.Large, false);    
        this.pictureBox1.Image = Bitmap.FromHicon(ico.Handle);    

        //获取对应图标的说明名称    
        string name = IconReaderHelper.GetDisplayName(this.txtFilPath.Text, false);    
        this.Text = name;    
    }    

    //获取系统文件夹图标    
    Icon ico2 = IconReaderHelper.GetFolderIcon(IconReaderHelper.IconSize.Large,     
        IconReaderHelper.FolderType.Open);    
    this.pictureBox2.Image = Bitmap.FromHicon(ico2.Handle);    

}    

private void btnGetExtensionIcon_Click(object sender, EventArgs e)    
{    
    if (!string.IsNullOrEmpty(this.txtExtension.Text))    
    {    
        //获取扩展名图标    
        Icon ico = IconReaderHelper.ExtractIconForExtension(this.txtExtension.Text, true);    
        this.pictureBox2.Image = Bitmap.FromHicon(ico.Handle);    
    }    
}

2、实现图片打印操作,并提供图片打印预览功能,图片自动根据页面进行居中、缩放、对齐等操作辅助类

ImagePrintHelper

实现效果 1)本辅助类主要是用来方便实现图片打印操作,并提供图片打印预览功能,图片自动根据页面进行居中、缩放、对齐等默认操作。
打印预览的时候,自动调整图片为止,如下:

或者打印对话框

实现代码
1)辅助类提供的方法接口如下所示

#region 配置参数    
///     
/// 将在页面上居中打印输出。    
///     
public bool AllowPrintCenter = true;    

///     
/// 旋转图像,如果它符合页面更好    
///     
public bool AllowPrintRotate = true;    

///     
/// 缩放图像,以更好地适应页面    
///     
public bool AllowPrintEnlarge = true;    

///     
/// 允许打印收缩,以更好适应页面    
///     
public bool AllowPrintShrink = true;   

#endregion    

///     
/// 构造函数    
///     
/// 待打印的图片对象    
public ImagePrintHelper(Image image) : this(image, \"test.png\")    

///     
/// 构造函数    
///     
/// 待打印的图片对象    
/// 文档名称    
public ImagePrintHelper(Image image, string documentname)     

///     
/// 显示打印对话框,确定则进行打印    
///     
///     
public PrinterSettings PrintWithDialog()    

///     
/// 在预览对话框中预览图片    
///     
public void PrintPreview()

2)辅助类ImagePrintHelper的使用例子代码如下所示

private void btnPrintImage_Click(object sender, EventArgs e)    
{    
    ImagePrintHelper imagePrint = new ImagePrintHelper(this.pictureBox1.Image, Path.GetFileName(this.txtFilPath.Text));    
    imagePrint.AllowPrintShrink = true;    
    imagePrint.AllowPrintCenter = true;    
    imagePrint.AllowPrintEnlarge = true;    
    imagePrint.AllowPrintRotate = true;    
    imagePrint.PrintWithDialog();//弹出打印对话框,确认进行打印    
}    

private void btnPrintView_Click(object sender, EventArgs e)    
{    
    ImagePrintHelper imagePrint = new ImagePrintHelper(this.pictureBox1.Image, Path.GetFileName(this.txtFilPath.Text));    
    imagePrint.PrintPreview();//弹出打印预览页面    
}

2、Web页面预览效果图片抓取辅助类 WebPageCapture和WebPreview

实现效果 1)本辅助类主要是用来方便实现Web页面预览效果图片抓取操作。

实现代码
1)辅助类提供的方法接口如下所示

#region 字段属性    

///     
/// 图片下载完毕的时间处理    
///     
public event ImageEventHandler DownloadCompleted;    
///     
/// 图片处理委托定义    
///     
///     
public delegate void ImageEventHandler(Image image);    

///     
/// 浏览区域大小    
///     
public Size BrowserSize { get; set; }    

///     
/// 页面URL地址    
///     
public string URL { get; set; }    

///     
/// 下载的图片对象    
///     
public Image Image { get; private set; }   

#endregion   

#region 构造函数    

///     
/// 构造函数,默认为屏幕大小    
///     
public WebPageCapture() : this(Screen.PrimaryScreen.Bounds.Size) { }    

///     
/// 构造函数,指定浏览区域大小    
///     
/// 宽度    
/// 高度    
public WebPageCapture(int width, int height) : this(new Size(width, height)) { }    

///     
/// 构造函数,指定浏览区域大小    
///     
/// 浏览区域大小    
public WebPageCapture(Size browserSize)   

#endregion    

///     
/// 下载页面到图片中    
///     
public void DownloadPage()    

///     
/// 下载页面到图片中    
///     
/// 页面Url地址    
public void DownloadPage(string url)

2)辅助类WebPageCapture的使用例子代码如下所示

public partial class FrmWebPreview : Form    
{    
    private WebPageCapture capture = new WebPageCapture();    
    public FrmWebPreview()    
    {    
        InitializeComponent();    

        capture.DownloadCompleted += new WebPageCapture.ImageEventHandler(capture_DownloadCompleted);       }    

    void capture_DownloadCompleted(Image image)    
    {    
        this.pictureBox1.Image = image;    
    }    

    private void btnSnap1_Click(object sender, EventArgs e)    
    {    
        if (this.txtUrl.Text.Length == 0) return;    

        capture.DownloadPage(this.txtUrl.Text);                
    }

3) 辅助类WebPreview的使用例子代码如下所示

private void btnSnap2_Click(object sender, EventArgs e)    
{    
    if (this.txtUrl.Text.Length == 0) return;    
    this.pictureBox1.Image = WebPreview.GetWebPreview(new Uri(this.txtUrl.Text), Screen.PrimaryScreen.Bounds.Size);    
}

2、图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类 ImageHelper

实现效果 1)本辅助类主要是用来方便实现图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作。
2)基本图片所有的相关操作,均可在该辅助类中找到,该辅助类具有专业、全面、封装良好等特点。

实现代码
1)辅助类提供的方法接口如下所示

 ///     
/// 图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类    
///     
public class ImageHelper    
{   
    #region 图片比较    

    ///     
    /// 对比两张图片    
    ///     
    /// The first bitmap image    
    /// The second bitmap image    
    /// CompareResult    
    public static CompareResult CompareTwoImages(Bitmap bmp1, Bitmap bmp2)   

    #endregion   

    #region 图片缩放    

    ///     
    /// 按比例调整图片大小    
    ///     
    /// 原始图片对象.    
    /// 调整比例    
    ///     
    public static Image ResizeImageByPercent(Image imgPhoto, int Percent)    

    ///     
    /// 缩放、裁切图片    
    ///     
    /// 原始图片对象    
    /// 宽度    
    /// 高度    
    /// 调整模式    
    ///     
    public static Image ResizeImageToAFixedSize(Image originalImage, int width, int height, ScaleMode mode)   

    #endregion   

    #region 创建缩略图    

    ///     
    /// 生成缩略图    
    ///     
    /// 源图路径(物理路径)    
    /// 缩略图路径(物理路径)    
    /// 缩略图宽度    
    /// 缩略图高度    
    /// 生成缩略图的方式    
    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ScaleMode mode)    

    ///     
    /// 生成缩略图    
    ///     
    /// 源图路径(物理路径)    
    /// 缩略图路径(物理路径)    
    /// 缩略图宽度    
    /// 缩略图高度    
    /// 生成缩略图的方式    
    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ScaleMode mode, System.Drawing.Imaging.ImageFormat format)   

    #endregion   

    #region 图片水印    

    ///     
    /// 添加文字水印   默认字体:Verdana  12号大小  斜体  黑色    位置:右下角    
    ///     
    /// Image    
    /// 水印字    
    ///     
    public static Image WatermarkText(Image originalImage, string text)    

    ///     
    /// 添加文字水印   默认字体:Verdana  12号大小  斜体  黑色    
    ///     
    /// Image    
    /// 水印字    
    /// 水印位置    
    ///     
    public static Image WatermarkText(Image originalImage, string text, WatermarkPosition position)    

    ///     
    /// 添加文字水印    
    ///     
    /// Image    
    /// 水印字    
    /// 水印位置    
    /// 字体    
    /// 颜色    
    ///     
    public static Image WatermarkText(Image originalImage, string text, WatermarkPosition position, Font font, Brush brush)    

    ///     
    /// 添加图片水印(位置默认右下角)    
    ///     
    /// Image    
    /// 水印Image    
    ///     
    public static Image WatermarkImage(Image originalImage, Image watermarkImage)    

    ///     
    /// 添加图片水印    
    ///     
    /// Image    
    /// 水印Image    
    /// 位置    
    ///     
    public static Image WatermarkImage(Image originalImage, Image watermarkImage, WatermarkPosition position)   

    #endregion   

    #region 复制到剪切板    

    ///     
    /// 复制指定的图片对象到剪切板    
    ///     
    /// 复制的图片对象    
    public static void CopyToClipboard(Image img)   

    #endregion   

    #region 其他操作    

    ///     
    /// 获取图片编码解码器,保存JPG时用    
    ///     
    ///      
    /// 得到指定mimeType的ImageCodecInfo     
    public static ImageCodecInfo GetCodecInfo(string mimeType)    


    ///     
    /// 设置图片透明度    
    ///     
    /// 原始图片对象    
    /// 透明度    
    ///     
    public static Image SetImgOpacity(Image imgPic, float imgOpac)    

    ///     
    /// 修改图片亮度    
    ///     
    /// 原始位图对象    
    /// 亮度值    
    ///     
    public static Bitmap ChangeBrightness(Bitmap bmp, int value)   

    #region 改变图片大小    

    ///     
    /// 改变图片大小    
    ///     
    /// 原始图片对象    
    /// 改变的宽度    
    /// 改变的高度    
    ///     
    public static Image ChangeImageSize(Image img, int width, int height)    

    ///     
    /// 改变图片大小    
    ///     
    /// 原始图片对象    
    /// 改变的宽度    
    /// 改变的高度    
    /// 限制的最小宽度或高度    
    ///     
    public static Image ChangeImageSize(Image img, int width, int height, bool preserveSize)    

    ///     
    /// 改变图片大小(按比例缩放)    
    ///     
    /// 原始图片对象    
    /// 大小比例    
    ///     
    public static Image ChangeImageSize(Image img, float percentage)   

    #endregion    

    ///     
    /// 剪切图片    
    ///     
    /// 原始图片对象    
    /// 剪切区域    
    ///     
    public static Image CropImage(Image img, Rectangle rect)    

    ///     
    /// 剪裁 -- 用GDI+    
    ///     
    /// 原始Bitmap    
    /// 开始坐标X    
    /// 开始坐标Y    
    /// 宽度    
    /// 高度    
    /// 剪裁后的Bitmap    
    public static Bitmap CropImage(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)    

    public static Image AddCanvas(Image img, int size)    

    ///     
    /// 旋转图片    
    ///     
    /// 待旋转的图片对象    
    /// 旋转角度    
    ///     
    public static Bitmap RotateImage(Image img, float theta)    

    private static Rectangle BoundingBox(Image img, Matrix matrix)    

    ///     
    /// 获取所有屏幕的矩形宽度    
    ///     
    ///     
    public static Rectangle GetScreenBounds()    

    ///     
    /// 使窗口背景透明    
    ///     
    /// 窗口句柄    
    /// 透明图片    
    ///     
    public static Bitmap MakeBackgroundTransparent(IntPtr hWnd, Image image)    

    ///     
    /// 保存打印图片文件    
    ///     
    /// 图片对象    
    /// 透明色    
    /// 打印的dpi    
    /// 保存文件的路径    
    public static void SaveOneInchPic(Image image, Color transColor, float dpi, string path)   

    #endregion   

    #region BASE64编码转换    

    ///     
    /// Base64编码转换到Image对象    
    ///     
    ///     
    ///     
    public static Bitmap Base64StrToBmp(string ImgBase64Str)    

    ///     
    /// 从文件中转换图片对象到Base64编码    
    ///     
    ///     
    ///     
    public static string ImageToBase64Str(string ImgName)    

    ///     
    /// 转换图片对象到Base64编码    
    ///     
    ///     
    ///     
    public static string ImageToBase64Str(Image Img)   

    #endregion   

    #region 色彩处理    

    ///       
    /// 设置图形颜色  边缘的色彩更换成新的颜色      
    ///       
    /// 图片      
    /// 老的边缘色彩      
    /// 新的边缘色彩      
    /// 溶差      
    /// 清理后的图形      
    public static Image SetImageColorBrim(Image p_Image, Color p_OldColor, Color p_NewColor, int p_Float)    

    ///       
    /// 设置图形颜色  所有的色彩更换成新的颜色      
    ///       
    /// 图片      
    /// 老的颜色      
    /// 新的颜色      
    /// 溶差      
    /// 清理后的图形      
    public static Image SetImageColorAll(Image p_Image, Color p_OdlColor, Color p_NewColor, int p_Float)    

    ///       
    /// 设置图形颜色  坐标的颜色更换成新的色彩 (漏斗)      
    ///       
    /// 新图形      
    /// 位置      
    /// 新的色彩      
    /// 溶差      
    /// 清理后的图形      
    public static Image SetImageColorPoint(Image p_Image, Point p_Point, Color p_NewColor, int p_Float)    

    ///       
    /// SetImageColorPoint 循环调用 检查新的坐标是否符合条件 符合条件会写入栈p_ColorStack 并更改颜色      
    ///       
    /// 数据区      
    /// 行扫描字节数      
    /// 需要检查的位置栈      
    /// 位置X      
    /// 位置Y      
    /// 老色彩      
    /// 新色彩      
    /// 溶差      
    private static void SetImageColorPoint(byte[] p_DataBytes, int p_Stride, Stack p_ColorStack, int p_X, int p_Y, Color p_OldColor, Color p_NewColor, int p_Float)    

    ///       
    /// 检查色彩(可以根据这个更改比较方式      
    ///       
    /// 当前色彩      
    /// 比较色彩      
    /// 溶差      
    ///       
    private static bool ScanColor(Color p_CurrentlyColor, Color p_CompareColor, int p_Float)   

    #endregion
#region 图像压缩    
///     
/// 将Bitmap对象压缩为JPG图片类型    
///     
///     
/// 源bitmap对象    
/// 目标图片的存储地址    
/// 压缩质量,越大照片越清晰,推荐80    
public static void CompressAsJPG(Bitmap bmp, string saveFilePath, int quality)    

///     
/// 将inputStream中的对象压缩为JPG图片类型    
///     
/// 源Stream对象    
/// 目标图片的存储地址    
/// 压缩质量,越大照片越清晰,推荐80    
public static void CompressAsJPG(Stream inputStream, string saveFilePath, int quality)   

#endregion   

#region 图片转换    

///     
/// 把Image对象转换为Byte数组    
///     
/// 图片Image对象    
/// 字节集合    
public static byte[] ImageToBytes(System.Drawing.Image image)    

///     
/// 转换Byte数组到Image对象    
///     
/// 字节数组    
/// Image图片    
public static System.Drawing.Image ImageFromBytes(byte[] bytes)    

///     
/// 转换地址(文件路径或者URL地址)到Image对象    
///     
/// 图片地址(文件路径或者URL地址)    
/// Image对象    
public static System.Drawing.Image ImageFromUrl(string url)   

#endregion

2) 辅助类ImageHelper的使用例子代码如下所示

private void btnLoadBitmap_Click(object sender, EventArgs e)    
{    
    //加载图片    
    string file = FileDialogHelper.OpenImage();    
    if (!string.IsNullOrEmpty(file))    
    {    
        pictureBox1.Image = Image.FromFile(file);    
    }    
}    

private void btnWaterMark_Click(object sender, EventArgs e)    
{    
    //图片水印效果    
    string file = FileDialogHelper.OpenImage();    
    if (!string.IsNullOrEmpty(file))    
    {    
        this.pictureBox2.Image = ImageHelper.WatermarkImage(this.pictureBox1.Image, Image.FromFile(file));    
    }    
}    

private void btnTextWatermark_Click(object sender, EventArgs e)    
{    
    //文字水印效果    
    this.pictureBox2.Image = ImageHelper.WatermarkText(this.pictureBox1.Image, \"测试水印效果\");    
}    

private void btnZoom_Click(object sender, EventArgs e)    
{    
    //按比例放大图片    
    this.pictureBox2.Image = ImageHelper.ResizeImageByPercent(this.pictureBox1.Image, 200);    
}    

private void btnRotate_Click(object sender, EventArgs e)    
{    
    //旋转图片    
    this.pictureBox2.Image = ImageHelper.RotateImage(this.pictureBox1.Image, 90);    
}

2、弹出打印窗体的预览对话框辅助类 PrintFormHelper

实现效果 1)本辅助类主要是用来方便实现窗体界面打印的功能。
2)窗体界面打印提供通用的打印预览功能。

实现代码
1)辅助类提供的方法接口如下所示

///     
/// Winform窗口打印辅助类    
///     
public class PrintFormHelper    
{    
       ///     
       /// 弹出打印窗体的预览对话框    
       ///     
       ///     
       public static void Print(Form form)    
}

2) 辅助类PrintFormHelper的使用例子代码如下所示

private void btnPrintForm_Click(object sender, EventArgs e)    
{    
    try   
    {    
        this.Cursor = Cursors.WaitCursor;    
        PrintFormHelper.Print(this);//打印当前窗体    
    }    
    catch (Exception ex)    
    {    
        MessageBox.Show(this, ex.Message);    
    }    
    finally   
    {    
        this.Cursor = Cursors.Default;    
    }    
}

2、抓取整个屏幕或指定窗口,并可把抓取的图片保存到文件等操作辅助类 ScreenCapture实现效果
1)本辅助类主要是用来方便实现抓取整个屏幕或指定窗口,并可把抓取的图片保存到文件等操作 2)该辅助类类非常方便实现对桌面、指定窗口进行屏幕截图,并可以指定保存路径、指定图片格式、后缀名等属性对截图进行保存,适合用于对屏幕监控等相关的操作中。

实现代码
1)辅助类提供的方法接口如下所示

///     
/// 图片保存的路径,默认为C:\\\\CaptureImages    
///     
public string ImageSavePath    

///     
/// 图片的后缀名,如果为空,使用图片格式作为后缀名    
///     
public string ImageExtension    

///     
/// 图片保存的格式    
///     
public ImageFormat ImageFormat    

///     
/// 抓取桌面整个屏幕截图到一个图片对象中    
///     
///     
public Image CaptureScreen()    

///     
/// 抓取桌面整个指定窗口的截图到一个图片对象中    
///     
/// 指定窗口的句柄    
///     
public Image CaptureWindow(IntPtr handle)    

///     
/// 抓取桌面整个指定窗口的截图,并保存到文件中    
///     
///     
///     
///     
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)    

///     
/// 根据属性配置自动保存图片    
///     
public void AutoCaptureScreen()    

///     
/// 抓取桌面整个指定窗口的截图,并保存到文件中    
///     
///     
///     
public void CaptureScreenToFile(string filename, ImageFormat format)

2)辅助类ScreenCapture的使用例子代码如下所示

ScreenCapture capture = new ScreenCapture();  
Image image = capture.CaptureWindow(form.Handle);