C# Get Desktop Screenshot ZZ

I found this feature while looking inside Graphics class and since it was so simple to use, I decided to post it here.

As I said, it doesn't require more than 15 lines of code - this function: Graphics.CopyFromScreen does all the 'hard work' so we only need to put this into a Bitmap and save/display it.

To the code!



There are 3 steps that you need to follow:

  • create a Bitmap that's exactly the screen's size
  • using that Bitmap, create a Graphics object (Graphics.FromImage)
  • use CopyFromScreen() and save the Bitmap



The code looks like this:

    1. private void takeScreenShot()
    2. {
    3. Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    4. using (Graphics g = Graphics.FromImage(bmp))
    5. {
    6. g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
    7. bmp.Save("screenshot.png"); // saves the image
    8. }
    9. }

转载于:https://www.cnblogs.com/zeroone/p/3378784.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值