none
如何剪裁圖片 RRS feed

  • 問題

  •  假設有兩個PictureBox   A,B

    我需要在圖A 中裁減"固定長方形範圍"內的圖片 貼到 圖B ,該如何完成?

     

    EX:  圖A  size  640x480      需要的部分為  固定長方形範圍 內的區域影像   到圖B內

    2006年11月19日 上午 02:14

解答

  • HI,

    您可以這樣做:

    1. Imports System.Runtime.InteropServices

    2.<DllImport("gdi32.dll")> _
        Private Shared Function StretchBlt(ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal nsrcWidth As Integer, ByVal nsrcHeight As Integer, ByVal dwRop As Int32) As Boolean
        End Function

        <DllImport("User32.dll")> _
        Public Shared Function GetDC(ByVal hWnd As IntPtr) As System.IntPtr
        End Function

        <DllImport("User32.dll")> _
        Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
        End Function

    3.Dim srcDC As System.IntPtr = GetDC(PictureBox1.Handle)
            Dim destDC As System.IntPtr = GetDC(PictureBox2.Handle)

            StretchBlt(destDC, 0, 0, PictureBox2.Width, PictureBox2.Height, srcDC, 欲複製的區域的左上角點X座標, 欲複製的區域的左上角點Y座標, 欲複製的區域的寬度, 欲複製的區域的高度, &HCC0020) 'SRCCOPY

            ReleaseDC(PictureBox1.Handle, srcDC)
            ReleaseDC(PictureBox2.Handle, destDC)

    就可以達到剪裁圖片的效果

    tihs

     

    2006年11月19日 上午 06:31
  • HI,

    您可以再加入以下兩個宣告:

     <DllImport("User32.dll")> _
        Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As System.IntPtr
        End Function

    <DllImport("User32.dll")> _
        Public Shared Function GetDesktopWindow() As IntPtr
        End Function

    然後執行以下的程式碼:

    Dim srcDC As IntPtr = GetDC(PictureBox1.Handle)
    Dim deskHandle As IntPtr = GetDesktopWindow()
    Dim destDC As IntPtr = GetWindowDC(deskHandle)

    StretchBlt(destDC, 0, 0, 100, 100, srcDC, 0, 0, 100, 100, &HCC0020)

    就可以達到您的效果了

    tihs

    2006年11月23日 上午 06:04

所有回覆

  • :::試試看

            Dim A_image As New Bitmap(PictureBox1.Image)
            Dim B_image As New Bitmap(PictureBox2.Image)

            Dim g As Graphics = PictureBox3.CreateGraphics
            g.DrawImage(A_image, 0, 0)

            g.Clip = New Region(New RectangleF(0, 0, 50, 50))
            g.DrawImage(B_image, 0, 0)

    結果顯示在PictureBox3裡面!!!

    2006年11月19日 上午 04:25
  • HI,

    您可以這樣做:

    1. Imports System.Runtime.InteropServices

    2.<DllImport("gdi32.dll")> _
        Private Shared Function StretchBlt(ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal nsrcWidth As Integer, ByVal nsrcHeight As Integer, ByVal dwRop As Int32) As Boolean
        End Function

        <DllImport("User32.dll")> _
        Public Shared Function GetDC(ByVal hWnd As IntPtr) As System.IntPtr
        End Function

        <DllImport("User32.dll")> _
        Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
        End Function

    3.Dim srcDC As System.IntPtr = GetDC(PictureBox1.Handle)
            Dim destDC As System.IntPtr = GetDC(PictureBox2.Handle)

            StretchBlt(destDC, 0, 0, PictureBox2.Width, PictureBox2.Height, srcDC, 欲複製的區域的左上角點X座標, 欲複製的區域的左上角點Y座標, 欲複製的區域的寬度, 欲複製的區域的高度, &HCC0020) 'SRCCOPY

            ReleaseDC(PictureBox1.Handle, srcDC)
            ReleaseDC(PictureBox2.Handle, destDC)

    就可以達到剪裁圖片的效果

    tihs

     

    2006年11月19日 上午 06:31
  • 請問假設我要把圖擷取到在"桌面" 上,  其中ReleaseDC(PictureBox2.Handle, destDC)的參數1改如何取得  ?
    2006年11月20日 下午 02:47
  • HI,

    您可以再加入以下兩個宣告:

     <DllImport("User32.dll")> _
        Public Shared Function GetWindowDC(ByVal hWnd As IntPtr) As System.IntPtr
        End Function

    <DllImport("User32.dll")> _
        Public Shared Function GetDesktopWindow() As IntPtr
        End Function

    然後執行以下的程式碼:

    Dim srcDC As IntPtr = GetDC(PictureBox1.Handle)
    Dim deskHandle As IntPtr = GetDesktopWindow()
    Dim destDC As IntPtr = GetWindowDC(deskHandle)

    StretchBlt(destDC, 0, 0, 100, 100, srcDC, 0, 0, 100, 100, &HCC0020)

    就可以達到您的效果了

    tihs

    2006年11月23日 上午 06:04