Home » VB.NetRSS

Screen Capture with Cropping

Hi how would I allow my screen capture to crop and capture only a particular area of a screen?

The user should be able to select the area they want to crop. It would be good if the cursor changers to a plus like other screen captures and also the screen is all paused so the user can select the area they want.v

 

4 Answers Found

 

Answer 1

Try this link, there is example of what you looking for on image crop using mouse to select  http://www.vbcodesource.com/vbNetExamples.php

kaymaf

 

Answer 2

Try this link, there is example of what you looking for on image crop using mouse to select  http://www.vbcodesource.com/vbNetExamples.php

kaymaf


CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/ .

http://www.developerfusion.com/tools/convert/csharp-to-vb/ .

hi kaymaf, yes that is what i was looking for but at the example it crops the picturebox named "p", can you change the code so instead it crops the screen. here is the code, please can you also remove codes that do not involve with cropping. The code:

 

 

 

'''Title: VB.NET Example of using the Mouse to Select Image Cropping Cordinates'''By: Jason Hensley'''Contact: mailto:jason@vbcodesource.com'''Website: http://www.vbcodesource.com or http://www.vbcodesource.info'''Description: A very nice example I made of how to crop images inside 'of a picturebox control. Made it to where you press and hold the left 'mouse button and then move the mouse and it will make a rectangle and 'you put inside the rectange the portion of the picture you want to keep 'and the rest will be cropped from the image. Then click the crop button 'and it will take the selection you specified and put that section of 'the image in the other picturebox control. This would be a very nice 'and user friendly feature to add to a imaging application. '''''Copyright: 2004, June 15'Imports System.Drawing
Imports System.Drawing.Imaging
'PublicClass frmMain
  Inherits System.Windows.Forms.Form

  'bitmap to contain the cropped imageDim cropBitmap As Bitmap

  'the position and size to crop the image fileDim cropX AsIntegerDim cropY AsIntegerDim cropWidth AsIntegerDim cropHeight AsInteger'create a pen objectPublic cropPen As Pen

  'select a default crop line sizePublic cropPenSize AsInteger = 2

  'will contain the dashStyle of the penPublic cropDashStyle As Drawing2D.DashStyle = Drawing2D.DashStyle.Solid

  'set a default crop line colorPublic cropPenColor As Color = Color.Aquamarine

  'this is for changing the mouseicon.'being used for setting cropping points. 'makes it more user friendlyPublic c As Cursors


#Region" Windows Form Designer generated code "PublicSubNew()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() callEndSub'Form overrides dispose to clean up the component list.ProtectedOverloadsOverridesSub Dispose(ByVal disposing AsBoolean)
    If disposing ThenIfNot (components IsNothing) Then
        components.Dispose()
      EndIfEndIfMyBase.Dispose(disposing)
  EndSub'Required by the Windows Form DesignerPrivate components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor.FriendWithEvents p As System.Windows.Forms.PictureBox
  FriendWithEvents btnOpen As System.Windows.Forms.Button
  FriendWithEvents btnSave As System.Windows.Forms.Button
  FriendWithEvents btnCrop As System.Windows.Forms.Button
  FriendWithEvents pbCrop As System.Windows.Forms.PictureBox
  <System.Diagnostics.DebuggerStepThrough()> PrivateSub InitializeComponent()
    Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
    Me.p = New System.Windows.Forms.PictureBox
    Me.btnOpen = New System.Windows.Forms.Button
    Me.btnSave = New System.Windows.Forms.Button
    Me.btnCrop = New System.Windows.Forms.Button
    Me.pbCrop = New System.Windows.Forms.PictureBox
    Me.SuspendLayout()
    ''p'Me.p.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    Me.p.Image = CType(resources.GetObject("p.Image"), System.Drawing.Image)
    Me.p.Location = New System.Drawing.Point(8, 8)
    Me.p.Name = "p"Me.p.Size = New System.Drawing.Size(344, 208)
    Me.p.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
    Me.p.TabIndex = 0
    Me.p.TabStop = False''btnOpen'Me.btnOpen.Location = New System.Drawing.Point(8, 224)
    Me.btnOpen.Name = "btnOpen"Me.btnOpen.TabIndex = 2
    Me.btnOpen.Text = "Open"''btnSave'Me.btnSave.Location = New System.Drawing.Point(88, 224)
    Me.btnSave.Name = "btnSave"Me.btnSave.TabIndex = 3
    Me.btnSave.Text = "Save"''btnCrop'Me.btnCrop.Location = New System.Drawing.Point(184, 224)
    Me.btnCrop.Name = "btnCrop"Me.btnCrop.TabIndex = 4
    Me.btnCrop.Text = "Crop"''pbCrop'Me.pbCrop.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    Me.pbCrop.Location = New System.Drawing.Point(360, 8)
    Me.pbCrop.Name = "pbCrop"Me.pbCrop.Size = New System.Drawing.Size(344, 208)
    Me.pbCrop.TabIndex = 5
    Me.pbCrop.TabStop = False''frmMain'Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(712, 253)
    Me.Controls.Add(Me.pbCrop)
    Me.Controls.Add(Me.btnCrop)
    Me.Controls.Add(Me.btnSave)
    Me.Controls.Add(Me.btnOpen)
    Me.Controls.Add(Me.p)
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
    Me.Name = "frmMain"Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = " VB.NET - Cropping images and using the Mouse to Select the Cordinates"Me.ResumeLayout(False)

  EndSub#End RegionPrivateSub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click

    Try'Create a new Open Dialog object to get our image fromDim openDLG AsNew OpenFileDialog

      'Specifiy what type of files you want to work with
      openDLG.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg"'If the user clicked the OK button on the dialog...If openDLG.ShowDialog = DialogResult.OK Then'then load the selected image file in the source picturebox
        p.Image = Image.FromFile(openDLG.FileName, True)

      EndIfCatch exc As Exception

      MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    EndTryEndSubPrivateSub btnCrop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrop.Click

    TryIf cropWidth < 1 Then'if there are not proper cropping cordinates, then throw a message and exit code
        MessageBox.Show("You need to first select what portion of the image to crop.", " No cropping Cordinates!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        ExitSubEndIf'a rectangle to set the location and size from the source imageDim rect As Rectangle = New Rectangle(cropX, cropY, cropWidth, cropHeight)

      'You will need to remember that you will want to decide the width and height of the'bitmap according to what the sizemode of the picturebox that contains the image is.'If the sizemode = Stretch, then you will want the width/height of the control itself.'IF the sizemode = Normal, then you will want the width/height of the IMAGE. So on,'and so forth depending on the picturebox controls sizemode. If you do not do this,'then you will end up with a completely different cropping image then what you gave'cordinates for.Dim bit As Bitmap = New Bitmap(p.Image, p.Width, p.Height)

      'create a new bitmap with the width/height values that were specified in the textboxes.
      cropBitmap = New Bitmap(cropWidth, cropHeight)

      'a new Graphics object that will draw on the cropBitmapDim g As Graphics = Graphics.FromImage(cropBitmap)

      'draw the portion of the image that you supplied cropping values for.
      g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)

      'go ahead and make the new portion of the image a PART of the picturebox instead of'just drawing on it. Because if you draw the image through the Graphic class, then'you will lose the image whenever the application minimized or got covered up since'the image is not persisted on the control.
      pbCrop.Image = cropBitmap

    Catch exc As Exception

      MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    EndTryEndSubPrivateSub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

    Try'make sure there is a image to save before we go any furtherIf pbCrop.Image IsNothingThen

        MessageBox.Show("You have not edited the original image. There is no new image to save.", " Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ExitSubEndIf'create a new instance of the saveDialog objectDim saveDLG As SaveFileDialog = New SaveFileDialog

      'Specifiy what type of files you want to save the image as.
      saveDLG.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg"'If the user clicked the OK button on the dialog...If saveDLG.ShowDialog = DialogResult.OK ThenIf saveDLG.FileName.EndsWith("bmp") Then'save the image as a bitmap image
          pbCrop.Image.Save(saveDLG.FileName, ImageFormat.Bmp)

        ElseIf saveDLG.FileName.EndsWith("gif") Then'save the image in the gif format
          pbCrop.Image.Save(saveDLG.FileName, ImageFormat.Gif)

        Else'save the image as a jpeg image type
          pbCrop.Image.Save(saveDLG.FileName, ImageFormat.Jpeg)

        EndIf'clean up
        saveDLG.Dispose()

      EndIfCatch exc As Exception

      MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    EndTryEndSubPrivateSub p_MouseDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.MouseEventArgs) Handles p.MouseDown

    TryIf e.Button = MouseButtons.Left Then

        cropX = e.X
        cropY = e.Y

        cropPen = New Pen(cropPenColor, cropPenSize)
        cropPen.DashStyle = cropDashStyle

        'change the mouse pointer to the cross cursor which kinda helps when'sizing for the crop
        Cursor = c.Cross

      EndIf'clear the previoius crop lines by clicking on the pic
      p.Refresh()


    Catch exc As Exception

      MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    EndTryEndSubPrivateSub p_MouseUp(ByVal sender AsObject, ByVal e As System.Windows.Forms.MouseEventArgs) Handles p.MouseUp

    Try'change the mouse pointer back to the default cursor
      Cursor = c.DefaultCatch exc As Exception

      MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    EndTryEndSubPrivateSub p_MouseMove(ByVal sender AsObject, ByVal e As System.Windows.Forms.MouseEventArgs) Handles p.MouseMove

    TryIf p.Image IsNothingThenExitSubIf e.Button = MouseButtons.Left Then'clear the previous drawn crop lines
        p.Refresh()

        'need to take into count where the mouse started at'and calculate the new position
        cropWidth = e.X - cropX
        cropHeight = e.Y - cropY

        'draw the outline for the cropping
        p.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)

      EndIf'release as much memory as possible
      GC.Collect()

    Catch exc As Exception

      'this is a error when the cropping parameters are less than 1If Err.Number = 5 ThenExitSub

      MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    EndTryEndSubEndClass

 

Answer 3

It can be done using Graphics Class in System.Drawing Namespace. Using function CopyFromScreen.

Check this link.

http://www.codeproject.com/KB/cs/TeboScreen.aspx

 

Answer 4

It can be done using Graphics Class in System.Drawing Namespace. Using function CopyFromScreen.

Check this link.

http://www.codeproject.com/KB/cs/TeboScreen.aspx


Regards, Chethan Kamath. "Remember that if the world didn't suck, we'd all fall off."

Yes I see this is what I want but can you copy and paste the code for cropping the image in vb?
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter