- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Runtime.Remoting.Messaging;
- using System.Text;
- using System.Windows.Forms;
- using System.Security;
- using System.Timers;
- using System.Threading;
- namespace PictureView
- {
- public delegate void AsyncMethodCaller();
- [Description("PictureView для просмотра изображений")]
- [ToolboxItem(true)]
- [ToolboxBitmap(typeof(PictureView), "PictureView.bmp")]
- public partial class PictureView : UserControl
- {
- private float size;
- private List<pictureList> pictListSave;
- private bool randGalery;
- private int iNoRand;
- System.Timers.Timer pictureTimer = new System.Timers.Timer();
- private Thread pictRefreshThreadL;
- //private Thread pictRefreshThreadR;
- private int nThredRefr;
- public PictureView()
- {
- InitializeComponent();
- size = this.Width / 50;
- pictListSave = new List<pictureList>();
- CheckForIllegalCrossThreadCalls = false;
- randGalery = false;
- pictRefreshThreadL = new Thread(PictRefreshL);
- //pictRefreshThreadR = new Thread(PictRefreshR);
- pictureTimer.Elapsed += new ElapsedEventHandler(GaleryEvent);
- pictureTimer.Interval = 1000;
- pictureTimer.Enabled = false;
- nThredRefr = 0;
- }
- //private void PictRefresh(object sender, EventArgs e)
- public void PictRefreshL()
- {
- //while (true)
- //{
- // if (pictListSave.Count != 0 && nThredRefr >= 0 && nThredRefr < pictListSave.Count)
- // {
- // for (int i = nThredRefr; i >= 0; i--)
- // {
- // 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))
- // {
- // pictListSave[i].pictureElementShow();
- // nThredRefr = i - 1;
- // }
- // else
- // {
- // pictListSave[i].pictureElementClear();
- // }
- // }
- // panel.Refresh();
- // GC.Collect();
- // }
- //}
- while(true)
- {
- for (int i = 0; i < pictListSave.Count; i++)
- {
- 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))
- {
- pictListSave[i].pictureElementShow();
- }
- else
- {
- pictListSave[i].pictureElementClear();
- }
- }
- panel.Refresh();
- GC.Collect();
- }
- }
- //public void PictRefreshR()
- //{
- // while (true)
- // {
- // if (pictListSave.Count != 0 && nThredRefr >= 0 && nThredRefr < pictListSave.Count)
- // {
- // for (int i = nThredRefr; i < pictListSave.Count; i++)
- // {
- // 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))
- // {
- // pictListSave[i].pictureElementShow();
- // nThredRefr = i - 1;
- // }
- // else
- // {
- // pictListSave[i].pictureElementClear();
- // }
- // }
- // panel.Refresh();
- // GC.Collect();
- // }
- // }
- //}
- public void Start()
- {
- pictRefreshThreadL.Start();
- //pictRefreshThreadR.Start();
- }
- public void Abort()
- {
- pictRefreshThreadL.Abort();
- //pictRefreshThreadR.Abort();
- }
- public void Clear()
- {
- nThredRefr = -1;
- if (pictureTimer.Enabled == true)
- {
- pictureTimer.Stop();
- pictureTimer.Enabled = false;
- }
- pictListSave.Clear();
- panel.Controls.Clear();
- panel.Refresh();
- label.Text = null;
- viewBox.Image = null;
- viewBox.Invalidate();
- GC.Collect();
- }
- public void Add()
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Multiselect = true;
- openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
- openFileDialog.Filter = "Files (*.jpg,*.png,*.bmp)|*.jpg|*.png|*.bmp";
- if (openFileDialog.ShowDialog(this) == DialogResult.OK)
- {
- foreach (String file in openFileDialog.FileNames)
- {
- try
- {
- pictListSave.Add(new pictureList(file, pictListSave.Count + 1));
- panel.Controls.Add(pictListSave[pictListSave.Count - 1].pictureBox);
- if (pictListSave.Count <= 4)
- {
- pictListSave[pictListSave.Count - 1].pictureBox.Location = new Point(Convert.ToInt32((1 * (pictListSave.Count) + ((pictListSave.Count - 1) * 10.25)) * size), Convert.ToInt32(1 * size));
- }
- else
- {
- panel.ScrollControlIntoView(pictListSave[0].pictureBox);
- panel.Height = Convert.ToInt32(11 * size);
- pictListSave[pictListSave.Count - 1].pictureBox.Location = new Point(Convert.ToInt32((1 * (pictListSave.Count) + ((pictListSave.Count - 1) * 10.25)) * size), Convert.ToInt32(1 * size));
- panel.ScrollControlIntoView(pictListSave[pictListSave.Count - 1].pictureBox);
- }
- pictListSave[pictListSave.Count - 1].pictureBox.Width = Convert.ToInt32(9.75 * size);
- pictListSave[pictListSave.Count - 1].pictureBox.Height = Convert.ToInt32(7.25 * size);
- pictListSave[pictListSave.Count - 1].pictureBox.MouseClick += new MouseEventHandler(PictureView_MouseClick);
- }
- catch (SecurityException ex)
- {
- // The user lacks appropriate permissions to read files, discover paths, etc.
- MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
- "Error message: " + ex.Message + "\n\n" +
- "Details (send to Support):\n\n" + ex.StackTrace
- );
- }
- catch (Exception ex)
- {
- // Could not load the image - probably related to Windows file system permissions.
- MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
- + ". You may not have permission to read the file, or " +
- "it may be corrupt.\n\nReported error: " + ex.Message);
- }
- }
- if (pictListSave.Count <= 4)
- {
- for (int i = 0; i < pictListSave.Count; i++)
- {
- pictListSave[i].pictureElementShow();
- nThredRefr = pictListSave.Count - 1;
- }
- }
- else
- {
- for (int i = pictListSave.Count - 1; i > pictListSave.Count - 6; i--)
- {
- pictListSave[i].pictureElementShow();
- nThredRefr = pictListSave.Count - 5;
- }
- for (int i = pictListSave.Count - 6; i >= 0; i--)
- {
- pictListSave[i].pictureElementClear();
- }
- }
- GC.Collect();
- panel.Refresh();
- }
- }
- public void ShowGalery(int time, bool rand)
- {
- randGalery = rand;
- pictureTimer.Interval = time;
- if (pictureTimer.Enabled == false && panel.Controls != null)
- {
- iNoRand = 0;
- pictureTimer.Start();
- pictureTimer.Enabled = true;
- }
- else
- {
- pictureTimer.Stop();
- pictureTimer.Enabled = false;
- }
- }
- private void GaleryEvent(object sender, EventArgs e)
- {
- if (randGalery == true)
- {
- Random i = new Random(DateTime.Now.Millisecond);
- int j = i.Next(0, pictListSave.Count);
- DrawPicture(pictListSave[j].Name);
- }
- else
- {
- if (iNoRand == pictListSave.Count)
- {
- iNoRand = 0;
- }
- DrawPicture(pictListSave[iNoRand].Name);
- iNoRand++;
- }
- }
- private void PictureView_SizeChanged(object sender, EventArgs e)
- {
- size = this.Width / 50;
- if (size < 5)
- {
- size = 5;
- }
- this.Width = Convert.ToInt32(50 * size);
- this.Height = Convert.ToInt32(42 * size);
- this.viewBox.Location = new Point(Convert.ToInt32(9 * size), Convert.ToInt32(2 * size));
- this.viewBox.Width = Convert.ToInt32(32 * size);
- this.viewBox.Height = Convert.ToInt32(24 * size);
- this.label.Location = new Point(Convert.ToInt32(9 * size), Convert.ToInt32(26 * size));
- this.label.Width = Convert.ToInt32(32 * size);
- this.label.Height = Convert.ToInt32(1.5 * size);
- this.panel.Location = new Point(Convert.ToInt32(2 * size), Convert.ToInt32(28 * size));
- this.panel.Width = Convert.ToInt32(46 * size);
- this.panel.Height = Convert.ToInt32(9 * size);
- }
- private void DrawPicture(string name)
- {
- label.Text = System.IO.Path.GetFileName(name);
- viewBox.Image = Image.FromFile(name);
- viewBox.Refresh();
- }
- private void PictureView_MouseClick(object sender, MouseEventArgs e)
- {
- for (int i = 0; i < pictListSave.Count; i++)
- {
- if (pictListSave[i].pictureBox.Equals(sender))
- {
- if (pictureTimer.Enabled == true)
- {
- pictureTimer.Stop();
- pictureTimer.Enabled = false;
- }
- DrawPicture(pictListSave[i].Name);
- break;
- }
- }
- }
- //private void panel_Scroll(object sender, ScrollEventArgs e)
- //{
- // for (int i = 0; i < pictListSave.Count; i++)
- // {
- // 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))
- // {
- // pictListSave[i].pictureElementShow();
- // }
- // else
- // {
- // pictListSave[i].pictureElementClear();
- // }
- // }
- // panel.Refresh();
- // GC.Collect();
- //}
- }
- }