Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Crop Image

Posted on 2006-11-05
4
Medium Priority
?
328 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 86

Accepted Solution

by:
Mike Tomlinson earned 80 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

971 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