[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

cropping a graphic

hello...

i'm doing programming in vb 5.0 and i'm looking for a way to code a program that allows the user to select a particular section of a wmf or bmp file and then crop the rest of the graphic outside the select box.  or the user could click an x1 and a y2 position using a mouse and then produce the same result.

can anyone help me?  thanks in advance
0
janeausten
Asked:
janeausten
1 Solution
 
twardCommented:
Look into the StretchBlt Function:

Public Const BLACKNESS = &H42           ' (DWORD) dest = BLACK
Public Const DSTINVERT = &H550009       ' (DWORD) dest = (NOT dest)
Public Const MERGECOPY = &HC000CA       ' (DWORD) dest = (source AND pattern)
Public Const MERGEPAINT = &HBB0226      ' (DWORD) dest = (NOT source) OR dest
Public Const NOTSRCCOPY = &H330008      ' (DWORD) dest = (NOT source)
Public Const NOTSRCERASE = &H1100A6     ' (DWORD) dest = (NOT src) AND (NOT dest)
Public Const PATCOPY = &HF00021         ' (DWORD) dest = pattern
Public Const PATINVERT = &H5A0049       ' (DWORD) dest = pattern XOR dest
Public Const PATPAINT = &HFB0A09        ' (DWORD) dest = DPSnoo
Public Const SRCAND = &H8800C6          ' (DWORD) dest = source AND dest
Public Const SRCCOPY = &HCC0020         ' (DWORD) dest = source
Public Const SRCERASE = &H440328        ' (DWORD) dest = source AND (NOT dest )
Public Const SRCINVERT = &H660046       ' (DWORD) dest = source XOR dest
Public Const SRCPAINT = &HEE0086        ' (DWORD) dest = source OR dest
Public Const WHITENESS = &HFF0062       ' (DWORD) dest = WHITE

Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

0
 
janeaustenAuthor Commented:
hi, tward,

i'm not really rejecting your answer, i'm giving you the points, but
i'd like to request for more elaboration on the subject if you can.
do i  need createdc?  it's kinda hard to find materials on this subject (api programming).

thanks for everything.

jane austen
0
 
soeding1Commented:
janeausten,
still interested in your question. I could answer it.
Regards
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
janeaustenAuthor Commented:
dear soedingl,

i'll be very much obliged if you would.  didn't hear from
tward again.  i think the ...blt's are the answer, but i'm
clueless as to how i could use them.

thanks
0
 
soedingCommented:
Hi janeausten,
sorry for the long time.
Here's the code.
The only problem is how to show the blinking rectangle.
Place two picture boxes (Picture1 and Picture2), and a command button (Command1) on a form.

Option Explicit
Private Declare Function SetCaretPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function ShowCaret Lib "user32" (ByVal hwnd As Long) As Long

'If you would like to declare this function in a module,
'you would have to write "Public Declare..."
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

'If you would like to declare these constants in a module,
'you would have to write "Public Const..."
'Also try the constants, tward proposed, for image conversion.
'In this case, you will need:
Const SRCCOPY = &HCC0020

'Initialize cropping area:
Dim newLeft&
Dim newTop&
Dim newWidth&
Dim newHeight&
 
Private Sub Form_Load()
  'Place two picture boxes and a command button on your form.
 
  'I think much of your problems was to get a device context.
  'Image boxes do not have (easy to find) device contexts!
  'Also, there is no PaintPicture method for image boxes.
 
  'Try "cutting" an area with the mouse...
 
  'Set all scale modes to pixels:
  Picture1.ScaleMode = vbPixels
  Picture2.ScaleMode = vbPixels
 
  'Set the pictures to autosize:
  Picture1.AutoSize = True
  Picture2.AutoSize = True
 
  'This is the picture, you later will crop.
  Picture1.Picture = LoadPicture(App.Path & "\test.bmp")
End Sub

Private Sub Command1_Click()
  'Define cropping area:
  newLeft = 20
  newTop = 20
  newWidth = Picture1.ScaleWidth - newLeft
  newHeight = Picture1.ScaleHeight - newTop
 
  'AutoSize will not work, when you use BitBlt.
  'Therefore resize manually:
  Picture2.Width = newWidth + Picture2.Width - Picture2.ScaleWidth
  Picture2.Height = newHeight + Picture2.Height - Picture2.ScaleHeight

  Picture2.Cls
 
  'BitBlt
  'It will copy an area of the picture1 to picture2:
  BitBlt Picture2.hDC, _
  0, 0, newWidth, newHeight, _
  Picture1.hDC, newLeft, newTop, SRCCOPY
 
  '(Here are your device contexts.)
  'Of course, (one of) the pictures may be hidden.
  'Also, you may BitBlt into the picture1.
 
  'PaintPicture is (quite) the same as BitBlt,
  'but the picture will autosize.
  'Now, you will not use the picture's hDC,
  'but the property 'Picture'.
'  Picture2.PaintPicture Picture1.Picture, _
'  0, 0, newWidth, newHeight, _
'  newLeft, newTop, newWidth, newHeight, _
'  SRCCOPY
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  'Save the newLeft and newTop coordinates
  newLeft = X
  newTop = Y
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  'does not work...
  SetCaretPos 50, 150
  ShowCaret Me.hwnd
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If X >= newLeft Then
    newWidth = X - newLeft
  Else: newWidth = newLeft - X
  End If
 
   If Y >= newTop Then
    newHeight = Y - newTop
  Else: newHeight = newTop - Y
  End If
 
  Picture2.Width = newWidth + Picture2.Width - Picture2.ScaleWidth
  Picture2.Height = newHeight + Picture2.Height - Picture2.ScaleHeight
 
  Picture2.Cls
 
  BitBlt Picture2.hDC, _
  0, 0, newWidth, newHeight, _
  Picture1.hDC, newLeft, newTop, SRCCOPY
End Sub

0
 
janeaustenAuthor Commented:
hi, soeding...

the delay's no prob; actually i'm working on another project
right now, so i might not be able to evaluate your answer at
once.  anyway i'm sure to use your code, though not right now.
i'll post a comment once it works to tell you to repost it as
an answer.

thanks in advance

jane austen
0
 
janeaustenAuthor Commented:
oops, made a mistake.. you have posted it as an answer =)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now