c# - 捕获屏幕并放入 PictureBox

标签 c# compact-framework windows-mobile-5.0

我正在使用 C# 开发一个工具,用于捕获屏幕的一部分并将其放入 PicBox 中,我已经能够获取屏幕截图,但我在某一点上遇到了麻烦。我拍摄的镜头是从屏幕的开头拍摄的,我想给出镜头应该开始的坐标(X,Y)。 这是我的代码,有什么建议吗?

#region DLLIMPORTS

    enum RasterOperation : uint { SRC_COPY = 0x00CC0020 }

    [DllImport("coredll.dll")]
    static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, RasterOperation rasterOperation);

    [DllImport("coredll.dll")]
    private static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("coredll.dll")]
    private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

    #endregion

    public PrtScreenFrm()
    {
        InitializeComponent();
    }
    private void MakeLayout()
    {

        this.imageOriginalPicBox.Image = PrtScreenProjectCF.Properties.Resources.testImage;
        this.imageOriginalPicBox.SizeMode = PictureBoxSizeMode.StretchImage;

    }

    private void PrtScreenFrm_Load(object sender, EventArgs e)
    {

        this.MakeLayout();

    }

    private void TakeScreenShot(Point _start, Point _end)
    {

        Rectangle boundsOfShot = new Rectangle((int)_start.X, (int)_start.Y, (int)(_end.X - _start.X), (int)(_end.Y - _start.Y));

        IntPtr hdc = GetDC(IntPtr.Zero);
        Bitmap shotTook = new Bitmap(boundsOfShot.Width, boundsOfShot.Height, PixelFormat.Format16bppRgb565);

        using (Graphics draw = Graphics.FromImage(shotTook))
        {

            IntPtr dstHdc = draw.GetHdc();
            BitBlt(dstHdc, 0, 0, boundsOfShot.Width, boundsOfShot.Height, hdc, 0, 0,
            RasterOperation.SRC_COPY);
            draw.ReleaseHdc(dstHdc);
        }

        imagePrintedPicBox.Image = shotTook;
        imagePrintedPicBox.SizeMode = PictureBoxSizeMode.StretchImage;

        ReleaseDC(IntPtr.Zero, hdc);

    }

    private void takeShotMenuItem_Click(object sender, EventArgs e)
    {

        Point start = new Point(3, 3);    //here I am forcing the rectangle
        Point end = new Point(237, 133);  //to start where the picBox starts 
        TakeScreenShot(start, end);       //but it doesn't work

    }

非常感谢,我可能错过了一些愚蠢的东西,请给我一盏灯。

最佳答案

问题是您的 BitBlt 指定 (0,0) 作为源 X-Y 位置。更改此行:

BitBlt(dstHdc, 0, 0, boundsOfShot.Width, boundsOfShot.Height, hdc, 
       0, 0, RasterOperation.SRC_COPY);

BitBlt(dstHdc, 0, 0, boundsOfShot.Width, boundsOfShot.Height, hdc, 
       _start.X, _start.Y, RasterOperation.SRC_COPY);

修复了代码问题。

如果您需要某个控件的屏幕位置(与 Location 属性不同),您可以使用:

Point _start = this.imageOriginalPicBox.PointToScreen(this.imageOriginalPicBox.Location);

上面的解决方案适用于紧凑框架,但如果您使用完整框架,则有点过于复杂。为了完整起见,这里有两种更好的方法。

正如 Yorye Nathan 所指出的在注释中,更简单的方法是使用 Graphics 类的 CopyFromScreen 方法。这将删除所有 p/Invokes,并将捕获代码减少为:

Rectangle boundsOfShot = new Rectangle(_start.X, _start.Y,
                                       _end.X - _start.X, _end.Y - _start.Y);

Bitmap shotTook = new Bitmap(boundsOfShot.Width, boundsOfShot.Height,
                             PixelFormat.Format16bppRgb565);

using (Graphics draw = Graphics.FromImage(shotTook))
{
    draw.CopyFromScreen(_start, new Point(0, 0), boundsOfShot.Size);
}

或者,如果您 try catch 自己的控件之一的位图:

Bitmap b = new Bitmap(this.imageOriginalPicBox.Width, this.imageOriginalPicBox.Height);
this.imageOriginalPicBox.DrawToBitmap(b, 
      new Rectangle(new Point(0, 0), this.imageOriginalPicBox.Size));    

关于c# - 捕获屏幕并放入 PictureBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33318782/

相关文章:

c# - 在 WCF 中支持具有属性的 XSD

javascript - CORS:凭据模式为 'include'

c# - Compact Framework 2.0 上的控制反转

windows-mobile - 找出哪些应用程序正在使用电池生命周期

c# - 分析 C# 应用程序转储文件

c# - 如何在 Oracle 查询 "Select-For-Update"中参数化等待时间

c# - Windows CE 上 WebBrowser 控件的替代品

c# - 如何创建私有(private)的非全局记录器

c# - Compact Framework - OpenNetCf.Net FTP 示例?

c# - Windows Mobile - 停止主手机应用程序