Advertisement
Kimossab

WinForm Show Animated Image

Jul 10th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. import clr
  2. import os
  3. import tempfile
  4. import threading
  5. clr.AddReference('System')
  6. clr.AddReference('System.Windows.Forms')
  7. clr.AddReference('System.Drawing')
  8.  
  9. from System import IO
  10. from System.IO import Path, File
  11. from System.Windows.Forms import Application, DockStyle, Form, PictureBox, PictureBoxSizeMode, Timer
  12. from System.Drawing import Image, Bitmap, Rectangle, Size
  13. from time import sleep
  14.  
  15. pathToFile = 'JC.png'
  16. col = 4
  17. lin = 2
  18.  
  19. def GetRectangle(img):
  20.     h = img.Height/lin
  21.     w = img.Width/col
  22.     return [w,h]
  23.  
  24. def CropImage(img, rect):
  25.     bmpCrop = img.Clone(rect,img.PixelFormat)
  26.     return bmpCrop
  27.  
  28.  
  29.  
  30. class MainForm(Form):
  31.  
  32.     def Draw(self,):
  33.         retan = Rectangle(self.ccol*self.ret[0],self.clin*self.ret[1],self.ret[0],self.ret[1])
  34.         self.ccol+=1
  35.         if self.ccol == col:
  36.             self.ccol = 0
  37.             self.clin+=1
  38.         if self.clin == lin:
  39.             self.clin = 0;
  40.         self.pictureBox.Image = CropImage(self.image,retan)
  41.  
  42.     def __init__(self):
  43.         Form.__init__(self)
  44.         self.ccol = 0
  45.         self.clin = 0
  46.         self.pictureBox = PictureBox()
  47.         self.image = Image.FromFile(pathToFile)
  48.         self.ret = GetRectangle(self.image)
  49.         retan = Rectangle(0,0,self.ret[0],self.ret[1])
  50.         self.ClientSize = Size(self.ret[0],self.ret[1])
  51.  
  52.         self.pictureBox.SizeMode = PictureBoxSizeMode.StretchImage
  53.         self.pictureBox.Dock = DockStyle.Fill
  54.  
  55.         self.timer = Timer()
  56.         self.timer.Interval = 100
  57.         self.timer.Enabled = True
  58.         self.timer.Tick += self.OnTick
  59.         self.timer.Start()
  60.  
  61.         self.Controls.Add(self.pictureBox)
  62.  
  63.         self.Show()
  64.  
  65.     def OnTick(self, sender, event):
  66.         self.Draw()
  67.         self.Refresh()
  68.  
  69.  
  70. Application.EnableVisualStyles()
  71. form = MainForm()
  72. Application.Run(form)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement