標籤:style blog http io ar os sp for 檔案
新增拖拽功能 如果出現看不見的情況明天就好了,好了我們步入正題,作為一個吊絲,如果你用你女朋友/女神的照片做了個圖片瀏覽器,那麼你每次用來看圖的時候第一張看的都是她了,當看到第二張你想看見的圖時,是不是有個鮮明的對比呢,是落差還是什麼的,就因人而異咯~~又開始想入非非了?哈哈,不扯淡了,繼續:4步走~~~ 軟體下載(.NET 4.0 架構)http://pan.baidu.com/s/1i387wJ7 【源碼看下面】 逆天圖片查看器1.0.1 下載 http://pan.baidu.com/s/1o6Hlrx4 【新增拖拽功能】 一、設計圖紙:
二、程式碼分析:(趕時間寫的,寫了快50分鐘了,如果有解釋不詳細的可以問)
1、開啟指定圖片 OpenFileDiaog控制項,先設定過濾器,然後再開啟圖片,顯示在picturebox上 OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "jpg映像(*.jpg)|*.jpg|png映像(*.png)|*.png|bmp映像(*.bmp)|*.bmp|所有格式(*.*)|*.*"; openFile.Title = "逆天只允許你開啟一張圖片"; if (openFile.ShowDialog() == DialogResult.OK) { string filePath = openFile.FileName; ShowPhoto(filePath, PictureBoxSizeMode.Zoom);//調用顯示圖片的方法---方法看下面 int lastIndex = filePath.LastIndexOf(@"\"); paths = Directory.GetFiles(filePath.Substring(0, lastIndex));//真正獲得該目錄下所有檔案的路徑 } 2、越界智能提醒 放個lable上面,不用就visible=false,用就設定為true,再設定下按鈕自動隱藏顯現 private void MainForm_Load(object sender, EventArgs e) { lbl1.Visible = false;//智能提示預設不可見 } #region 按鈕顯示和隱藏 public void VisibleFalse() { pbtnLeft.Visible = false; pbtnRight.Visible = false; } public void VisibleTrue() { pbtnLeft.Visible = true; pbtnRight.Visible = true; } private void pbtnLeft_MouseLeave(object sender, EventArgs e) { VisibleFalse(); } private void pbtnRight_MouseLeave(object sender, EventArgs e) { VisibleFalse(); } private void pictureBox1_MouseLeave(object sender, EventArgs e) { VisibleTrue(); } #endregion 3、置頂顯示圖片 置頂是TopMost,顯示圖片因為非常常用就封裝一個方法 // 置頂 if (topmost1.Checked == false) { this.TopMost = false; topmost1.Text = " 窗 體 置 頂"; } else { this.TopMost = true; topmost1.Text = " 取 消 置 頂"; } //顯示圖片 public void ShowPhoto(string filePath, PictureBoxSizeMode mode) //PictureBoxSizeMode 枚舉類型(以什麼方式顯示) { pictureBox1.ImageLocation = filePath; pictureBox1.SizeMode = mode; } 4、鍵盤控制切換 這個放在MainForm_KeyDown事件中,如果你想放在picturebox中那記得聚焦一下 private void MainForm_KeyDown(object sender, KeyEventArgs e)//觸發按鍵時 { if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up)//上一張 { GetLeft(); } if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Down)//下一張 { GetRight(); } if (e.Control == true && e.KeyCode == Keys.S) { SavePhoto(); } }
5、投影片的模式 思路就是Timer控制項+Random,產生隨機數組(下標),根據這個隨機數來顯示,Timer控制項,每1秒調用一次下一張的方法【??注意Timer的啟用和關閉,不施放資源會越來越卡的!!!】 //順序播放________________________________________________________________________________________ if (sequence.Checked == true) { timer2.Enabled = true; timer2.Start(); sequence.Text = " 取 消 播 放"; } else { sequence.Text = " 順 序 播 放"; timer2.Stop(); } private void timer2_Tick(object sender, EventArgs e)//順序播放{ GetRight();} public void GetRight() //下一張{ try { i++; if (i >= paths.Length) { i = 0; lbl1.Visible = true; timer1.Enabled = true;//節約資源 timer1.Start(); } ShowPhoto(paths[i], PictureBoxSizeMode.Zoom); } catch { timer2.Stop(); sequence.Text = " 順 序 播 放"; MessageBox.Show("親,你還沒右鍵開啟圖片呢!", "逆天友情提醒"); }} //逆序播放________________________________________________________________________________________ try { if (random.Checked == true) { timer3.Enabled = true; timer3.Start(); random.Text = " 取 消 播 放"; } else { timer3.Stop(); random.Text = " 隨 機 播 放"; } } catch { MessageBox.Show("親,你確定你開啟了圖片?", "逆天友情提醒"); } private void timer3_Tick(object sender, EventArgs e)//全部隨機播放 { try { Random r = new Random(); int n = r.Next(0, paths.Length); pictureBox1.ImageLocation = paths[n]; } catch { timer3.Stop(); random.Text = " 隨 機 播 放"; MessageBox.Show("親,你還沒開啟圖片呢!"); } } 6、圖片可以另存 利用了savaFile控制項,設定過濾器,擷取儲存後的圖片名字,給image建立一個對象用來接收pictureBox上的圖片,image.sava()就可以了[如果你想儲存旋轉之後的圖片就直接右擊儲存或者Ctrl+S] SaveFileDialog savaFile = new SaveFileDialog(); savaFile.Filter = "jpg映像(*.jpg)|*.jpg|png映像(*.png)|*.png|bmp映像(*.bmp)|*.bmp|所有格式(*.*)|*.*"; //過濾器 savaFile.Title = "很喜歡這張圖片吧~"; if (savaFile.ShowDialog() == DialogResult.OK) { string name = savaFile.FileName; Image image = pictureBox1.Image; image.Save(name); } 7、圖片全螢幕顯示 思路是這樣的,把表單邊框樣式設定為None,然後讓表單最大化 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; 8、圖片旋轉功能 系統內建Image.RotateFlip(RotateFlipType)方法,用完後記得pictureBox1.Refresh()一下 //逆時針轉 pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipXY); pictureBox1.Refresh(); //順時針轉 pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone); pictureBox1.Refresh(); 9、淺淺的小說明 還有圖片刪除跟旋轉後儲存旋轉後的映像就下次再說吧 本來準備弄一個靠邊隱藏之類的最佳化的,時間問題就下次弄吧(8期以後) 10、異常一定到位 一個程式大Bug不能有! 不是程式沒Bug而是你沒找到Bug! 開放過程中多注意點,把每個if else都考慮到,執行個體化對象時候把未將對象執行個體化考慮進去,可以省很多事情 很多枚舉類型的東西不需要都記得,你記不得時候打個【空格】他就自動出來了11、★新增:拖拽功能 表單載入時候加這一句話:this.pictureBox1.AllowDrop = true; PictureBox兩個事件:DragEnter(拖拽時發生) DragDrop(拖拽完成時發生) #region PictureBox支援拖拽 private void pictureBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Link; } else { e.Effect = DragDropEffects.None; } } private void pictureBox1_DragDrop(object sender, DragEventArgs e) { string fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); ShowPhoto(fileName, PictureBoxSizeMode.Zoom);//調用顯示圖片的方法 int lastIndex = fileName.LastIndexOf(@"\"); paths = Directory.GetFiles(fileName.Substring(0, lastIndex));//真正獲得該目錄下所有檔案的路徑 this.pictureBox1.Image = Image.FromFile(fileName); }#endregion 三、源碼分享:http://pan.baidu.com/s/13vSXC
四、視頻共用: 等~~~ 本期推薦: 23種設計模式(PDF+源碼) {補充} 下一期:點名系列的小系統(簡單)
小白C#表單系列-03.圖片查看器