Asp.net如何截屏
生活随笔
收集整理的這篇文章主要介紹了
Asp.net如何截屏
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在C#案例開發(fā)這本書中提到了一個方法
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
namespace zhua2
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
/// <summary>
/// 必需的設(shè)計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設(shè)計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計器生成的代碼
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 24);
this.button1.TabIndex = 1;
this.button1.Text = "開始抓圖";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// saveFileDialog1
//
this.saveFileDialog1.FileName = "doc1";
this.saveFileDialog1.Filter = "jpg Files(*.jpg) |*.jpg |jpeg Files(*.*) |*.jpeg |bmp Files(*.bmp) |*.bmp";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 93);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "抓圖軟件";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//調(diào)用動態(tài)鏈接庫gdi32.dll
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest , //目標設(shè)備的句柄
int nXDest , // 目標對象的左上角的X坐標
int nYDest , // 目標對象的左上角的X坐標
int nWidth , // 目標對象的矩形的寬度
int nHeight , // 目標對象的矩形的長度
IntPtr hdcSrc , // 源設(shè)備的句柄
int nXSrc , // 源對象的左上角的X坐標
int nYSrc , // 源對象的左上角的X坐標
System.Int32 dwRop // 光柵的操作值
) ;
//調(diào)用動態(tài)鏈接庫gdi32.dll
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern IntPtr CreateDC (
string lpszDriver , // 驅(qū)動名稱
string lpszDevice , // 設(shè)備名稱
string lpszOutput , // 無用,可以設(shè)定位"NULL"
IntPtr lpInitData // 任意的打印機數(shù)據(jù)
) ;
private void button1_Click(object sender, System.EventArgs e)
{
this.Hide();
IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;
//創(chuàng)建顯示器的DC
Graphics g1 = Graphics.FromHdc ( dc1 ) ;
//由一個指定設(shè)備的句柄創(chuàng)建一個新的Graphics對象
Bitmap MyImage = new Bitmap ( Screen.PrimaryScreen.Bounds.Width , Screen.PrimaryScreen.Bounds.Height , g1 ) ;
//根據(jù)屏幕大小創(chuàng)建一個與之相同大小的Bitmap對象
Graphics g2 = Graphics.FromImage ( MyImage ) ;
//獲得屏幕的句柄
IntPtr dc3 = g1.GetHdc ( ) ;
//獲得位圖的句柄
IntPtr dc2 = g2.GetHdc ( ) ;
//把當前屏幕捕獲到位圖對象中
BitBlt ( dc2 , 0 , 0 , Screen.PrimaryScreen.Bounds.Width , Screen.PrimaryScreen.Bounds.Height , dc3 , 0 , 0 , 13369376 ) ;
//把當前屏幕拷貝到位圖中
g1.ReleaseHdc ( dc3 ) ;
//釋放屏幕句柄
g2.ReleaseHdc ( dc2 ) ;
//釋放位圖句柄
if (saveFileDialog1.ShowDialog () == DialogResult.OK )
{
MyImage.Save ( saveFileDialog1.FileName, ImageFormat.Bmp ) ;
MessageBox.Show ( "已經(jīng)把當前屏幕保存!" ) ;
this.Show();
}
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
namespace zhua2
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
/// <summary>
/// 必需的設(shè)計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設(shè)計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計器生成的代碼
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 24);
this.button1.TabIndex = 1;
this.button1.Text = "開始抓圖";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// saveFileDialog1
//
this.saveFileDialog1.FileName = "doc1";
this.saveFileDialog1.Filter = "jpg Files(*.jpg) |*.jpg |jpeg Files(*.*) |*.jpeg |bmp Files(*.bmp) |*.bmp";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 93);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "抓圖軟件";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//調(diào)用動態(tài)鏈接庫gdi32.dll
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest , //目標設(shè)備的句柄
int nXDest , // 目標對象的左上角的X坐標
int nYDest , // 目標對象的左上角的X坐標
int nWidth , // 目標對象的矩形的寬度
int nHeight , // 目標對象的矩形的長度
IntPtr hdcSrc , // 源設(shè)備的句柄
int nXSrc , // 源對象的左上角的X坐標
int nYSrc , // 源對象的左上角的X坐標
System.Int32 dwRop // 光柵的操作值
) ;
//調(diào)用動態(tài)鏈接庫gdi32.dll
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern IntPtr CreateDC (
string lpszDriver , // 驅(qū)動名稱
string lpszDevice , // 設(shè)備名稱
string lpszOutput , // 無用,可以設(shè)定位"NULL"
IntPtr lpInitData // 任意的打印機數(shù)據(jù)
) ;
private void button1_Click(object sender, System.EventArgs e)
{
this.Hide();
IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;
//創(chuàng)建顯示器的DC
Graphics g1 = Graphics.FromHdc ( dc1 ) ;
//由一個指定設(shè)備的句柄創(chuàng)建一個新的Graphics對象
Bitmap MyImage = new Bitmap ( Screen.PrimaryScreen.Bounds.Width , Screen.PrimaryScreen.Bounds.Height , g1 ) ;
//根據(jù)屏幕大小創(chuàng)建一個與之相同大小的Bitmap對象
Graphics g2 = Graphics.FromImage ( MyImage ) ;
//獲得屏幕的句柄
IntPtr dc3 = g1.GetHdc ( ) ;
//獲得位圖的句柄
IntPtr dc2 = g2.GetHdc ( ) ;
//把當前屏幕捕獲到位圖對象中
BitBlt ( dc2 , 0 , 0 , Screen.PrimaryScreen.Bounds.Width , Screen.PrimaryScreen.Bounds.Height , dc3 , 0 , 0 , 13369376 ) ;
//把當前屏幕拷貝到位圖中
g1.ReleaseHdc ( dc3 ) ;
//釋放屏幕句柄
g2.ReleaseHdc ( dc2 ) ;
//釋放位圖句柄
if (saveFileDialog1.ShowDialog () == DialogResult.OK )
{
MyImage.Save ( saveFileDialog1.FileName, ImageFormat.Bmp ) ;
MessageBox.Show ( "已經(jīng)把當前屏幕保存!" ) ;
this.Show();
}
}
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/goody9807/archive/2008/10/29/1322298.html
總結(jié)
以上是生活随笔為你收集整理的Asp.net如何截屏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RedHat下JDK1.6安装-利用al
- 下一篇: 在预览fastreport报表之前改变一