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

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

Swapping Pictures within PictureBox

My guess is that this is very simple.

I have a PictureBox and when the user clicks certain button on my Form I need to change the Picture within the PictureBox.

I will have two pictures that I will be swapping between.

I need code to store two pictures (which happen to come from a property called Image() of an control I purchased...which grabs images from a webcam) and at the click of a button change from one to the other in the PictureBox.
0
dwieringa
Asked:
dwieringa
  • 3
  • 3
  • 2
1 Solution
 
bruintjeCommented:
Hi dwieringa,
----------

from your comment i assume Image() is an array so what you can do on the button click is loading one picture from the array

have a public module variable blnFirstIsCurrent as boolean

then in your buttonclick try something like

if blnFirstIsCurrent then
  With myPictureBox
      Set .Picture = Nothing
      Set .Picture = Image(2)
      .Refresh
  End With
else
  With myPictureBox
      Set .Picture = Nothing
      Set .Picture = Image(1)
      .Refresh
  End With
end if

----------
bruintje
share what you know, learn what you don't
0
 
dwieringaAuthor Commented:
bruintje:

No, sorry to mislead you.

I copy the images into the Picture box with:

   thePictureBox.Picture = myPreviewForm.ControlName.Image

Once I copy it into the PictureBox I can't safely go back to the control.  I want to move it into "holding" data items.  One will be the raw image.  The other I will change various pixels using the PSet function -- think of this as "decoration" on top of the image.  I want the user to be able to toggle between the raw and decorated images.

Is this clearer?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This demonstrates the basic idea...

Option Explicit

Private img As StdPicture

Private Sub Form_Load()
    Set img = LoadPicture("C:\Documents and Settings\Michael\My Documents\My Pictures\3_1024x768.jpg")
    Set Picture1.Picture = LoadPicture("C:\Documents and Settings\Michael\My Documents\My Pictures\1_1024x768.jpg")
End Sub

Private Sub Command1_Click()
    Dim tmp As StdPicture
    Set tmp = Picture1.Picture
    Set Picture1.Picture = img
    Set img = tmp
End Sub
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
bruintjeCommented:
.... you got it there Idle_Mind was still typing :-)

indeed using a copy to take a step back to the original or one step back in the addition of decoration to the image
0
 
dwieringaAuthor Commented:
Idle Mind:

Thanks.

One question...  What is the significance of the "Set" each time we are copying the images?  Does it have something to do with copying by value vs by reference?

One comment... At first this "didn't work" for me (I probably wasn't specific enough).  I use PictureBox.pset to alter various pixels in my "decorated" version.  Following your pattern above, I kept losing these decorations.  I found I need to use Picture1.Image instead of Picture1.Picture if I want to copy these drawing changes.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
We use Set because we are dealing with an Object.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is another way to do your "decorations"...

Option Explicit

Private overlayOn As Boolean

Private Sub Command1_Click()
    overlayOn = Not overlayOn
    Picture1.Refresh
End Sub

Private Sub Picture1_Paint()
    If overlayOn Then
        Picture1.Line (0, 0)-(Picture1.ScaleWidth, Picture1.ScaleHeight), vbRed
    End If
End Sub
0
 
dwieringaAuthor Commented:
Idle Mind:

Thanks.  I actually use something like that for other markings on the images.  The stuff I'm doing with PSet looks at each pixel and then paints it a different color based on rules.  Over a large area, this is too slow for frequent repainting.  ...which is why I'm storing that in the StdPicture and toggling it via the picture swap.

Thanks again.

Dave
0

Featured Post

Technology Partners: 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!

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