Solved

Crop Image

Posted on 2006-11-05
4
313 Views
Last Modified: 2012-08-14
Ok. Using a webcam to capture a pic of a client.

The web cam's orientation is landscape, I need to capture a portrait. So I make the picturebox the right size. I then want to capture what I see in the picture box. (top left of the webcam feed). The picbox however saves the whole feed, not just the part that is visible to me.

I have tried to use this, but cannot get it to work effectively.

            bmpImage = New Bitmap(picCapture.Image)
            recCrop = New Rectangle(0, 0, 96, 120)
            bmpCrop = New Bitmap(recCrop.Width, recCrop.Height, bmpImage.PixelFormat)
            gphCrop = Graphics.FromImage(bmpCrop)
            recDest = New Rectangle(0, 0, 96, 120)
            gphCrop.DrawImage(bmpImage, recDest, recCrop.X, recCrop.Y, recCrop.Width, recCrop.Height, GraphicsUnit.Pixel)



            bmpCrop.Save(Application.StartupPath & "\tmp.jpg", Imaging.ImageFormat.Jpeg)

Depending on the size of recCrop, I either get a very small top left part of the pic, (pic dimensions of 96x120), but really its a VERY small part of the top left corner. If I make recCrop 640x480, I get a 640x480 pic with the top left the piece that I want.???

PLease help.
0
Comment
Question by:x50Fade
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17881642
I don't understand what you are looking for.

Bob
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 20 total points
ID: 17881656
Here is a different approach...we simply take a screen cap of your picturebox:

    Private Const SRCCOPY = &HCC0020

    Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
    Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
    Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
    Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
    Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal SrcX As Integer, ByVal SrcY As Integer, ByVal Rop As Integer) As Integer
    Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
    Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' convert PictureBox rectangle coordinates to screen coordinates
        Dim r As Rectangle = Me.RectangleToScreen(New Rectangle(Me.picCapture.Location, Me.picCapture.Size))
        ' grab the image of the PictureBox
        Dim bmp As Bitmap = GetScreen(r.X, r.Y, r.Width, r.Height)
        ' do something with it...
        bmp.Save(Application.StartupPath & "\tmp.jpg", Imaging.ImageFormat.Jpeg)
    End Sub

    Private Function GetScreen(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) As Bitmap
        Dim hDeskDC As Integer
        Dim hTempDC As Integer
        Dim hBitmap As Integer
        Dim hTempBmp As Integer
        Dim desktopArea As Bitmap

        hDeskDC = CreateDC("DISPLAY", "", "", "")
        If hDeskDC Then
            hTempDC = CreateCompatibleDC(hDeskDC)
            If hTempDC Then
                hBitmap = CreateCompatibleBitmap(hDeskDC, Width, Height)
                If hBitmap Then
                    hTempBmp = SelectObject(hTempDC, hBitmap)
                    BitBlt(hTempDC, 0, 0, Width, Height, hDeskDC, X, Y, SRCCOPY)
                    desktopArea = Bitmap.FromHbitmap(New IntPtr(hBitmap))
                    DeleteObject(SelectObject(hTempDC, hTempBmp))
                End If
                DeleteDC(hTempDC)
            End If
            DeleteDC(hDeskDC)
        End If
        Return desktopArea
    End Function
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question