OKIEWARDOYO

C#.NET No.34

Sep 1st, 2013
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. /***********
  2.  * SIMPLE IMAGE GALLERY
  3.  * PREV AND NEXT BUTTON
  4.  * BY Okie Eko Wardoyo
  5.  * MediaTutorial.web.id
  6.  * *********/
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. using System.IO; //perlu ditambahkan untuk directory
  17.  
  18. namespace NEXT_PREVIOUS
  19. {
  20.     public partial class Form1 : Form
  21.     {
  22.         Bitmap img = null;
  23.  
  24.         //kita buat variable
  25.         string directoryPath;
  26.         string[] filesArray;
  27.         int totalFiles;
  28.         int i = 0;
  29.  
  30.         public Form1()
  31.         {
  32.             InitializeComponent();
  33.         }
  34.  
  35.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  36.         {
  37.             this.Close();
  38.         }
  39.  
  40.         private void openToolStripMenuItem_Click(object sender, EventArgs e)
  41.         {
  42.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  43.             {
  44.                 //kita jadikan mode zoom agar ukuran file fit dengan pictureBox
  45.                 pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
  46.  
  47.                 //pictureBox.Load(openFileDialog1.FileName);
  48.  
  49.                 img = new Bitmap(openFileDialog1.FileName);
  50.                 pictureBox.Image = img;
  51.  
  52.                 //kita coba mengambil directory dari FileName
  53.                 directoryPath = Path.GetDirectoryName(openFileDialog1.FileName);
  54.                
  55.                 //kita coba tampilkan di window output
  56.                 Console.WriteLine(directoryPath);
  57.  
  58.                 //mari kita buat array string
  59.                 filesArray = Directory.GetFiles(@directoryPath);
  60.                 //kita coba tampilkan di window output
  61.                 foreach (string file in filesArray)
  62.                 {
  63.                     Console.WriteLine(file);
  64.                 }
  65.  
  66.                 totalFiles = filesArray.Length;
  67.  
  68.                 //kita coba tampilkan di window output
  69.                 Console.WriteLine("Total semua = "+totalFiles);
  70.                
  71.                 //kita sekarang akan men-set curent i
  72.                 i = Array.IndexOf(filesArray, openFileDialog1.FileName);
  73.  
  74.                 //yuk kita tampilkan curent i di output biar keliatan
  75.                 Console.WriteLine("Curent i = " + i);
  76.             }
  77.            
  78.         }
  79.  
  80.         private void buttonNext_Click(object sender, EventArgs e)
  81.         {
  82.             if (img != null)
  83.             {
  84.                 if (i < totalFiles-1)
  85.                     i++;
  86.  
  87.                 img = new Bitmap(filesArray[i]);
  88.                 pictureBox.Image = img;
  89.  
  90.                 //kita coba tampilkan di window output
  91.                 Console.WriteLine("i = " + i);
  92.             }
  93.         }
  94.  
  95.         private void buttonPrev_Click(object sender, EventArgs e)
  96.         {
  97.             if (img != null)
  98.             {
  99.                 if (i > 0)
  100.                     i--;
  101.  
  102.                 img = new Bitmap(filesArray[i]);
  103.                 pictureBox.Image = img;
  104.  
  105.                 //kita coba tampilkan di window output
  106.                 Console.WriteLine("i = " + i);
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment