Solved

cropping a graphic

Posted on 1998-06-21
7
428 Views
Last Modified: 2011-09-20
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
Comment
Question by:janeausten
7 Comments
 
LVL 7

Expert Comment

by:tward
ID: 1463941
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
 

Author Comment

by:janeausten
ID: 1463942
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
 

Expert Comment

by:soeding1
ID: 1463943
janeausten,
still interested in your question. I could answer it.
Regards
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:janeausten
ID: 1463944
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
 
LVL 1

Accepted Solution

by:
soeding earned 120 total points
ID: 1463945
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
 

Author Comment

by:janeausten
ID: 1463946
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
 

Author Comment

by:janeausten
ID: 1463947
oops, made a mistake.. you have posted it as an answer =)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
change vba from autofit to 13.5 width? 4 29
Determine Range to Select 5 52
MS Access 03, TransferText, decimal places 8 78
transition to visual .net from vb6 5 39
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

829 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