在一个button的click事件里面,把winform的某一部分保存到word。例如把winform的(100,100)(100,200)(200,200)(200,100)这四个点所围成的矩形保存到word。请问要如何做。

解决方案 »

  1.   

    类似QQ截图using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;namespace CaptureScreen
    {
     /// <summary>
     /// CaptureScreenForm 的摘要说明。
     /// </summary>
     public class CaptureScreenForm : System.Windows.Forms.Form
     {
      private System.Windows.Forms.PictureBox picbox;
      private System.Windows.Forms.Button exitBtn;
      private System.Windows.Forms.Button aboutBtn;
      private System.Windows.Forms.Button ClearBtn;
      private System.Windows.Forms.Button saveBtn;
      private System.Windows.Forms.Button fullScreenBtn;
      private System.Windows.Forms.Button mouseCaptureBtn;  public static Image image = null ;
      private System.Windows.Forms.Panel panel1;  /// <summary>
      /// 必需的设计器变量。
      /// </summary>
      private System.ComponentModel.Container components = null;  public CaptureScreenForm()
      {
       //
       // Windows 窗体设计器支持所必需的
       //
       InitializeComponent();   //
       // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
       //
      }  /// <summary>
      /// 清理所有正在使用的资源。
      /// </summary>
      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }  #region Windows 窗体设计器生成的代码
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {
       this.picbox = new System.Windows.Forms.PictureBox();
       this.fullScreenBtn = new System.Windows.Forms.Button();
       this.mouseCaptureBtn = new System.Windows.Forms.Button();
       this.ClearBtn = new System.Windows.Forms.Button();
       this.saveBtn = new System.Windows.Forms.Button();
       this.aboutBtn = new System.Windows.Forms.Button();
       this.exitBtn = new System.Windows.Forms.Button();
       this.panel1 = new System.Windows.Forms.Panel();
       this.panel1.SuspendLayout();
       this.SuspendLayout();
       //
       // picbox
       //
       this.picbox.Location = new System.Drawing.Point(0, 40);
       this.picbox.Name = "picbox";
       this.picbox.Size = new System.Drawing.Size(680, 312);
       this.picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
       this.picbox.TabIndex = 0;
       this.picbox.TabStop = false;
       //
       // fullScreenBtn
       //
       this.fullScreenBtn.Location = new System.Drawing.Point(0, 0);
       this.fullScreenBtn.Name = "fullScreenBtn";
       this.fullScreenBtn.Size = new System.Drawing.Size(96, 32);
       this.fullScreenBtn.TabIndex = 1;
       this.fullScreenBtn.Text = "全屏截取";
       this.fullScreenBtn.Click += new System.EventHandler(this.fullScreenBtn_Click);
       //
       // mouseCaptureBtn
       //
       this.mouseCaptureBtn.Location = new System.Drawing.Point(120, 0);
       this.mouseCaptureBtn.Name = "mouseCaptureBtn";
       this.mouseCaptureBtn.Size = new System.Drawing.Size(96, 32);
       this.mouseCaptureBtn.TabIndex = 2;
       this.mouseCaptureBtn.Text = "鼠标截取";
       this.mouseCaptureBtn.Click += new System.EventHandler(this.mouseCaptureBtn_Click);
       //
       // ClearBtn
       //
       this.ClearBtn.Location = new System.Drawing.Point(240, 0);
       this.ClearBtn.Name = "ClearBtn";
       this.ClearBtn.Size = new System.Drawing.Size(88, 32);
       this.ClearBtn.TabIndex = 3;
       this.ClearBtn.Text = "清除截取图片";
       this.ClearBtn.Click += new System.EventHandler(this.ClearBtn_Click);
       //
       // saveBtn
       //
       this.saveBtn.Location = new System.Drawing.Point(352, 0);
       this.saveBtn.Name = "saveBtn";
       this.saveBtn.Size = new System.Drawing.Size(96, 32);
       this.saveBtn.TabIndex = 4;
       this.saveBtn.Text = "保存截取图片";
       this.saveBtn.Click += new System.EventHandler(this.saveBtn_Click);
       //
       // aboutBtn
       //
       this.aboutBtn.Location = new System.Drawing.Point(472, 0);
       this.aboutBtn.Name = "aboutBtn";
       this.aboutBtn.Size = new System.Drawing.Size(96, 32);
       this.aboutBtn.TabIndex = 5;
       this.aboutBtn.Text = "关  于";
       this.aboutBtn.Click += new System.EventHandler(this.aboutBtn_Click);
       //
       // exitBtn
       //
       this.exitBtn.Location = new System.Drawing.Point(592, 0);
       this.exitBtn.Name = "exitBtn";
       this.exitBtn.Size = new System.Drawing.Size(88, 32);
       this.exitBtn.TabIndex = 6;
       this.exitBtn.Text = "退  出";
       this.exitBtn.Click += new System.EventHandler(this.exitBtn_Click);
       //
       // panel1
       //
       this.panel1.Controls.Add(this.fullScreenBtn);
       this.panel1.Controls.Add(this.mouseCaptureBtn);
       this.panel1.Controls.Add(this.ClearBtn);
       this.panel1.Controls.Add(this.saveBtn);
       this.panel1.Controls.Add(this.aboutBtn);
       this.panel1.Controls.Add(this.exitBtn);
       this.panel1.Location = new System.Drawing.Point(0, 0);
       this.panel1.Name = "panel1";
       this.panel1.Size = new System.Drawing.Size(680, 32);
       this.panel1.TabIndex = 7;
       //
       // CaptureScreenForm
       //
       this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
       this.AutoScroll = true;
       this.ClientSize = new System.Drawing.Size(696, 358);
       this.Controls.Add(this.panel1);
       this.Controls.Add(this.picbox);
       this.Name = "CaptureScreenForm";
       this.Text = "截取屏幕";
       this.panel1.ResumeLayout(false);
       this.ResumeLayout(false);  }
      #endregion  /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main()
      {
       Application.Run(new CaptureScreenForm());
      }  protected override void OnPaint(PaintEventArgs e)
      {
       base.OnPaint (e);
       if(image != null)
       {
        this.picbox.Image = image;
       }
       else
       {
        this.picbox.Image = null;
        this.picbox.Refresh();
       }
      }
      // 引用API  [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
      public static extern bool BitBlt(
       IntPtr hdcDest, //目标设备的句柄
       int nXDest, // 目标对象的左上角的X坐标
       int nYDest, // 目标对象的左上角的X坐标
       int nWidth, // 目标对象的矩形的宽度
       int nHeight, // 目标对象的矩形的长度
       IntPtr hdcSrc, // 源设备的句柄
       int nXSrc, // 源对象的左上角的X坐标
       int nYSrc, // 源对象的左上角的X坐标
       System.Int32 dwRop // 光栅的操作值
       );
      private void exitBtn_Click(object sender, System.EventArgs e)
      {
       Application.Exit();
      }  private void aboutBtn_Click(object sender, System.EventArgs e)
      {
       MessageBox.Show("Code by Star in 2006-3-17,E_mail: [email protected] ,thank you to use this.");
      }  private void ClearBtn_Click(object sender, System.EventArgs e)
      {
       CaptureScreenForm.image = null;
       this.Invalidate();
      }  private void saveBtn_Click(object sender, System.EventArgs e)
      {
       bool isSave = true;
       SaveFileDialog saveImageDialog = new SaveFileDialog();
       saveImageDialog.Title = "Capture screen image save dialog";
       saveImageDialog.Filter= @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";   if(saveImageDialog.ShowDialog() == DialogResult.OK)
       {
         string fileName = saveImageDialog.FileName.ToString();
        
        if(fileName != "" && fileName != null)
        {
         string fileExtName = fileName.Substring(fileName.LastIndexOf(".")+1).ToString();     System.Drawing.Imaging.ImageFormat imgformat = null;    
     
         if(fileExtName!="")
         {
          switch(fileExtName)
          {
           case "jpg":
            imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
            break;
           case "bmp":
            imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
            break;
           case "gif":
            imgformat = System.Drawing.Imaging.ImageFormat.Gif;
            break;
           default:
            MessageBox.Show("只能存取为: jpg,bmp,gif 格式");
            isSave = false;
            break;
          }     }     //默认保存为JPG格式
         if(imgformat == null)
         {
          imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
         }
                       
         if(isSave)
         {
          try
          {
           CaptureScreenForm.image.Save(fileName,imgformat);
           MessageBox.Show("图片已经成功保存!~~");
          }
          catch
          {
           MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");
          }
         }    }   }
      }  private void fullScreenBtn_Click(object sender, System.EventArgs e)
      {
          CaptureScreenForm.image = windowFullScreen();
       picbox.Image = CaptureScreenForm.image;
      }  public static Bitmap windowFullScreen()
      {
       //建立屏幕Graphics
       Graphics grpScreen = Graphics.FromHwnd(IntPtr.Zero);
       //根据屏幕大小建立位图
       Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,grpScreen);
       //建立位图相关Graphics
       Graphics grpBitmap = Graphics.FromImage(bitmap);
                //建立屏幕上下文
       IntPtr hdcScreen = grpScreen.GetHdc();
       //建立位图上下文
       IntPtr hdcBitmap = grpBitmap.GetHdc();
       //将屏幕捕获保存在图位中
       BitBlt(hdcBitmap,0,0,bitmap.Width,bitmap.Height,hdcScreen,0,0,0x00CC0020);
       //关闭位图句柄
       grpBitmap.ReleaseHdc(hdcBitmap);
       //关闭屏幕句柄
       grpScreen.ReleaseHdc(hdcScreen);
       //释放位图对像
       grpBitmap.Dispose();
       //释放屏幕对像
       grpScreen.Dispose();   //返回捕获位图
       return bitmap;
      }  private void mouseCaptureBtn_Click(object sender, System.EventArgs e)
      {
       MouseCaptureForm mouseCapture = new MouseCaptureForm();
       mouseCapture.Show();
      }
     }
    }
      

  2.   

    1、先实现截图:
    源码和实现原理
    Screen Shot Application
    效果图:
    2、实现保存到word,google一下,很多,可以使用vsto做。
      

  3.   

    我是想一键截图,就是把winform里面特点的4个点围成的矩形给保存为一个图片,不知道要怎么做
      

  4.   

      Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,grpScreen);   BitBlt(hdcBitmap,0,0,bitmap.Width,bitmap.Height,hdcScreen,0,0,0x00CC0020);这城宽和高就是你给的坐标。
    保存到wordusing Word;
    下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)        public string CreateWordFile(string CheckedInfo)
            ...{
                string message = "";
                try
                ...{
                    Object Nothing = System.Reflection.Missing.Value;
                    Directory.CreateDirectory("C:/CNSI");  //创建文件所在目录
                    string name = "CNSI_" + DateTime.Now.ToShortString()+".doc";
                    object filename = "C://CNSI//" + name;  //文件保存路径
                    //创建Word文档
                    Word.Application WordApp = new Word.ApplicationClass();
                    Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);                //添加页眉
                    WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                    WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                    WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
                    WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                    WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距                //移动焦点并换行
                    object count = 14;
                    object WdLine = Word.WdUnits.wdLine;//换一行;
                     WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                     WordApp.Selection.TypeParagraph();//插入段落                 //文档中创建表格
                     Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
                     //设置表格样式
                     newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                     newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                     newTable.Columns[1].Width = 100f;
                     newTable.Columns[2].Width = 220f;
                     newTable.Columns[3].Width = 105f;                 //填充表格内容
                     newTable.Cell(1, 1).Range.Text = "产品详细信息表";
                     newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                     //合并单元格
                     newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
                     WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                     WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
                            
                     //填充表格内容
                     newTable.Cell(2, 1).Range.Text = "产品基本信息";
                     newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
                     //合并单元格
                     newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
                     WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;                  //填充表格内容
                      newTable.Cell(3, 1).Range.Text = "品牌名称:";
                      newTable.Cell(3, 2).Range.Text = BrandName;
                      //纵向合并单元格
                      newTable.Cell(3, 3).Select();//选中一行
                      object moveUnit = Word.WdUnits.wdLine;
                      object moveCount = 5;
                      object moveExtend = Word.WdMovementType.wdExtend;
                       WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                       WordApp.Selection.Cells.Merge();
                       //插入图片
                       string FileName = Picture;//图片所在路径
                       object LinkToFile = false;
                       object SaveWithDocument = true;
                       object Anchor = WordDoc.Application.Selection.Range;
                       WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                        WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
                        WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
                        //将图片设置为四周环绕型
                        Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                        s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;
                            
                        newTable.Cell(12, 1).Range.Text = "产品特殊属性";
                        newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
                         //在表格中增加行
                         WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
                          
                         WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
                         WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;                    //文件保存
                        WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                        WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                        WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                        message=name+"文档生成成功,以保存到C:\CNSI\下";
                }
                catch
                ...{
                    message = "文件导出异常!";
                }
                return message;
            }
      

  5.   

    鼠标选择范围,记录位置
    http://topic.csdn.net/u/20090304/14/833e7d98-07bf-4b72-a344-ef91bfa6812a.html
      

  6.   


    c#winform截图
    http://blog.csdn.net/xiaolvzi/archive/2007/08/28/1762938.aspx