c#圖片自動播放程序

filp_v2系統,是練習c#控件的很好Demo案例。它包含了菜單、多窗口、圖片框、複選框、容器等控件。
功能如下:
1:通過定時器完成自動翻轉
2:設置毫秒值保存到xml數據中
3:文件夾對話框
4:文件對話框
5:自定義輸入文件或目錄

6:多個form之間數據共享


由於代碼比較多,下面貼出核心代碼

|---獲取所有可執行文件、初始化xml保存數據。

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

namespace flip_v2
{
	/** 圖片+Xml工具類*/	
	public class ImgTool
	{	
		/**
		 *
		 * 	傳遞一個完整路徑,返回一個ilst集合,0爲路徑(不含文件名)	1:播放的文件名(不包含路徑)
		 * 	如C:\Users\Public\Pictures\Sample Pictures\八仙花.jpg
		 * 		0:C:\Users\Public\Pictures\Sample Pictures
		 * 		1:八仙花.jpg
		 */
		public static string[] pathToList(string path){
			string[] array = new string[2];
			int last = path.LastIndexOf("\\");
			if(last!=-1){
				array[0]=(path.Substring(0,last));
				array[1]=(path.Substring(last+1));
			}
			
			return array;
		}
		
		/**把所有文件封裝到map中  key爲uint64  value=string*/
		public static Dictionary<UInt64,string> pathToMap(string path){
			Dictionary<UInt64,string> dict = new Dictionary<UInt64,string>();
			
			DirectoryInfo info = new DirectoryInfo(path);
			FileSystemInfo[] fileSystem = info.GetFileSystemInfos();
			
			UInt64 count = 1;
			//獲取所有子文件(不包含子文件夾),
			foreach(FileSystemInfo f in fileSystem){
				FileInfo file = f as FileInfo;
				if(file!=null && fileterEndName(file.Name))
					dict.Add(count++,f.FullName);
			}
			
			return dict;
		}
		//文件過濾器:png,jpg,gif
		public static bool fileterEndName(string endName){
			endName = endName.ToLower();
			bool b = false;
			string tag = ".png|.jpg|.gif";
			int last = endName.LastIndexOf(".");
			if(last!=-1){
				endName = endName.Substring(last);
				if(tag.Contains(endName))
					b= true;
			}
			return b;
		}
		
		/**操作xml:默認保存到c:\\property.xml*/
		public static void createXml(string path){
			//xml樣式 
			XmlWriterSettings settings = new XmlWriterSettings();
			settings.Indent = true;
			settings.NewLineOnAttributes = false;
			
			//本地創建一個新的xml
			XmlWriter write = XmlWriter.Create(path,settings);
			
			write.WriteStartDocument();
			write.WriteStartElement("property");
			
			write.WriteStartElement("time");
			write.WriteAttributeString("id","setTime");
			write.WriteValue("3000");
			write.WriteEndElement();
			
			write.WriteStartElement("localhostPath");
			write.WriteAttributeString("id","setInputBoxPath");
			write.WriteValue("");
			write.WriteEndElement();
			
			write.WriteEndElement();
			write.WriteEndDocument();
			
			//刷新緩衝區,並釋放資源
			write.Flush();
			write.Close();
		}
	}
}
|----目錄圖片的核心代碼

<pre code_snippet_id="237877" snippet_file_name="blog_20140315_2_3804652" name="code" class="csharp">		/*執行的事件*/
        public void theout(object source, EventArgs  e)
        {
        	Boolean b = checkTag.Checked;
			if(!b){
            	myTimer.Stop(); //停止定時器
        	}else{
	        	//執行獲取圖片
	        	string pageStr = showImg.Text;
				UInt64 pageNum  = UInt64.Parse(pageStr);
				pageNum = pageNum+1;
				
				try{
					showPage(pageNum);
				}catch(Exception){
					//循環
					showPage(1);
					//MessageBox.Show("已是未尾");
				}
			}
        }
		
		/**後退*/
		void BackOffClick(object sender, EventArgs e)
		{
			string pageStr = showImg.Text;
			UInt64 pageNum  = UInt64.Parse(pageStr);
			pageNum = pageNum-1;
			try{
				showPage(pageNum);
			}catch(Exception){
				MessageBox.Show("已是頂頁");
			}
			
		}
		
		/**前進*/
		void ForwardClick(object sender, EventArgs e)
		{
			string pageStr = showImg.Text;
			UInt64 pageNum  = UInt64.Parse(pageStr);
			pageNum = pageNum+1;
			try{
				showPage(pageNum);
			}catch(Exception){
				MessageBox.Show("已是未尾");
			}
		}
		
		public bool showPage(UInt64 pageNum)
		{
			if(dict==null || dict.Count==0){
				MessageBox.Show("抱歉,沒有找到可執行文件!");
				stopImg.ForeColor=Color.Red;
				startImg.ForeColor=Color.LimeGreen;
				
				/*還原*/
				Reduction();
				
				return false;
			}
			string showImgPath =  dict[pageNum];
			showImg.Image = Image.FromFile(showImgPath);
					
			//回顯信息
			string[] str = ImgTool.pathToList(showImgPath);
			Many.Text = "來自:"+str[0];
			Single.Text = "正在瀏覽【"+str[1]+"】";
			
			//回顯頁碼
			showImg.Text = Convert.ToString(pageNum);
			return true;
		}
</pre>

下面貼出程序執行圖片

發佈了56 篇原創文章 · 獲贊 16 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章