Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 12.66 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Runtime.Remoting.Messaging;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.Security;
  11. using System.Timers;
  12. using System.Threading;
  13.  
  14. namespace PictureView
  15. {
  16.     public delegate void AsyncMethodCaller();
  17.  
  18.     [Description("PictureView для просмотра изображений")]
  19.     [ToolboxItem(true)]
  20.     [ToolboxBitmap(typeof(PictureView), "PictureView.bmp")]
  21.  
  22.     public partial class PictureView : UserControl
  23.     {
  24.         private float size;
  25.         private List<pictureList> pictListSave;
  26.         private bool randGalery;
  27.         private int iNoRand;
  28.         System.Timers.Timer pictureTimer = new System.Timers.Timer();
  29.         private Thread pictRefreshThreadL;
  30.         //private Thread pictRefreshThreadR;
  31.         private int nThredRefr;
  32.        
  33.  
  34.  
  35.         public PictureView()
  36.         {
  37.             InitializeComponent();
  38.             size = this.Width / 50;
  39.             pictListSave = new List<pictureList>();
  40.  
  41.             CheckForIllegalCrossThreadCalls = false;
  42.  
  43.             randGalery = false;
  44.             pictRefreshThreadL = new Thread(PictRefreshL);
  45.             //pictRefreshThreadR = new Thread(PictRefreshR);
  46.  
  47.             pictureTimer.Elapsed += new ElapsedEventHandler(GaleryEvent);
  48.             pictureTimer.Interval = 1000;
  49.             pictureTimer.Enabled = false;
  50.  
  51.             nThredRefr = 0;
  52.  
  53.  
  54.         }
  55.  
  56.         //private void PictRefresh(object sender, EventArgs e)
  57.         public void PictRefreshL()
  58.         {
  59.             //while (true)
  60.             //{
  61.             //    if (pictListSave.Count != 0 && nThredRefr >= 0 && nThredRefr < pictListSave.Count)
  62.             //    {
  63.             //        for (int i = nThredRefr; i >= 0; i--)
  64.             //        {
  65.             //            if ((pictListSave[i].pictureBox.Location.X <= panel.Width && pictListSave[i].pictureBox.Location.X > 0) || ((pictListSave[i].pictureBox.Location.X + pictListSave[i].pictureBox.Width) <= panel.Width && (pictListSave[i].pictureBox.Location.X + pictListSave[i].pictureBox.Width) > 0))
  66.             //            {
  67.             //                pictListSave[i].pictureElementShow();
  68.             //                nThredRefr = i - 1;
  69.  
  70.             //            }
  71.             //            else
  72.             //            {
  73.             //                pictListSave[i].pictureElementClear();
  74.             //            }
  75.             //        }
  76.             //        panel.Refresh();
  77.             //        GC.Collect();
  78.             //    }
  79.             //}
  80.             while(true)
  81.             {
  82.                 for (int i = 0; i < pictListSave.Count; i++)
  83.                 {
  84.                     if ((pictListSave[i].pictureBox.Location.X <= panel.Width && pictListSave[i].pictureBox.Location.X > 0) || ((pictListSave[i].pictureBox.Location.X + pictListSave[i].pictureBox.Width) <= panel.Width && (pictListSave[i].pictureBox.Location.X + pictListSave[i].pictureBox.Width) > 0))
  85.                     {
  86.                         pictListSave[i].pictureElementShow();
  87.                     }
  88.                     else
  89.                     {
  90.                         pictListSave[i].pictureElementClear();
  91.                     }
  92.  
  93.                 }
  94.                 panel.Refresh();
  95.                 GC.Collect();
  96.             }
  97.  
  98.         }
  99.  
  100.         //public void PictRefreshR()
  101.         //{
  102.         //    while (true)
  103.         //    {
  104.         //        if (pictListSave.Count != 0 && nThredRefr >= 0 && nThredRefr < pictListSave.Count)
  105.         //        {
  106.         //            for (int i = nThredRefr; i < pictListSave.Count; i++)
  107.         //            {
  108.         //                if ((pictListSave[i].pictureBox.Location.X <= panel.Width && pictListSave[i].pictureBox.Location.X > 0) || ((pictListSave[i].pictureBox.Location.X + pictListSave[i].pictureBox.Width) <= panel.Width && (pictListSave[i].pictureBox.Location.X + pictListSave[i].pictureBox.Width) > 0))
  109.         //                {
  110.         //                    pictListSave[i].pictureElementShow();
  111.         //                    nThredRefr = i - 1;
  112.         //                }
  113.         //                else
  114.         //                {
  115.         //                    pictListSave[i].pictureElementClear();
  116.         //                }
  117.         //            }
  118.         //            panel.Refresh();
  119.         //            GC.Collect();
  120.         //        }
  121.         //    }
  122.         //}
  123.  
  124.         public void Start()
  125.         {
  126.             pictRefreshThreadL.Start();
  127.             //pictRefreshThreadR.Start();
  128.         }
  129.  
  130.         public void Abort()
  131.         {
  132.             pictRefreshThreadL.Abort();
  133.             //pictRefreshThreadR.Abort();
  134.         }
  135.  
  136.         public void Clear()
  137.         {
  138.             nThredRefr = -1;
  139.             if (pictureTimer.Enabled == true)
  140.             {
  141.                 pictureTimer.Stop();
  142.                 pictureTimer.Enabled = false;
  143.             }
  144.             pictListSave.Clear();
  145.             panel.Controls.Clear();
  146.             panel.Refresh();
  147.             label.Text = null;
  148.             viewBox.Image = null;
  149.             viewBox.Invalidate();
  150.             GC.Collect();
  151.         }
  152.  
  153.         public void Add()
  154.         {
  155.             OpenFileDialog openFileDialog = new OpenFileDialog();
  156.             openFileDialog.Multiselect = true;
  157.             openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  158.             openFileDialog.Filter = "Files (*.jpg,*.png,*.bmp)|*.jpg|*.png|*.bmp";
  159.             if (openFileDialog.ShowDialog(this) == DialogResult.OK)
  160.             {
  161.                 foreach (String file in openFileDialog.FileNames)
  162.                 {
  163.                     try
  164.                     {
  165.                         pictListSave.Add(new pictureList(file, pictListSave.Count + 1));
  166.                         panel.Controls.Add(pictListSave[pictListSave.Count - 1].pictureBox);
  167.                         if (pictListSave.Count <= 4)
  168.                         {
  169.                             pictListSave[pictListSave.Count - 1].pictureBox.Location = new Point(Convert.ToInt32((1 * (pictListSave.Count) + ((pictListSave.Count - 1) * 10.25)) * size), Convert.ToInt32(1 * size));
  170.                         }
  171.                         else
  172.                         {
  173.                             panel.ScrollControlIntoView(pictListSave[0].pictureBox);
  174.                             panel.Height = Convert.ToInt32(11 * size);
  175.                             pictListSave[pictListSave.Count - 1].pictureBox.Location = new Point(Convert.ToInt32((1 * (pictListSave.Count) + ((pictListSave.Count - 1) * 10.25)) * size), Convert.ToInt32(1 * size));
  176.                             panel.ScrollControlIntoView(pictListSave[pictListSave.Count - 1].pictureBox);
  177.                         }
  178.                         pictListSave[pictListSave.Count - 1].pictureBox.Width = Convert.ToInt32(9.75 * size);
  179.                         pictListSave[pictListSave.Count - 1].pictureBox.Height = Convert.ToInt32(7.25 * size);
  180.                         pictListSave[pictListSave.Count - 1].pictureBox.MouseClick += new MouseEventHandler(PictureView_MouseClick);
  181.                     }
  182.                     catch (SecurityException ex)
  183.                     {
  184.                         // The user lacks appropriate permissions to read files, discover paths, etc.
  185.                         MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
  186.                             "Error message: " + ex.Message + "\n\n" +
  187.                             "Details (send to Support):\n\n" + ex.StackTrace
  188.                         );
  189.                     }
  190.                     catch (Exception ex)
  191.                     {
  192.                         // Could not load the image - probably related to Windows file system permissions.
  193.                         MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
  194.                             + ". You may not have permission to read the file, or " +
  195.                             "it may be corrupt.\n\nReported error: " + ex.Message);
  196.                     }
  197.                 }
  198.                 if (pictListSave.Count <= 4)
  199.                 {
  200.                     for (int i = 0; i < pictListSave.Count; i++)
  201.                     {
  202.                         pictListSave[i].pictureElementShow();
  203.                         nThredRefr = pictListSave.Count - 1;
  204.                     }
  205.                 }
  206.                 else
  207.                 {
  208.                     for (int i = pictListSave.Count - 1; i > pictListSave.Count - 6; i--)
  209.                     {
  210.                         pictListSave[i].pictureElementShow();
  211.                         nThredRefr = pictListSave.Count - 5;
  212.                     }
  213.                     for (int i = pictListSave.Count - 6; i >= 0; i--)
  214.                     {
  215.                         pictListSave[i].pictureElementClear();
  216.                     }
  217.                 }
  218.                 GC.Collect();
  219.                 panel.Refresh();
  220.             }
  221.         }
  222.  
  223.         public void ShowGalery(int time, bool rand)
  224.         {
  225.             randGalery = rand;
  226.             pictureTimer.Interval = time;
  227.             if (pictureTimer.Enabled == false && panel.Controls != null)
  228.             {
  229.                 iNoRand = 0;
  230.                 pictureTimer.Start();
  231.                 pictureTimer.Enabled = true;
  232.             }
  233.             else
  234.             {
  235.                 pictureTimer.Stop();
  236.                 pictureTimer.Enabled = false;
  237.             }
  238.  
  239.         }
  240.  
  241.         private void GaleryEvent(object sender, EventArgs e)
  242.         {
  243.             if (randGalery == true)
  244.             {
  245.                 Random i = new Random(DateTime.Now.Millisecond);
  246.                 int j = i.Next(0, pictListSave.Count);
  247.                 DrawPicture(pictListSave[j].Name);
  248.             }
  249.             else
  250.             {
  251.                 if (iNoRand == pictListSave.Count)
  252.                 {
  253.                     iNoRand = 0;
  254.                 }
  255.                 DrawPicture(pictListSave[iNoRand].Name);
  256.                 iNoRand++;
  257.             }
  258.         }
  259.  
  260.         private void PictureView_SizeChanged(object sender, EventArgs e)
  261.         {
  262.             size = this.Width / 50;
  263.             if (size < 5)
  264.             {
  265.                 size = 5;
  266.             }
  267.             this.Width = Convert.ToInt32(50 * size);
  268.             this.Height = Convert.ToInt32(42 * size);
  269.             this.viewBox.Location = new Point(Convert.ToInt32(9 * size), Convert.ToInt32(2 * size));
  270.             this.viewBox.Width = Convert.ToInt32(32 * size);
  271.             this.viewBox.Height = Convert.ToInt32(24 * size);
  272.             this.label.Location = new Point(Convert.ToInt32(9 * size), Convert.ToInt32(26 * size));
  273.             this.label.Width = Convert.ToInt32(32 * size);
  274.             this.label.Height = Convert.ToInt32(1.5 * size);
  275.             this.panel.Location = new Point(Convert.ToInt32(2 * size), Convert.ToInt32(28 * size));
  276.             this.panel.Width = Convert.ToInt32(46 * size);
  277.             this.panel.Height = Convert.ToInt32(9 * size);
  278.         }
  279.  
  280.         private void DrawPicture(string name)
  281.         {
  282.             label.Text = System.IO.Path.GetFileName(name);
  283.             viewBox.Image = Image.FromFile(name);
  284.             viewBox.Refresh();
  285.         }
  286.  
  287.         private void PictureView_MouseClick(object sender, MouseEventArgs e)
  288.         {
  289.             for (int i = 0; i < pictListSave.Count; i++)
  290.             {
  291.                 if (pictListSave[i].pictureBox.Equals(sender))
  292.                 {
  293.                     if (pictureTimer.Enabled == true)
  294.                     {
  295.                         pictureTimer.Stop();
  296.                         pictureTimer.Enabled = false;
  297.                     }
  298.                     DrawPicture(pictListSave[i].Name);
  299.                     break;
  300.                 }
  301.             }
  302.         }
  303.  
  304.         //private void panel_Scroll(object sender, ScrollEventArgs e)
  305.         //{
  306.         //    for (int i = 0; i < pictListSave.Count; i++)
  307.         //    {
  308.         //        if ((pictListSave[i].pictureBox.Location.X <= panel.Width && pictListSave[i].pictureBox.Location.X > 0) || ((pictListSave[i].pictureBox.Location.X + pictListSave[i].pictureBox.Width) <= panel.Width && (pictListSave[i].pictureBox.Location.X + pictListSave[i].pictureBox.Width) > 0))
  309.         //        {
  310.         //            pictListSave[i].pictureElementShow();
  311.         //        }
  312.         //        else
  313.         //        {
  314.         //            pictListSave[i].pictureElementClear();
  315.         //        }
  316.  
  317.         //    }
  318.         //    panel.Refresh();
  319.         //    GC.Collect();
  320.         //}
  321.  
  322.  
  323.     }
  324. }