Увеличение изображения и перелистывания при клике мышкой
#39924476
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
Есть проект, есть в нем TreeView и FlowLayoutPanel. Выбираю папку и из папки динамически грузятся картинки из TreeView в FlowLayoutPanel. Как сделать так, чтобы я мог сделать клик мышкой из динамически созданного PictureBox и изображение увеличилось и можно было перелистывать?
Вот код:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199.
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CyberforumPhoto
{
public partial class Form1 : Form
{
/// <summary>
/// Базовый класс для создание узлов в элементе TreeView
/// </summary>
public class BaseNode : TreeNode
{
/// <summary>
/// Возвращает или задает значение. Может-ли данный узел загружать массив изображений.
/// </summary>
public bool IsloadArrayPicture { get; set; }
/// <summary>
/// Возвращает или задает значение. Может-ли данный узел загружать массив изображений.
/// </summary>
public int CountPicture { get; set; }
List<BaseNode> nodeList = new List<BaseNode>();
public IEnumerable<List<BaseNode>> GetChildNodes(int Count = 5)
{
return ToChunks(this.Nodes, Count);
}
private IEnumerable<List<BaseNode>> ToChunks(TreeNodeCollection items, int chunkSize)
{
List<BaseNode> chunk = new List<BaseNode>(chunkSize);
foreach (BaseNode item in items)
{
chunk.Add(item);
if (chunk.Count == chunkSize)
{
yield return chunk;
chunk = new List<BaseNode>(chunkSize);
}
}
if (chunk.Any())
yield return chunk;
}
public BaseNode(string Text) : base(Text) { }
}
public Form1()
{
InitializeComponent();
}
Dictionary<string, string> dirs = new Dictionary<string, string>();
public IEnumerable<FileInfo> GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
{
IEnumerable<FileInfo> files = dir.GetFiles("*.*", SearchOption.AllDirectories);
return files.Where(f => extensions.Contains(f.Extension));
}
DriveInfo[] driveInfo = DriveInfo.GetDrives();
DirectoryInfo directory;
BaseNode NodeDriver = null;
BaseNode NodeDirectory = null;
BaseNode NodePicture;
int stepProgress;
private void FindImageDriver()
{
directory = new DirectoryInfo("C:\\");
NodeDriver = new BaseNode(directory.Name);
var h = directory.GetDirectories().Where(x =>
!x.Attributes.ToString().Replace(" ", "").Split(',')[0].Contains("Hidden") &
!x.Attributes.ToString().Replace(" ", "").Split(',')[0].Contains("ReadOnly")).Where(x => x.Name != "Windows");
foreach (DirectoryInfo dir in h)
{
IEnumerable<FileInfo> fileInfos = GetFilesByExtensions(dir, ".jpg", ".png");
if (fileInfos.Count() != 0)
{
this.Invoke(new Action(() =>
{
progress.ProgressBar.Maximum = fileInfos.Count();
}));
NodeDirectory = new BaseNode(dir.Name);
NodeDirectory.ImageIndex = 0;
NodeDriver.Nodes.Add(NodeDirectory);
NodeDriver.ImageIndex = 3;
NodeDriver.Tag = false;
}
foreach (FileInfo file in fileInfos)
{
this.Invoke(new Action(() =>
{
progress.ProgressBar.Value = stepProgress++;
}));
NodePicture = new BaseNode(file.FullName);
NodePicture.Tag = false;
NodePicture.ImageIndex = file.Extension.Contains(".jpg") ? 1 : 2;
NodeDirectory.Nodes.Add(NodePicture);
NodeDirectory.IsloadArrayPicture = true;
}
stepProgress = 0;
}
this.Invoke(new Action(() =>
{
treeView1.Nodes.Add(NodeDriver);
}));
this.Invoke(new Action(() =>
{
lbStatus.Text = "";
progress.ProgressBar.Value = 0;
}));
}
private void Form1_LoadAsync(object sender, EventArgs e)
{
new Task(new Action(FindImageDriver)).Start();
}
public void ViewPhoto(List<BaseNode> baseNodes)
{
panel1.Controls.Clear();
foreach (BaseNode path in baseNodes)
{
int Top = 5;
PictureBox image = new PictureBox();
image.Image = Image.FromFile(path.Text);
image.SizeMode = PictureBoxSizeMode.StretchImage;
image.Width = 200;
image.Height = 160;
image.Margin = new Padding(0, ++Top, 0, 0);
panel1.Controls.Add(image);
}
}
IEnumerable<List<BaseNode>> array;
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
BaseNode node = (treeView1.SelectedNode as TreeNode) as BaseNode;
if (node.IsloadArrayPicture)
{
array = node.GetChildNodes(30);
ViewPhoto(array.FirstOrDefault());
panel1.VerticalScroll.Maximum = array.Count();
panel1.VerticalScroll.SmallChange = 1;
}
}
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
try
{
ViewPhoto(array.ToList()[e.NewValue]);
}
catch (Exception)
{
}
}
//
//
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void panel1_Click(object sender, EventArgs e)
{
}
}
}
|
|