locked
Saving an image RRS feed

  • Question

  • Hello everyone,

    I am working on a program, of which I would like the user to be able to click a button, and when that button is clicked, it will take a Screen Shot of the screen, use code to copy that off of the clipboard, prompt the user where to save it, and save it as a Bitmap picture file.

    How can this be done?

    Sincerely,

    - Jake M.
    Thursday, November 12, 2009 3:12 AM

Answers

  • hi
    try for following code
    Window win = Current.MainWindow;
                RenderTargetBitmap bmp = new RenderTargetBitmap((int)win.Width, (int)win.Height, 96, 96, PixelFormats.Pbgra32);
                bmp.Render(win);

                string PicPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Picture");
                if (!Directory.Exists(PicPath))
                    Directory.CreateDirectory(PicPath);

                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bmp));

                string filePath = Path.Combine(PicPath, string.Format("{0:MMddyyyyhhmmss}.png", DateTime.Now));
                using (Stream stream = File.Create(filePath))
                {
                    encoder.Save(stream);
                }


    this will automatically save the picture @ specified path


    hope it wil work for you
    Harshad..... Always 4 U
    • Proposed as answer by Harjohn Friday, November 20, 2009 5:31 AM
    • Marked as answer by Blazing Ardor Tuesday, December 1, 2009 4:10 AM
    Thursday, November 12, 2009 5:26 AM
  • Hi Jake,

    You can add a reference to the System.Windows.Forms.dll, then you can use the Screen class to get the width and height of the screen, and use the Graphics.CopyFromScreen() method to take a screenshot.

    using .......
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Imaging;

      private void button1_Click(object sender, RoutedEventArgs e)
            {
                int screenWidth = Screen.PrimaryScreen.Bounds.Width ;
                int screenHeight = Screen.PrimaryScreen.Bounds.Height;
                Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
                Graphics gfx = Graphics.FromImage((System.Drawing.Image)bmpScreenShot);
                gfx.CopyFromScreen (0, 0, 0, 0, new System.Drawing.Size(screenWidth, screenHeight));
                bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
            }

    If anything is unclear, please feel free to let me know.

    Best Regards,
    Zhi-Xin


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Zhi-Xin Ye Friday, November 20, 2009 3:58 AM
    Thursday, November 19, 2009 8:58 AM
  • OK, that is because there are many classes of the same name in both System.Drawing namespace and other namespaces. You can use the full name of the classes instead. i.e.


    'remove the Imports System.Drawing here
    Imports System.Windows.Forms

    Class Window1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

            Dim screenWidth As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
            Dim screenHeight As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height

            Dim bmpScreenshot As System.Drawing.Bitmap = New System.Drawing.Bitmap (screenWidth, screenHeight)
            Dim gdx As System.Drawing.Graphics = System.Drawing.Graphics .FromImage(bmpScreenshot)
            gdx.CopyFromScreen(0, 0, 0, 0, New System.Drawing.Size (screenWidth, screenHeight))
            Dim filebrowserDialog As FolderBrowserDialog = New FolderBrowserDialog()
            If (filebrowserDialog.ShowDialog() = Forms.DialogResult.OK) Then
                Dim path As String = filebrowserDialog.SelectedPath
                Dim filename As String = "test.bmp"
                bmpScreenshot.Save(path + filename, System.Drawing.Imaging.ImageFormat.Bmp)
                MessageBox.Show("File is saved successfully!")
            End If
        End Sub

    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Blazing Ardor Tuesday, December 1, 2009 4:10 AM
    Tuesday, December 1, 2009 3:16 AM
  • Change this line:

     bmpScreenshot.Save(path + filename, System.Drawing.Imaging.ImageFormat.Bmp)

    to:

     bmpScreenshot.Save(path + "/" + filename, System.Drawing.Imaging.ImageFormat.Bmp)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Blazing Ardor Tuesday, December 1, 2009 4:10 AM
    Tuesday, December 1, 2009 4:01 AM

All replies

  • hi
    try for following code
    Window win = Current.MainWindow;
                RenderTargetBitmap bmp = new RenderTargetBitmap((int)win.Width, (int)win.Height, 96, 96, PixelFormats.Pbgra32);
                bmp.Render(win);

                string PicPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Picture");
                if (!Directory.Exists(PicPath))
                    Directory.CreateDirectory(PicPath);

                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bmp));

                string filePath = Path.Combine(PicPath, string.Format("{0:MMddyyyyhhmmss}.png", DateTime.Now));
                using (Stream stream = File.Create(filePath))
                {
                    encoder.Save(stream);
                }


    this will automatically save the picture @ specified path


    hope it wil work for you
    Harshad..... Always 4 U
    • Proposed as answer by Harjohn Friday, November 20, 2009 5:31 AM
    • Marked as answer by Blazing Ardor Tuesday, December 1, 2009 4:10 AM
    Thursday, November 12, 2009 5:26 AM
  • Hi Jake,

    You can add a reference to the System.Windows.Forms.dll, then you can use the Screen class to get the width and height of the screen, and use the Graphics.CopyFromScreen() method to take a screenshot.

    using .......
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Imaging;

      private void button1_Click(object sender, RoutedEventArgs e)
            {
                int screenWidth = Screen.PrimaryScreen.Bounds.Width ;
                int screenHeight = Screen.PrimaryScreen.Bounds.Height;
                Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
                Graphics gfx = Graphics.FromImage((System.Drawing.Image)bmpScreenShot);
                gfx.CopyFromScreen (0, 0, 0, 0, new System.Drawing.Size(screenWidth, screenHeight));
                bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
            }

    If anything is unclear, please feel free to let me know.

    Best Regards,
    Zhi-Xin


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Zhi-Xin Ye Friday, November 20, 2009 3:58 AM
    Thursday, November 19, 2009 8:58 AM
  • Probably should have mentioned this before, but I'm using Visual Basic.

    Also, I want the user to be able to save the screen shot in a .bmp format and be able to specify which directory it is saved in.

    Any ideas?


    Sincerely, 

    - Jake M.
    Friday, November 20, 2009 11:55 PM
  • To save the image as .bmp format, you can pass ImageFormat.Bmp to the save method. To let the users choose directory, you can use the OpenFileDialog.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Saturday, November 21, 2009 12:35 PM
  • I'm sorry, but I still do not understand. Could you be a little more specific please? I'm kinda new at programming as a whole.
    Tuesday, November 24, 2009 1:38 AM
  • Hi,

    You can do something like this, use the FolderBrowserDialog to select a folder to save the screenshot.

    First add a reference to the System.Windows.Forms.dll.

    Imports System.Drawing
    Imports System.Windows.Forms

    Class Window1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

            Dim screenWidth As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
            Dim screenHeight As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height

            Dim bmpScreenshot As Bitmap = New Bitmap(screenWidth, screenHeight)
            Dim gdx As Graphics = Graphics.FromImage(bmpScreenshot)
            gdx.CopyFromScreen(0, 0, 0, 0, New System.Drawing.Size(screenWidth, screenHeight))
            Dim filebrowserDialog As FolderBrowserDialog = New FolderBrowserDialog()
            If (filebrowserDialog.ShowDialog() = Forms.DialogResult.OK) Then
                Dim path As String = filebrowserDialog.SelectedPath
                Dim filename As String = "test.bmp"
                bmpScreenshot.Save(path + filename, System.Drawing.Imaging.ImageFormat.Bmp)
                MessageBox.Show("File is saved successfully!")
            End If

        End Sub

    End Class


    If anything is unclear, please feel free to let me know.

    Best Regards,
    Zhi-Xin



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Tuesday, November 24, 2009 7:45 AM
  • Theres no errors in this section when I do it this way, but my program uses brushes and colors, and when I use the Imports.System.Drawing, it throws 102 errors, most of them about a color or brush issue. What can I do about this?
    Tuesday, December 1, 2009 2:19 AM
  • OK, that is because there are many classes of the same name in both System.Drawing namespace and other namespaces. You can use the full name of the classes instead. i.e.


    'remove the Imports System.Drawing here
    Imports System.Windows.Forms

    Class Window1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

            Dim screenWidth As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
            Dim screenHeight As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height

            Dim bmpScreenshot As System.Drawing.Bitmap = New System.Drawing.Bitmap (screenWidth, screenHeight)
            Dim gdx As System.Drawing.Graphics = System.Drawing.Graphics .FromImage(bmpScreenshot)
            gdx.CopyFromScreen(0, 0, 0, 0, New System.Drawing.Size (screenWidth, screenHeight))
            Dim filebrowserDialog As FolderBrowserDialog = New FolderBrowserDialog()
            If (filebrowserDialog.ShowDialog() = Forms.DialogResult.OK) Then
                Dim path As String = filebrowserDialog.SelectedPath
                Dim filename As String = "test.bmp"
                bmpScreenshot.Save(path + filename, System.Drawing.Imaging.ImageFormat.Bmp)
                MessageBox.Show("File is saved successfully!")
            End If
        End Sub

    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Blazing Ardor Tuesday, December 1, 2009 4:10 AM
    Tuesday, December 1, 2009 3:16 AM
  • This works to some extent. But it does something odd though. I use the code:

    Dim screenWidth As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
            Dim screenHeight As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height

            Dim bmpScreenshot As System.Drawing.Bitmap = New System.Drawing.Bitmap(screenWidth, screenHeight)
            Dim gdx As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmpScreenshot)
            gdx.CopyFromScreen(0, 0, 0, 0, New System.Drawing.Size(screenWidth, screenHeight))
            Dim filebrowserDialog As System.Windows.Forms.FolderBrowserDialog = New System.Windows.Forms.FolderBrowserDialog()
            If (filebrowserDialog.ShowDialog() = Forms.DialogResult.OK) Then
                Dim path As String = filebrowserDialog.SelectedPath
                Dim filename As String = "test.bmp"
                bmpScreenshot.Save(path + filename, System.Drawing.Imaging.ImageFormat.Bmp)
                MessageBox.Show("File is saved successfully!")
            End If

    But when the dialog box opens, I select a path and click okay. It says that it is saved. But what it really does is save one directory above the directory you've chosen, and renaming the file to that directory plus the work "test". For example, I saved it to my Desktop, and found the image in my user folder named "Desktoptest".

    Another example just in case this isn't clear:

    Path chosen: Desktop\New Folder
    File creation location: Desktop\New Foldertest.bmp

    Path chosen: C:\Windows
    File creation location: C:\Windowstest.bmp

    Any ideas?
    Tuesday, December 1, 2009 3:55 AM
  • Change this line:

     bmpScreenshot.Save(path + filename, System.Drawing.Imaging.ImageFormat.Bmp)

    to:

     bmpScreenshot.Save(path + "/" + filename, System.Drawing.Imaging.ImageFormat.Bmp)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Blazing Ardor Tuesday, December 1, 2009 4:10 AM
    Tuesday, December 1, 2009 4:01 AM
  • Thanks! This works just right now.

    P. S. Is there a FileDialogBox that allows the use to specify the file name? If not I'm sure I can find a workaround.
    Tuesday, December 1, 2009 4:11 AM
  • You can try the OpenFileDialog.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Tuesday, December 1, 2009 6:35 AM