Capture screenshot of active window?

前端 未结 11 2424
春和景丽
春和景丽 2020-11-21 23:34

I am making a screen capturing application and everything is going fine. All I need to do is capture the active window and take a screenshot of this active window. Does an

11条回答
  •  日久生厌
    2020-11-22 00:19

    A little tweak to method static void ImageSave() will grant you the option where to save it. Credit goes to Microsoft (http://msdn.microsoft.com/en-us/library/sfezx97z.aspx)

    static void ImageSave(string filename, ImageFormat format, Image image, SaveFileDialog saveFileDialog1)
        { 
            saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
            saveFileDialog1.Title = "Enregistrer un image";
            saveFileDialog1.ShowDialog();
    
            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                // Saves the Image via a FileStream created by the OpenFile method.
                System.IO.FileStream fs =
                   (System.IO.FileStream)saveFileDialog1.OpenFile();
                // Saves the Image in the appropriate ImageFormat based upon the
                // File type selected in the dialog box.
                // NOTE that the FilterIndex property is one-based.
                switch (saveFileDialog1.FilterIndex)
                {
                    case 1:
                        image.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
    
                    case 2:
                        image.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Bmp);
                        break;
    
                    case 3:
                        image.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Gif);
                        break;
                }
    
                fs.Close();
            }
    
    
    
        }
    

    Your button_click event should be coded something like this...

    private void btnScreenShot_Click(object sender, EventArgs e)
        {
    
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    
    
            ScreenCapturer.CaptureAndSave(filename, mode, format, saveFileDialog1);
    
        }//
    

提交回复
热议问题