国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#模仿QQ截圖功能

2024-07-21 02:26:39
字體:
來源:轉載
供稿:網友

前陣子改了段c#截圖功能的代碼,現貼上來希望對大家有用

主文件

capturescreenform.cs

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();
  }
 }
}

另一個截屏文件:

mousecaptureform.cs

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;

namespace capturescreen
{
 /// <summary>
 /// mousecaptureform 的摘要說明。
 /// </summary>
 public class mousecaptureform : system.windows.forms.form
 {
  /// <summary>
  /// 必需的設計器變量。
  /// </summary>
  //
  //自定義變量
  //
  private point pot;
  private rectangle area = rectangle.empty;
  private image img;
  private int index = -1;
  private string noselectstring   =  "/n.請按下鼠標左鍵不放拖動選取截圖區域/n/n .單擊鼠標右鍵或按esc取消截圖";
  private string selectingstring  =  "/n.松開鼠標左鍵確定選取范圍/n/n.按esc重新選擇";
  private string selectedstring   =  "/n.按鼠標左鍵調整選擇范圍/n/n .雙擊鼠標左鍵保存截圖 /n/n.按鼠標右鍵重新選擇 /n/n.esc鍵取消截圖";
  private system.windows.forms.label tiptext;
       
  //
  //系統生成定義
  //
  private system.componentmodel.container components = null;

  public mousecaptureform()
  {
   this.bounds = system.windows.forms.screen.primaryscreen.bounds;
   this.backgroundimage = capturescreenform.windowfullscreen(); 

   //
   // 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.tiptext = new system.windows.forms.label();
   this.suspendlayout();
   //
   // tiptext
   //
   this.tiptext.backcolor = system.drawing.color.fromargb(((system.byte)(192)), ((system.byte)(192)), ((system.byte)(255)));
   this.tiptext.forecolor = system.drawing.color.white;
   this.tiptext.location = new system.drawing.point(10, 10);
   this.tiptext.name = "tiptext";
   this.tiptext.size = new system.drawing.size(166, 140);
   this.tiptext.tabindex = 0;
   this.tiptext.mousemove += new system.windows.forms.mouseeventhandler(this.tiptext_mousemove);
   //
   // mousecaptureform
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(456, 320);
   this.controls.add(this.tiptext);
   this.formborderstyle = system.windows.forms.formborderstyle.none;
   this.name = "mousecaptureform";
   this.showintaskbar = false;
   this.startposition = system.windows.forms.formstartposition.windowsdefaultbounds;
   this.text = "鼠標截屏";
   this.topmost = true;
   this.windowstate = system.windows.forms.formwindowstate.maximized;
   this.keydown += new system.windows.forms.keyeventhandler(this.mousecaptureform_keydown);
   this.load += new system.eventhandler(this.mousecaptureform_load);
   this.doubleclick += new system.eventhandler(this.mousecaptureform_doubleclick);
   this.resumelayout(false);

  }


  #endregion

  /// <summary>
  /// 重載mousedown
  /// </summary>
  ///
  protected override void onmousedown(mouseeventargs e)
  {    
   base.onmousedown (e);
    {
   if(this.area == rectangle.empty && e.button == mousebuttons.left)
   {
    this.tiptext.text = selectingstring;
    this.area.location = new point(e.x,e.y);
   }
   this.pot = new point(e.x,e.y);
   this.index = this.getselectedhandle(new point(e.x,e.y));
   this.setcursor();    
    }
  }

  /// <summary>
  /// 設置鼠標方案
  /// </summary>
  private void setcursor()
  {
   cursor cr = cursors.default;
 
   if(index == 1 || index == 5)
   {
    cr = cursors.sizenwse;
   }
   else if(index == 2 || index == 6)
   {
    cr = cursors.sizens;
   }
   else if(index == 3 || index == 7)
   {
    cr = cursors.sizenesw;
   }
   else if(index == 4 || index == 8)
   {
    cr = cursors.sizewe;
   }
   else if(index == 0)
   {
    cr = cursors.sizeall;
   }
   cursor.current = cr;
  }

  protected override void onpaint(painteventargs e)
  {
   base.onpaint(e);
   e.graphics.drawrectangle(new pen(this.forecolor),this.area);
 
   for(int i = 1;i < 9;i++)
   {
    e.graphics.fillrectangle(new solidbrush(color.green),this.gethandlerect(i));
   }
  }

  private rectangle gethandlerect(int index)
  {
   point point = gethandle(index);
   return new rectangle(point.x - 3, point.y - 3, 6, 6);
  }

  private point gethandle(int index)
  {
   int x, y, xcenter, ycenter;

   xcenter = area.x + area.width/2;
   ycenter = area.y + area.height/2;
   x = area.x;
   y = area.y;

   switch ( index )
   {
    case 1:
     x = area.x;
     y = area.y;
     break;
    case 2:
     x = xcenter;
     y = area.y;
     break;
    case 3:
     x = area.right;
     y = area.y;
     break;
    case 4:
     x = area.right;
     y = ycenter;
     break;
    case 5:
     x = area.right;
     y = area.bottom;
     break;
    case 6:
     x = xcenter;
     y = area.bottom;
     break;
    case 7:
     x = area.x;
     y = area.bottom;
     break;
    case 8:
     x = area.x;
     y = ycenter;
     break;
   }

   return new point(x, y);
  }

  protected override void onmouseup(mouseeventargs e)
  {
   base.onmouseup (e);
   int left = area.left;
   int top = area.top;
   int right = area.right;
   int bottom = area.bottom;
   area.x = math.min(left,right);
   area.y = math.min(top,bottom);
   area.width = math.abs(left-right);
   area.height = math.abs(top-bottom);
   this.tiptext.text = selectedstring;
   if(e.button == mousebuttons.right)
   {
    if(this.area == rectangle.empty)
    {
     this.close();
    }
    else
    {
     this.area=rectangle.empty;
     this.invalidate();
    }
   }
   this.index = this.getselectedhandle(new point(e.x,e.y));
   this.setcursor();
  }

  private int getselectedhandle(point p)
  {
   int index = -1;
   for(int i = 1;i < 9;i++)
   {
    if(gethandlerect(i).contains(p))
    {
     index=i;
     break;
    }
   }
   if(this.area.contains(p))index = 0;

   return index;
  }

  protected override void onmousemove(mouseeventargs e)
  {
   base.onmousemove (e);
   if(this.capture)
   {
    this.movehandleto(new point(e.x,e.y));
    this.invalidate();
   }
   else
   {
    this.index = this.getselectedhandle(new point(e.x,e.y));
    this.setcursor();
   }
  }

  private void movehandleto(point point)
  {
   int left = area.left;
   int top = area.top;
   int right = area.right;
   int bottom = area.bottom;

   switch ( index )
   {
    case 0:
     area.x +=point.x - this.pot.x;
     area.y += point.y - pot.y;
     this.pot = point;
     return;
    case 1:
     left = point.x;
     top = point.y;
     break;
    case 2:
     top = point.y;
     break;
    case 3:
     right = point.x;
     top = point.y;
     break;
    case 4:
     right = point.x;
     break;
    case 5:
     right = point.x;
     bottom = point.y;
     break;
    case 6:
     bottom = point.y;
     break;
    case 7:
     left = point.x;
     bottom = point.y;
     break;
    case 8:
     left = point.x;
     break;
   }
   this.pot = point;
   area.x = left;
   area.y = top;
   area.width = right - left;
   area.height = bottom - top;
  }

  private void mousecaptureform_doubleclick(object sender, system.eventargs e)
  {
   //修正截取圖片過界提示內存不足bug
   int left   =  area.left;
   int right  =  area.right;
   int top    =  area.top;
   int bottom =  area.bottom;
   if(left < screen.primaryscreen.bounds.left)
   {left = screen.primaryscreen.bounds.left; }
   if(right > screen.primaryscreen.bounds.right )
   {right = screen.primaryscreen.bounds.right;}
   if(top < screen.primaryscreen.bounds.top)
   {top = screen.primaryscreen.bounds.top;}
   if(bottom > screen.primaryscreen.bounds.bottom)
   {bottom = screen.primaryscreen.bounds.bottom;}
   area.x = left;
   area.y = top;
   area.width = right - left;
   area.height = bottom - top;

   //截取選擇區域圖片
   bitmap bm  = new bitmap(this.backgroundimage);
   this.img = bm.clone(this.area,system.drawing.imaging.pixelformat.format16bppargb1555);
   capturescreenform.image = img;
   capturescreenform.activeform.invalidate();
   this.close();
  }

  private void mousecaptureform_keydown(object sender, system.windows.forms.keyeventargs e)
  {
   if(e.keydata == keys.escape && system.windows.forms.button.mousebuttons == mousebuttons.left)
   {
    this.area=rectangle.empty;
    this.invalidate(); 
    tiptext.text = noselectstring;
   }
   else if(e.keydata == keys.escape)
   {this.close();}
  }

  private void tiptext_mousemove(object sender, system.windows.forms.mouseeventargs e)
  {
   if (tiptext.location.x == 10 && tiptext.location.y == 10)
   {
    point formloc = new point((screen.primaryscreen.bounds.width - 176),10) ;
    tiptext.location = formloc;
   }
   else
   {tiptext.location = new point(10,10);}    
  }

  private void mousecaptureform_load(object sender, system.eventargs e)
  {
   tiptext.text = noselectstring;
  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连平县| 霍邱县| 中江县| 五大连池市| 轮台县| 呼伦贝尔市| 深州市| 桦川县| 凤山市| 南和县| 永定县| 阿图什市| 筠连县| 磐安县| 唐河县| 罗平县| 苗栗市| 蓬安县| 郴州市| 长治县| 独山县| 巨野县| 宜兰县| 余姚市| 凌云县| 陇西县| 崇明县| 汾西县| 额尔古纳市| 清流县| 驻马店市| 囊谦县| 潼关县| 贞丰县| 蓬安县| 西宁市| 华亭县| 青川县| 富顺县| 麦盖提县| 鄱阳县|