c#圖像截取實(shí)例
本文實(shí)例講述了c#圖像截取的實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
圖像截取的相關(guān)代碼如下:
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Image pic = new Bitmap(this.Width, this.Height);
Graphics graphic = Graphics.FromImage(pic);
graphic.CopyFromScreen(new Point(this.Location.X, this.Location.Y), new Point(0, 0), new Size(this.Width, this.Height));
pic.Save(@"d:/test.jpeg", ImageFormat.Jpeg);
graphic.Dispose();
}
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, //目標(biāo)設(shè)備的句柄
int nXDest, // 目標(biāo)對(duì)象的左上角的X坐標(biāo)
int nYDest, // 目標(biāo)對(duì)象的左上角的X坐標(biāo)
int nWidth, // 目標(biāo)對(duì)象的矩形的寬度
int nHeight, // 目標(biāo)對(duì)象的矩形的長度
IntPtr hdcSrc, // 源設(shè)備的句柄
int nXSrc, // 源對(duì)象的左上角的X坐標(biāo)
int nYSrc, // 源對(duì)象的左上角的X坐標(biāo)
System.Int32 dwRop // 光柵的操作值
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateDC(
string lpszDriver, // 驅(qū)動(dòng)名稱
string lpszDevice, // 設(shè)備名稱
string lpszOutput, // 無用,可以設(shè)定位"NULL"
IntPtr lpInitData // 任意的打印機(jī)數(shù)據(jù)
);
private void Form1_SizeChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
IntPtr dc1 = CreateDC("DISPLAY", null,
null, (IntPtr)null);
//創(chuàng)建顯示器的DC
Graphics g1 = Graphics.FromHdc(dc1);
//由一個(gè)指定設(shè)備的句柄創(chuàng)建一個(gè)新的Graphics對(duì)象
Bitmap MyImage =
new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height, g1);
//根據(jù)屏幕大小創(chuàng)建一個(gè)與之相同大小的Bitmap對(duì)象
Graphics g2 = Graphics.FromImage(MyImage);
//獲得屏幕的句柄
IntPtr dc3 = g1.GetHdc();
//獲得位圖的句柄
IntPtr dc2 = g2.GetHdc();
//把當(dāng)前屏幕捕獲到位圖對(duì)象中
BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
dc3, 0, 0, 13369376);
//把當(dāng)前屏幕拷貝到位圖中
g1.ReleaseHdc(dc3);
//釋放屏幕句柄
g2.ReleaseHdc(dc2);
//釋放位圖句柄
Bitmap img = new Bitmap(MyImage, 800, 600);
//縮放圖片到800*600
img.Save("d:\\MyJpeg.jpg", ImageFormat.Jpeg);
MessageBox.Show("已經(jīng)把當(dāng)前屏幕保存到" +
"C:\\MyJpeg.jpg文件中!");
this.Show();
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
使用aspose.word 第三方的插件實(shí)現(xiàn)導(dǎo)出word
本文給大家分享的是一個(gè)使用使用aspose.word 第三方的插件實(shí)現(xiàn)導(dǎo)出word的實(shí)例,十分的實(shí)用,有需要的小伙伴可以參考下。2015-06-06C#面向?qū)ο笾M實(shí)現(xiàn)商城購物功能
這篇文章主要為大家詳細(xì)介紹了C#面向?qū)ο笾M實(shí)現(xiàn)商城購物功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C#實(shí)現(xiàn)統(tǒng)計(jì)字?jǐn)?shù)功能的方法
這篇文章主要介紹了C#實(shí)現(xiàn)統(tǒng)計(jì)字?jǐn)?shù)功能的方法,較為詳細(xì)的分析了C#字?jǐn)?shù)統(tǒng)計(jì)功能的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#實(shí)現(xiàn)隨鼠標(biāo)移動(dòng)窗體實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)隨鼠標(biāo)移動(dòng)窗體實(shí)例,主要通過簡單的窗體事件代碼即可實(shí)現(xiàn)鼠標(biāo)隨窗體移動(dòng)的功能,非常簡單實(shí)用,需要的朋友可以參考下2014-10-10