?
Solved

cropping a graphic

Posted on 1998-06-21
7
Medium Priority
?
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 240 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

771 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