none
請問是否有使用c#來擷取目前螢幕的程式呢? RRS feed

  • 問題

  • 因為公司需要

    想要做一個SERVER來即時拍下目前螢幕的畫面

    然後存成JPG或者MPEG黨

    不知道c#是否可以這樣子做呢?

    我知道市面上有很多的螢幕解取的程式

    但是我要的東西是我可以操控蛇麼時候擷取蛇麼時候不擷取

    甚至可以連續擷取幾張

    這樣子

    希望能有人可以提供擷取圖片的程式碼

    其他的我自己想辦法

    感激不盡

    2007年2月1日 上午 05:54

解答

  • public class Screen
    {
       [DllImport("gdi32.dll")]
       private static extern IntPtr CreateDC(string lpDriverName, string lpDeviceName, string lpOutput, string lpInitData);
       
       [DllImport("gdi32.dll")]
       private static extern IntPtr CreateCompatibleDC(IntPtr hDC);
     
       [DllImport("gdi32.dll")]
       private static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
     
       [DllImport("gdi32.dll")]
       private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
     
       [DllImport("gdi32.dll")]
       private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
     
       [DllImport("gdi32.dll")]
       public static extern int BitBlt(IntPtr desthDC, int srcX, int srcY, int srcW, int srcH, IntPtr srchDC, int destX, int destY, int op);
     
       [DllImport("gdi32.dll")]
       private static extern int DeleteDC(IntPtr hDC);
       
       [DllImport("gdi32.dll")]
       private static extern int DeleteObject(IntPtr hObj);
       
       public Screen()
       {
       }
     
       static public Bitmap CaptureControl(IntPtr SourceDC,int SourceWidth,int SourceHeight)
       {
          return Capture(SourceDC,SourceWidth,SourceHeight);
       }
     
       static public Bitmap CaptureScreen()
       {
          IntPtr screen=CreateDC("DISPLAY", "", "", "");
          int sourceWidth=GetDeviceCaps(screen, 8);
          int sourceHeight=GetDeviceCaps(screen, 10);
          Capture(screen,sourceWidth,sourceHeight);
          Bitmap ret=Capture(screen,sourceWidth,sourceHeight);
          DeleteDC(screen);
          return ret;
       }
     
       static private Bitmap Capture(IntPtr SourceDC,int SourceWidth,int SourceHeight)
       {
          IntPtr destDC;
          IntPtr BMP, BMPOld;
          destDC = CreateCompatibleDC(SourceDC);
          BMP = CreateCompatibleBitmap(SourceDC, SourceWidth, SourceHeight);
          BMPOld = SelectObject(destDC, BMP);
          BitBlt(destDC, 0, 0, SourceWidth, SourceHeight, SourceDC, 0, 0, 13369376);
          BMP = SelectObject(destDC, BMPOld);
          DeleteDC(destDC);
          Bitmap ret = Image.FromHbitmap(BMP);
          DeleteObject(BMP);
          return ret;
       }
    }

    使用:
    Bitmap bitmap=Screen.CaptureControl(button1.CreateGraphics().GetHdc(),button1.Width,button1.Height);
    bitmap.Save(@"c:\1.bmp");
    bitmap=Screen.CaptureScreen();
    bitmap.Save(@"c:\2.bmp");
    2007年2月2日 上午 02:30

所有回覆

  • Image myImage = new Bitmap(300, 200);
    Graphics g = Graphics.FromImage(myImage);
    g.CopyFromScreen(new Point(Cursor.Position.X - 150,            Cursor.Position.Y - 25), new Point(0, 0), new Size(300, 200));
    IntPtr dc1 = g.GetHdc();
    g.ReleaseHdc(dc1);
    this.pictureBox1.Image = myImage; 
    myImage.Save("c:\\1.jpeg");

     

    這是我在網路上找到的程式碼片段

    但是確找不到g.CopyFromScreen()

    這個方法....

    有人可以回答嗎?

    2007年2月1日 上午 08:41
  • public class Screen
    {
       [DllImport("gdi32.dll")]
       private static extern IntPtr CreateDC(string lpDriverName, string lpDeviceName, string lpOutput, string lpInitData);
       
       [DllImport("gdi32.dll")]
       private static extern IntPtr CreateCompatibleDC(IntPtr hDC);
     
       [DllImport("gdi32.dll")]
       private static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
     
       [DllImport("gdi32.dll")]
       private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
     
       [DllImport("gdi32.dll")]
       private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
     
       [DllImport("gdi32.dll")]
       public static extern int BitBlt(IntPtr desthDC, int srcX, int srcY, int srcW, int srcH, IntPtr srchDC, int destX, int destY, int op);
     
       [DllImport("gdi32.dll")]
       private static extern int DeleteDC(IntPtr hDC);
       
       [DllImport("gdi32.dll")]
       private static extern int DeleteObject(IntPtr hObj);
       
       public Screen()
       {
       }
     
       static public Bitmap CaptureControl(IntPtr SourceDC,int SourceWidth,int SourceHeight)
       {
          return Capture(SourceDC,SourceWidth,SourceHeight);
       }
     
       static public Bitmap CaptureScreen()
       {
          IntPtr screen=CreateDC("DISPLAY", "", "", "");
          int sourceWidth=GetDeviceCaps(screen, 8);
          int sourceHeight=GetDeviceCaps(screen, 10);
          Capture(screen,sourceWidth,sourceHeight);
          Bitmap ret=Capture(screen,sourceWidth,sourceHeight);
          DeleteDC(screen);
          return ret;
       }
     
       static private Bitmap Capture(IntPtr SourceDC,int SourceWidth,int SourceHeight)
       {
          IntPtr destDC;
          IntPtr BMP, BMPOld;
          destDC = CreateCompatibleDC(SourceDC);
          BMP = CreateCompatibleBitmap(SourceDC, SourceWidth, SourceHeight);
          BMPOld = SelectObject(destDC, BMP);
          BitBlt(destDC, 0, 0, SourceWidth, SourceHeight, SourceDC, 0, 0, 13369376);
          BMP = SelectObject(destDC, BMPOld);
          DeleteDC(destDC);
          Bitmap ret = Image.FromHbitmap(BMP);
          DeleteObject(BMP);
          return ret;
       }
    }

    使用:
    Bitmap bitmap=Screen.CaptureControl(button1.CreateGraphics().GetHdc(),button1.Width,button1.Height);
    bitmap.Save(@"c:\1.bmp");
    bitmap=Screen.CaptureScreen();
    bitmap.Save(@"c:\2.bmp");
    2007年2月2日 上午 02:30
  • 首先感謝Zion Wu 大大的提供CODE

    我沒有試可不可以

    但相信應該是可以使用的

    不過我找到更簡單的答案了

    原來上面的那八行必須在.net 2005上才能運行(2003好像不支援..還是我弄錯了...>"<  )

    很簡潔的幾行就可以達到螢幕擷取的功能了

    有興趣的人可以試試看

    2007年2月2日 上午 09:08