首先得到播放器的播放窗口句柄,然后把它做为BitBlt函数的源DC句柄参数,调用该函数。

解决方案 »

  1.   

    IntPtr Mediaintptr = (IntPtr)FindWindow("WMPlayerApp","Windows Media Player");
    Graphics g = Graphics.FromHdc (Mediaintptr);
    IntPtr dcSrc=g.GetHdc ();
    获得源DC句柄,为什么会出现如下错误:
    未处理的“System.OutOfMemoryException”类型的异常出现在 system.drawing.dll 中。
    其他信息: 内存不足。
      

  2.   

    给你一个例子,把form1的代码用下列代码代替。
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Drawing.Imaging;
    using System.IO;// **********************************************
    // Written by Mike Gold
    // Copyright 2002 Microgold Software Inc.
    // All Rights Reserved
    // April 16, 2002
    // **********************************************namespace ScreenCaptureUtility
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    { public bool PossibleCapture = false;
    public bool CaptureRequested = false;
    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.Windows.Forms.MenuItem menuItem2;
    private Bitmap MyImage = null;
    private System.Windows.Forms.MenuItem menuItem3;
    private System.Windows.Forms.MenuItem menuItem4;
    private System.Windows.Forms.MenuItem menuItem5;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null; private bool firstCrop = false; public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();// SetStyle(ControlStyles.UserPaint, true);
    // SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    // SetStyle(ControlStyles.DoubleBuffer, true); SetBounds(0,0,0,0);// this.SetBounds(Screen.PrimaryScreen.Bounds.X,
    // Screen.PrimaryScreen.Bounds.Y,
    // Screen.PrimaryScreen.Bounds.Width,
    // Screen.PrimaryScreen.Bounds.Height);
       this.Visible = false; //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }
      

  3.   

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.mainMenu1 = new System.Windows.Forms.MainMenu();
    this.menuItem1 = new System.Windows.Forms.MenuItem();
    this.menuItem2 = new System.Windows.Forms.MenuItem();
    this.menuItem3 = new System.Windows.Forms.MenuItem();
    this.menuItem4 = new System.Windows.Forms.MenuItem();
    this.menuItem5 = new System.Windows.Forms.MenuItem();
    // 
    // mainMenu1
    // 
    this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem1});
    // 
    // menuItem1
    // 
    this.menuItem1.Index = 0;
    this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem2,
      this.menuItem3,
      this.menuItem4,
      this.menuItem5});
    this.menuItem1.Text = "File";
    // 
    // menuItem2
    // 
    this.menuItem2.Index = 0;
    this.menuItem2.Text = "Capture...";
    this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
    // 
    // menuItem3
    // 
    this.menuItem3.Index = 1;
    this.menuItem3.Text = "Crop...";
    this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
    // 
    // menuItem4
    // 
    this.menuItem4.Index = 2;
    this.menuItem4.Text = "Save...";
    this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
    // 
    // menuItem5
    // 
    this.menuItem5.Index = 3;
    this.menuItem5.Text = "Exit";
    this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.AutoScroll = true;
    this.ClientSize = new System.Drawing.Size(274, 219);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.KeyPreview = true;
    this.Menu = this.mainMenu1;
    this.Name = "Form1";
    this.Text = "MGScreenCapture";
    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
    this.Resize += new System.EventHandler(this.Form1_Resize);
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
    this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
    this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private bool ControlPressed = false;
    private bool ShiftPressed = false; private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    if (e.Modifiers  ==  Keys.Shift)
    {
    ShiftPressed = true;
    } if (e.Modifiers == Keys.Control)
    {
    ControlPressed = true;
    }
    if (e.Control)
    {
    ControlPressed = true;
    } if (e.Shift)
    {
    ShiftPressed = true;
    }
    } [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    private static extern bool BitBlt(
    IntPtr hdcDest, // handle to destination DC
    int nXDest,  // x-coord of destination upper-left corner
    int nYDest,  // y-coord of destination upper-left corner
    int nWidth,  // width of destination rectangle
    int nHeight, // height of destination rectangle
    IntPtr hdcSrc,  // handle to source DC
    int nXSrc,   // x-coordinate of source upper-left corner
    int nYSrc,   // y-coordinate of source upper-left corner
    System.Int32 dwRop  // raster operation code
    ); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    private static extern IntPtr  CreateDC(
                  string lpszDriver,        // driver name
      string lpszDevice,        // device name
      string lpszOutput,        // not used; should be NULL
     IntPtr lpInitData // optional printer data
      ); private void PerformCapture()
    {
    this.Visible = false;
    IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
    Graphics g1 = Graphics.FromHdc(dc1);
    MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
    Graphics g2 = Graphics.FromImage(MyImage);
    Visible = false;
        dc1 = g1.GetHdc();
    IntPtr dc2 = g2.GetHdc();
    BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc1, 0, 0, 13369376);
    g1.ReleaseHdc(dc1);
    g2.ReleaseHdc(dc2);
    MyImage.Save(@"c:\Captured.jpg", ImageFormat.Jpeg);
    Visible = true;
    MessageBox.Show("Finished Saving Image");
    this.Visible = true;
    this.SetBounds(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    SetupCropping();
    firstCrop = true;
    } private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if ((e.KeyChar == 16) && (ControlPressed && ShiftPressed))
    {
    PerformCapture();
    } } private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    if (e.Modifiers  ==  Keys.Shift)
    {
    ShiftPressed = false;
    } if (e.Modifiers == Keys.Control)
    {
    ControlPressed = false;
    }

    }
      

  4.   

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    // e.Graphics.FillRectangle(new SolidBrush(this.BackColor), ClientRectangle); if (MyImage != null)
    {
    e.Graphics.DrawImage(MyImage, ClientRectangle, 0, 0, MyImage.Width, MyImage.Height, GraphicsUnit.Pixel);
    } if (CroppingPaint == true)
    {
    Graphics g = this.CreateGraphics();
    g.DrawRectangle(Pens.Red, StartPoint.X, StartPoint.Y, DeltaX, DeltaY);
    g.Dispose();
    }
    } private void menuItem2_Click(object sender, System.EventArgs e)
    {
      PerformCapture();
    } private void pictureBox1_SizeChanged(object sender, System.EventArgs e)
    {
    } private void Form1_Resize(object sender, System.EventArgs e)
    {
    Invalidate();
    } private bool CropOn = false;
    private void menuItem3_Click(object sender, System.EventArgs e)
    {
    this.Left = 0;
    this.Top = 0;
    Bitmap theImage = new Bitmap(DeltaX, DeltaY);
    Graphics g = Graphics.FromImage(theImage);
    GraphicsUnit gu = GraphicsUnit.Pixel ;
    Rectangle destRect = new Rectangle(0, 0, DeltaX, DeltaY);
    // g.TranslateTransform(-StartPoint.X, -StartPoint.Y);
    if (firstCrop)
    {
    g.DrawImage(MyImage, destRect, StartPoint.X, StartPoint.Y + 7, theImage.Width, theImage.Height + 20, GraphicsUnit.Pixel);
    firstCrop = false;
    }
    else
    g.DrawImage(MyImage, destRect, StartPoint.X, StartPoint.Y, theImage.Width, theImage.Height, GraphicsUnit.Pixel); MyImage = (Bitmap)theImage.Clone();
    this.SetBounds(0, 0, MyImage.Width, MyImage.Height + 50);
    CroppingPaint = false;
    SetupCropping();
    Invalidate();
    } private void SetupCropping()
    {
    CropOn = true;
    this.Cursor = Cursors.Cross;
    } private bool CroppingPaint = false;
    Point StartPoint = new Point(0,0); private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if (StartedCrop == false)
    {
      DeltaX = 0;
      DeltaY  = 0;
    } if (CropOn == true)
    {
      StartedCrop = true;
      CroppingPaint = true;
      StartPoint = new Point(e.X, e.Y);
      DeltaX = 0;
      DeltaY = 0;
      Invalidate();
    } } private bool StartedCrop = false;
    private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      CropOn = false;
      StartedCrop = false;
      SetupCropping();
    } int DeltaX = 0;
    int DeltaY = 0;
    private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if ((CroppingPaint && CropOn) && StartedCrop)
    {
    DeltaX = e.X - StartPoint.X; 
    if (DeltaX < 0)
    DeltaX = 0;
    DeltaY = e.Y - StartPoint.Y; 
    if (DeltaY < 0)
    DeltaY = 0;// InvalidateClipBorders(new Rectangle(StartPoint.X, StartPoint.Y, DeltaX, DeltaY));
    Invalidate(new Rectangle(StartPoint.X, StartPoint.Y, DeltaX + 20, DeltaY + 20));
    } }
      

  5.   

    再加上如下代码: void InvalidateClipBorders(Rectangle rect)
    {
    const int thick = 10;
    const int thickx2 = 20;
    Invalidate(new Rectangle(rect.X - thick, rect.Y - thick,  thickx2, rect.Height + thick));
    Invalidate(new Rectangle(rect.X + rect.Width - thick, rect.Y - thick,  thickx2, rect.Height + thick));
    Invalidate(new Rectangle(rect.X - thick, rect.Y + rect.Height - thick,  rect.Width + thick, thickx2));
    Invalidate(new Rectangle(rect.X - thick, rect.Y - thick, rect.Width + thick , thickx2));
    } private void menuItem5_Click(object sender, System.EventArgs e)
    {
    Application.Exit();
    } private void menuItem4_Click(object sender, System.EventArgs e)
    {
    MyImage.Save(@"C:\Captured.jpg");
        MessageBox.Show("Image Saved.");
    }
    }
    }
      

  6.   

    不会的,给我你的email,我给你全部的代码。
      

  7.   

    to hunter4500
    我没有收到,你能否重新发一次?谢谢!
      

  8.   

    又发过去了一次。[email protected]
      

  9.   

    我收到了,谢谢!
    源程序中的那种抓图方法是不能抓到播放器播放的图片的。在WINDVD中的捕获按键能够捕获到播放器播放的视频。我就是想实现那种功能,调用BitBlt,是抓取不到视频图片的。
      

  10.   

    我记得好象要把autoredraw设为true