none
Trying To Capture An Area Of The Screen Image, All I Seem To Get Is A Black Box RRS feed

  • Question

  • Hello Community

    Thank you for reading, all help is greatly appreciated.
    I am attempting to capture an area of the screen image from user supplied rectangle co-ordinates. The code I'm trialling on a click event is as follows...

      Dim CoordinatesAsRectangle As Rectangle = New Rectangle(10, 10, 100, 100)
    
        Private Sub CaptureScreenImageWithinRectangle(sender As Object, e As EventArgs) Handles CaptureImageBUTTON.Click
    
            Dim ImageBoundaries As Rectangle = CoordinatesAsRectangle
            Dim CaptureImage As System.Drawing.Bitmap = New System.Drawing.Bitmap(ImageBoundaries.Width, ImageBoundaries.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
    
            PreviewPICTUREBOX.Image = CaptureImage
    
        End Sub

    All this keeps giving me in the previewPICTUREBOX is a black box equal to the size of CoordinatesAsRectangle dimensions.

    I Have played around with different  capture formats without success. Also have gone through forums; have seen similar code but cant seem to see why mine wont work. Or other queries similar to mine seem to use different systems and don't appear to apply to my code

    Was hoping someone can tell me where I've gone wrong please, or point me in the right direction.

    All help, as always, will be greatly appreciated.
    Thank you,

    {o_o}




    • Edited by AussieHack Saturday, June 3, 2017 5:34 AM
    Saturday, June 3, 2017 5:31 AM

Answers

  • I am attempting to capture an area of the screen image from user supplied rectangle co-ordinates.

    You don't have any code to capture the screen image. See:
    https://msdn.microsoft.com/en-us/library/system.drawing.graphics.copyfromscreen(v=vs.110).aspx

    Choose the overload you want to use, and look at the example provided there.

    • Proposed as answer by IronRazerz Saturday, June 3, 2017 10:14 AM
    • Marked as answer by AussieHack Tuesday, June 6, 2017 9:48 AM
    Saturday, June 3, 2017 5:50 AM
  • Its asking for a point value which I thought the rectangle supplied. I Cant seem to get the destination definition for the PictureBox correct.

    Rectangle.Size property is a Size object, and there is no overload of the CopyFromScreen method that takes a size object as the first argument.  I think the compiler is confused about which overload you want to use.  You must pass arguments of the correct type to get the result you want.  There are two overloads that take the operation as the final argument, and as you are using a size then I think the overload you want to use is

    CopyFromScreen(Point, Point, Size, CopyPixelOperation)

    I think that ImageBoundaries defines your source rectangle and that you want that source copied to the top left corner of the bitmap,  so the line should probably be:

    ImageCapture.CopyFromScreen(New Point(ImageBoundaries.Left, ImageBoundaries.Top), _
      New Point(0, 0), ImageBoundaries.Size, CopyPixelOperation.SourceCopy)
    You should always use Option Strict On at the top of your code.  It helps to avoid this sort of problem, and is particularly important when using methods with multiple overloads.

    • Marked as answer by AussieHack Tuesday, June 6, 2017 9:48 AM
    Tuesday, June 6, 2017 3:07 AM

All replies

  • I am attempting to capture an area of the screen image from user supplied rectangle co-ordinates.

    You don't have any code to capture the screen image. See:
    https://msdn.microsoft.com/en-us/library/system.drawing.graphics.copyfromscreen(v=vs.110).aspx

    Choose the overload you want to use, and look at the example provided there.

    • Proposed as answer by IronRazerz Saturday, June 3, 2017 10:14 AM
    • Marked as answer by AussieHack Tuesday, June 6, 2017 9:48 AM
    Saturday, June 3, 2017 5:50 AM
  • Thank you Acamar and IronRazerz for you help,

    I have gone through the link and have made some progress using Graphics.CopyFromSreen as suggested (was quite the lesson, have learned much tyvm for link).

    I Am close I think but sill an issue remains I cant resolve

    My trouble now is defining the preview pictureboxes bounds correctly as the copy destination. Here is my adjusted code....

    Private Sub CaptureScreenImageWithinRectangle(sender As Object, e As EventArgs) Handles CaptureImageBUTTON.Click
    
            Dim ImageBoundaries As Rectangle = New Rectangle(10, 10, 100, 100)
            Dim PreviewPictureBoxBounds As Rectangle = PreviewPICTUREBOX.Bounds
            Dim ImageCapture As Graphics = Nothing
    
            Dim ImageDefinition As System.Drawing.Bitmap = New System.Drawing.Bitmap(ImageBoundaries.Width, ImageBoundaries.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
    
            ImageCapture = Graphics.FromImage(ImageDefinition)
            ImageCapture.CopyFromScreen(ImageBoundaries.Size, PreviewPictureBoxBounds, CopyPixelOperation.SourceCopy)
    
        End Sub

    At the last line the compiler keeps giving me the error "Value of type 'CopyPixelOperation' cannot be converted to size" on the PreviewPictureBoxBounds Var.

    Its asking for a point value which I thought the rectangle supplied. I Cant seem to get the destination definition for the PictureBox correct.

    I think I'm close, probably overthinking it again. And is no doubt something silly (I seem rather good at messing up to blatantly obvious).

    A little more guidance would be great if at all possible please, again all help and replies are greatly appreciated.

    {o_o}






    • Edited by AussieHack Tuesday, June 6, 2017 1:38 AM
    Tuesday, June 6, 2017 1:30 AM
  • Its asking for a point value which I thought the rectangle supplied. I Cant seem to get the destination definition for the PictureBox correct.

    Rectangle.Size property is a Size object, and there is no overload of the CopyFromScreen method that takes a size object as the first argument.  I think the compiler is confused about which overload you want to use.  You must pass arguments of the correct type to get the result you want.  There are two overloads that take the operation as the final argument, and as you are using a size then I think the overload you want to use is

    CopyFromScreen(Point, Point, Size, CopyPixelOperation)

    I think that ImageBoundaries defines your source rectangle and that you want that source copied to the top left corner of the bitmap,  so the line should probably be:

    ImageCapture.CopyFromScreen(New Point(ImageBoundaries.Left, ImageBoundaries.Top), _
      New Point(0, 0), ImageBoundaries.Size, CopyPixelOperation.SourceCopy)
    You should always use Option Strict On at the top of your code.  It helps to avoid this sort of problem, and is particularly important when using methods with multiple overloads.

    • Marked as answer by AussieHack Tuesday, June 6, 2017 9:48 AM
    Tuesday, June 6, 2017 3:07 AM
  • Thank you Acamar for your reply,

    Yes that was it. Works exactly as expected thanks to your help. So ya, I had / have passed conflicting dimension and co-ordinate arguments when using the particular overload.
    Seemed obvious once you demonstrated it (again), wood for the trees thing perhaps?? (is my excuse:)

    I renamed the Vars to suit a little better. The sub to capture now is as follows...

      Dim ImageBounds As Rectangle = New Rectangle(10, 10, 100, 100)
            Dim DefineImageCapture As Graphics = Nothing
    
            Dim ImageCapture As System.Drawing.Bitmap = New System.Drawing.Bitmap(ImageBounds.Width, ImageBounds.Height,
                                                                                  System.Drawing.Imaging.PixelFormat.Format32bppRgb)
            DefineImageCapture = Graphics.FromImage(ImageCapture)
            DefineImageCapture.CopyFromScreen(New Point(ImageBounds.Left, ImageBounds.Top),
                                              New Point(0, 0), ImageBounds.Size, CopyPixelOperation.SourceCopy)
    
            PreviewPICTUREBOX.Image = ImageCapture
    
            End Sub
    Works great. And then to save Image I simply...
            ImageCapture.Save("C:\FilePath\File.BMP", Imaging.ImageFormat.Bmp)

    Thank you very much Acamar for all your assistance and patience. Your help has been invaluable yet again.

    {o_o}




    • Edited by AussieHack Tuesday, June 6, 2017 9:53 AM typos
    Tuesday, June 6, 2017 9:48 AM