C#截屏

C#截屏
2011-07-06 23:02

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

using System.Runtime.InteropServices ;

using System.IO;

 

using System.Media;

 

namespace BaiheTest

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

            this.FormBorderStyle = FormBorderStyle.None;

            this.Size = Screen.PrimaryScreen.Bounds.Size;

            this.Location = new Point(0,0);

            SoundPlayer sp = new SoundPlayer(Properties.Resources.mp3);

            sp.PlayLooping();

        }

// 还可以这样 :

 

        [DllImport("gdi32.dll")]

        private static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);

        [DllImport("gdi32.dll")]

        private static extern bool BitBlt(IntPtr hdcDest,

                                                                int nXDest,

                                                                int nYDest,

                                                                int nWidth,

                                                                int nHeight,

                                                                IntPtr hdcSrc,

                                                                int nXSrc,

                                                                int nYSrc,

                                                                System.Int32 dwRop);

 

        private void Form2_Load(object sender, EventArgs e)

        {

            panel1.Visible = false;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

           // SoundPlayer sp = new SoundPlayer();

           // sp.Play();

            this.Hide();

            Application.DoEvents();

            pauseMinSecond(2000);     

            int w = Screen.PrimaryScreen.Bounds.Width;

            int h = Screen.PrimaryScreen.Bounds.Height;

            IntPtr dc1 = CreateDC("Display", null, null, (IntPtr)null);

            Graphics g1 = Graphics.FromHdc(dc1);

            Bitmap bmp1 = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);

            Graphics bmp2 = Graphics.FromImage(bmp1);

            IntPtr screenHandle = g1.GetHdc();

            IntPtr imageHandle = bmp2.GetHdc();

            BitBlt(imageHandle, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, screenHandle, 0, 0, 13369376);

            g1.ReleaseHdc();

            bmp2.ReleaseHdc();

            MemoryStream oStream = new MemoryStream();

            Bitmap photo = new Bitmap(w, h);

            Graphics graphic = Graphics.FromImage(photo);

            graphic.CopyFromScreen(0, 0, 0, 0, new Size(photo.Width, photo.Height));

            photo.Save(oStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            Image img = Image.FromStream(oStream);

            this.BackgroundImage = img;

            // 这四句话就可以截屏了,但是就是不能截取视频的屏幕。。。

            this.Show ();

            Application.DoEvents();

 

        }

 

        void pauseMinSecond(int msecond)

        {

 

            DateTime t1 = DateTime.Now.AddMilliseconds(msecond);

 

            while (DateTime.Now < t1)

            {

                int i = 3;

                i += 6;

                Application.DoEvents();

            }

 

            return;

 

        }

 

 

        private void button2_Click(object sender, EventArgs e)

        {

 

            Close();

 

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            MessageBox.Show("my baby?");

            panel1.Visible = true; 

            this.BackgroundImage = Properties.Resources.to_My_bay1;

 

        }

        private void timer1_Tick(object sender, EventArgs e)

        {

 

            if (label1.Top <= -label1.Height) {

 

                label1.Top = this.panel1.Height;           

            

            }

 

            label1.Top -= 3;

 

        }

 

        private void panel1_DragOver(object sender, DragEventArgs e)

        {

 

        }

 

        private void panel1_MouseHover(object sender, EventArgs e)

        {

 

        }

 

        private void panel1_MouseEnter(object sender, EventArgs e)

        {

 

            timer1.Stop();

        }

 

        private void panel1_MouseLeave(object sender, EventArgs e)

        {

 

            timer1.Start();

        }

 

    }

}

转载于:https://www.cnblogs.com/vmyspace/archive/2011/12/27/2303311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值