Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some...

13
Making a cartoon or slideshow

Transcript of Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some...

Page 1: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Making a cartoon or slideshow

Page 2: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Aside VB comments

• An apostrophe starts a comment to end of line• Some of the following slides have code with

comments in them.

Page 3: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Timer & pictures

• Using a timer, you can create a cartoon or slideshow.

• Components you would use will be a single picturebox and a single timer.

• You will need to– Put an initial image on the picture box (in form load)– Set the timer interval and start the timer (in form load)– Somehow change the picture displayed in the timer-

tick sub event hander

Page 4: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Timer

• You add a timer to a project from the toolbox.• It does not go “on” your application like most

controls we have used.• Like a printer-capability, a timer goes in the

component “tray”, an area displayed “under” your design view.

• When you double-click the timer widget in the toolbox, it will appear in an area below your design window called the component tray.

Page 5: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Programming a Timer• You must start or stop the timer.• You must set the timing interval (in milli seconds)• You must enable the timer to let it start clicking, or disable it

to turn it off or pause slideshow.• Instructions to do these things must appear in an appropriate

place in your code.• Starting the timer might go in formload, or a buttonhandler

for “start slideshow” or “new Slideshow”.• Stopping the timer might go in a button handler for “stop

slideshow”

Page 6: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Timer

Here’s code which could go in a btnStart or in formload handler to set the timer interval to click every second:

Timer1.Interval = 1000 ‘=1 sec Timer1.Enabled = True Timer1.Start() ‘start it

Page 7: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

The slideshow code: formload

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

picnum=1 ‘ or 0 depending how you named pictures Timer1.Interval = 1000 ‘=1 sec… smaller to go faster Timer1.Enabled = True Timer1.Start() ‘start it pbOne.Image = Image.FromFile(“p:\mypictures\" &

name&picnum & ".jpg") ‘load first picture End Sub

Page 8: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Modular arithmetic

• Counting mod 2: 0,1,0,1,0,1,…• Counting mod 5: 0,1,2,3,4,0,1,2,3,4,0,1,…• You will have to count modulo the number of images

you created.• If your first picture is tree0.jpg and you have 10 pictures

(numbered 0 to 9) then you would count mod 10.• If your first picture is numbered 1, you will have to add 1

in your binary arithmetic or you will get an error.Num=num+1 ‘ regular countingNum=(Num+1) mod 10 ‘ counting mod 10… values 0..9

Page 9: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Creating an image name to load

• Your images will be on your p drive somewhere. So their names will start with “p:\”

• Your images are all type bmp or jpg or gif (or whatever). So their names will end with the same extension. If you made a bunch of jpg named tree0.jpg and so on, then their names all end with “.jpg”

• In VB, you can glue strings together with an &• If the variable num holds an integer 0 to 9 (for 10 pictures)

and your pictures are in a folder called p:\mypictures then their names will be constructed as “p:\mypictures\tree”&num&”.jpg”

Page 10: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

The timer-tick sub event handler• The timer tick sub handles what you wish to do each time the timer ticks.• This might be to move an image on a panel or a picturebox on an

application or load/display a new image in a picture box, or several of the above instructions.

• A common way to “cycle around” a set of images is to get the next one mod picturecount… If there are 4 pictures named pic0.jpg, pic1.jpg,pic2.jpg,pic3.jpg then

• Picbox.image=image.fromfile(“p:\pic” & num & “.jpg”) would get another picture from the p drive. Num must be incremented mod

4.• The pictures will all need to be the same type (jpg, gif, bmp, whatever).

Page 11: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Coding the timer

• Double-clicking the timer in the component tray will create a timer-tick sub event handler.

• This is where you put the code to change the number of the picture, to create the new picture name, and to load/display it.

Page 12: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Field variables

• The number integer variable to count which picture you are on is a field variable.

Dim picnum As Integer = 0 ’ or start at 1

Page 13: Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.

Timer tick sub example

‘Field variables Dim picnum As Integer = 0 Dim names() As String = {"dog", "tish", "keely",

"shan"} ‘a bunch of picnames Dim piclen As Integer = names.Length Private Sub Timer1_Tick(ByVal sender As

System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

picnum = (picnum + 1) Mod piclen PictureBox1.Image = Image.FromFile("C:\" &

names&picnum & ".jpg") End Sub