日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

C# 系统应用之ListView实现简单图片浏览器

發(fā)布時(shí)間:2024/5/28 C# 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 系统应用之ListView实现简单图片浏览器 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

??????? 最近有同學(xué)問(wèn)我如何使用ListView加載圖片列表,前面在"C#系統(tǒng)應(yīng)用"中TreeView+ListView+ContextMenuStrip控件實(shí)現(xiàn)樹(shù)狀圖顯示磁盤目錄,并在ListView中顯示文件的詳細(xì)信息.這里準(zhǔn)備簡(jiǎn)單介紹下給同學(xué)講述的如何使用ListView+ImageList控件實(shí)現(xiàn)簡(jiǎn)單的圖片瀏覽器知識(shí).
?????? 第一步?設(shè)計(jì)界面框架如下圖所示,同時(shí)添加ImageList控件(不可見(jiàn))

?


??????? 注意:設(shè)置ListView控件的Anchor屬性為Top,Bottom,Right;設(shè)置PictureBox的Anchor屬性為上下左右.
???????第二步 使用OpenFileDialog控件打開(kāi)顯示圖片

//打開(kāi)圖片 private void button1_Click(object sender, EventArgs e) {//設(shè)置打開(kāi)文件控件OpenFileDialog openfile = new OpenFileDialog();openfile.Filter = "JPG(*.JPG;*.JPEG);gif文件(*.GIF);BMP文件(*.BMP);PNG文件(*.PNG)|*.jpg;*.jpeg;*.gif;*.bmp;*.png";openfile.FilterIndex = 1; //當(dāng)前選定索引openfile.RestoreDirectory = true;openfile.FileName = "";//對(duì)話框選擇確定按鈕if (openfile.ShowDialog() == DialogResult.OK){//FromFile從指定的文件創(chuàng)建ImagepictureBox1.Image = Image.FromFile(openfile.FileName);//圖片被拉伸或收縮適合pictureBox大小pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; } }

????????圖片顯示效果如下圖所示,需要注意的是在使用FromFile顯示圖片,可能圖片全屏顯示時(shí)會(huì)出現(xiàn)只出現(xiàn)部分圖片現(xiàn)象,我設(shè)置圖片為可拉伸或收縮StretchImage模式.


???????第三步?顯示圖片列表至ListView控件中
??????? 主要通過(guò)控件FolderBrowserDialog控件打開(kāi)文件夾,同時(shí)獲取文件夾的路徑;在通過(guò)GetFiles("*.jpg")函數(shù)獲取jpg格式圖片,并獲取文件夾中文件增加至ImageList中,設(shè)置ListView的View屬性格式為L(zhǎng)argeIcon大圖標(biāo)格式顯示.

//添加命名空間 using System.IO;?????????????????? //Directory目錄 using System.Diagnostics;????????? //Stopwatch顯示時(shí)間//定義變量 private string folderDirPath; //圖片文件夾地址 private string picDirPath = null; //圖片路徑 private List<string> imagePathList = new List<string>(); //獲取列表圖片路徑 private int index; //獲取選中列表圖片序號(hào)//ListView和imageList顯示圖片列表 private void button2_Click(object sender, EventArgs e) {try{//打開(kāi)選擇文件夾對(duì)話框FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();DialogResult result = folderBrowserDialog.ShowDialog();if (result == DialogResult.OK){//獲取用戶選擇的文件夾路徑this.folderDirPath = folderBrowserDialog.SelectedPath;//調(diào)用自定義函數(shù)顯示圖片列表至ListView控件ShowPicture();}else if (result == DialogResult.Cancel){MessageBox.Show("取消顯示圖片列表");}}catch (Exception msg){//報(bào)錯(cuò)提示 未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例throw msg;} }//顯示圖片列表至ListView控件 private void ShowPicture() {//提供一種方法測(cè)試運(yùn)行時(shí)間 開(kāi)始計(jì)算//參考資料:http://www.cnblogs.com/newstart/archive/2012/09/21/2696884.htmlStopwatch sw = new Stopwatch();sw.Start();//獲取目錄與子目錄DirectoryInfo dir = new DirectoryInfo(folderDirPath);//獲取當(dāng)前目錄JPG文件列表 GetFiles獲取指定目錄中文件的名稱(包括其路徑)FileInfo[] fileInfo = dir.GetFiles("*.jpg");//防止圖片失真//參考資料:http://blog.csdn.net/cdefg198/article/details/7821891 (博客中引用)this.imageList1.ColorDepth = ColorDepth.Depth32Bit;for (int i = 0; i < fileInfo.Length; i++){//獲取文件完整目錄picDirPath = fileInfo[i].FullName;//記錄圖片源路徑 雙擊顯示圖片時(shí)使用imagePathList.Add(picDirPath);//圖片加載到ImageList控件和imageList圖片列表this.imageList1.Images.Add(Image.FromFile(picDirPath));}//顯示文件列表this.listView1.Items.Clear();this.listView1.LargeImageList = this.imageList1;this.listView1.View = View.LargeIcon; //大圖標(biāo)顯示//imageList1.ImageSize = new Size(40, 40); //不能設(shè)置ImageList的圖像大小 屬性處更改//開(kāi)始綁定this.listView1.BeginUpdate();//增加圖片至ListView控件中for (int i = 0; i < imageList1.Images.Count; i++){ListViewItem lvi = new ListViewItem();lvi.ImageIndex = i;lvi.Text = "pic" + i;this.listView1.Items.Add(lvi);}this.listView1.EndUpdate();//顯示打開(kāi)圖片列表所需時(shí)間sw.Stop();long secords = sw.ElapsedMilliseconds; //毫秒單位label1.Text += '\n' + (Convert.ToDouble(secords) / 1000).ToString(); //轉(zhuǎn)換為秒 }

??????? 顯示結(jié)果如下圖所示:
?


????????需要注意的是:
??????? 1.使用ListView加載信息的幾個(gè)步驟:獲取文件夾路徑 -> DirectoryInfo獲取目錄 -> GetFiles獲取文件 -> Add圖片至ImageList -> Add圖片至ListView.
??????? 2.在設(shè)置ListView中圖片的大小時(shí),使用imageList1.ImageSize = new Size(40, 40)賦值失敗,我是通過(guò)修改ImageList1的ImageSize屬性為(64,64)實(shí)現(xiàn)的.
??????? 3.顯示ListView中圖片,通常會(huì)出現(xiàn)失真的情況,主要原因參考博客:ListView顯示圖片失真.
????????主要概括為ImageList里面圖片顏色失真和圖片大小失真,其中圖片顏色失真原因是Design-Time在VS.NET中添加圖片時(shí)沒(méi)有使用用戶指定的ColorDepth(如Depth32Bit),而用了ImageList.ColorDepth的默認(rèn)值(Depth8Bit).因此需要先設(shè)置圖片顏色深度,在再往ImageList中添加圖片,而圖片大小統(tǒng)一的都等于ImageList.ImageSize.
???????第四步 通過(guò)listView1_DoubleClick函數(shù)雙擊打開(kāi)圖片

????????在Form1.cs[設(shè)計(jì)]中ListView屬性頁(yè)為其添加DoubleClick雙擊事件,并通過(guò)Image.FromFile顯示圖片.

//增加雙擊ListView事件 顯示圖片至PictureBox private void listView1_DoubleClick(object sender, EventArgs e) {if (this.listView1.SelectedItems.Count == 0)return;//采用索引方式 imagePathList記錄圖片真實(shí)路徑index = this.listView1.SelectedItems[0].Index;//顯示圖片this.pictureBox1.Image = Image.FromFile(imagePathList[index]);//圖片被拉伸或收縮適合pictureBox大小pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; }

??????? 雙擊列表中不同圖片的顯示效果如下圖所示:

? ? ? ? 其中需要注意的是,我在列表中顯示圖片重命名為"pic+數(shù)字",同時(shí)定義變量記錄文件夾中圖片真實(shí)路徑與其一一對(duì)應(yīng).private List<string> imagePathList = new List<string>().這里使用index顯示對(duì)應(yīng)圖片即可,同樣顯示上一張\下一張相同.
???????第五步 顯示上一張\下一張

//顯示上一張圖片 private void button3_Click(object sender, EventArgs e) {if (pictureBox1.Image != null){if (index > 0){index--;//顯示圖片this.pictureBox1.Image = Image.FromFile(imagePathList[index]);pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; }else if (index == 0){index = imagePathList.Count;index--;//顯示圖片this.pictureBox1.Image = Image.FromFile(imagePathList[index]);pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; }} } //顯示下一張圖片 private void button4_Click(object sender, EventArgs e) {if (pictureBox1.Image != null){if (index == imagePathList.Count - 1) //最后一張圖片{index = 0;//顯示圖片this.pictureBox1.Image = Image.FromFile(imagePathList[index]);pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;}else{index++;//顯示圖片this.pictureBox1.Image = Image.FromFile(imagePathList[index]);pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; }} }

??????? 文章寫(xiě)到此處基本內(nèi)容完成,內(nèi)容比較簡(jiǎn)單,但也構(gòu)成了一個(gè)完整的圖片瀏覽器.同時(shí)很多時(shí)候我們需要上傳縮略圖,可以調(diào)用下面函數(shù)(在線筆記):

//添加命名空間 using System.Drawing.Drawing2D;??? //CompositingQuality.HighQuality using System.Drawing.Imaging;????? //EncoderParameter/// <SUMMARY> /// 圖片無(wú)損縮放 自定義函數(shù)生成縮略圖 /// </SUMMARY> /// <PARAM name="sourceFile">圖片源路徑</PARAM> /// <PARAM name="destFile">縮放后圖片輸出路徑</PARAM> /// <PARAM name="destHeight">縮放后圖片高度</PARAM> /// <PARAM name="destWidth">縮放后圖片寬度</PARAM> /// <RETURNS></RETURNS> public static bool GetThumbnail(string sourceFile, string destFile, int destHeight, int destWidth) {System.Drawing.Image imgSource = System.Drawing.Image.FromFile(sourceFile);System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;int sW = 0, sH = 0;// 按比例縮放int sWidth = imgSource.Width;int sHeight = imgSource.Height;if (sHeight > destHeight || sWidth > destWidth){if ((sWidth * destHeight) > (sHeight * destWidth)){sW = destWidth;sH = (destWidth * sHeight) / sWidth;}else{sH = destHeight;sW = (sWidth * destHeight) / sHeight;}}else{sW = sWidth;sH = sHeight;}//新建一個(gè)bmp圖片 Bitmap outBmp = new Bitmap(destWidth, destHeight);//新建一個(gè)畫(huà)板Graphics g = Graphics.FromImage(outBmp);//清空畫(huà)布并以透明背景色填充 Color.Black黑色填充g.Clear(System.Drawing.Color.Transparent);//設(shè)置畫(huà)布的描繪質(zhì)量g.CompositingQuality = CompositingQuality.HighQuality;//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度g.SmoothingMode = SmoothingMode.HighQuality;//設(shè)置高質(zhì)量插值法g.InterpolationMode = InterpolationMode.HighQualityBicubic;//在指定位置并且按指定大小繪制原圖片的指定部分g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);g.Dispose();//以下代碼為保存圖片時(shí) 設(shè)置壓縮質(zhì)量EncoderParameters encoderParams = new EncoderParameters();long[] quality = new long[1];quality[0] = 100;EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);encoderParams.Param[0] = encoderParam;try{//獲得包含有關(guān)內(nèi)置圖像編碼解碼器的信息的ImageCodecInfo對(duì)象ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();ImageCodecInfo jpegICI = null;for (int x = 0; x < arrayICI.Length; x++){if (arrayICI[x].FormatDescription.Equals("JPEG")){jpegICI = arrayICI[x]; //設(shè)置JPEG編碼break;}}//保存為JPG格式圖片if (jpegICI != null){outBmp.Save(destFile, jpegICI, encoderParams);}else{outBmp.Save(destFile, thisFormat);}return true;}catch(Exception e){throw e;}finally{imgSource.Dispose();outBmp.Dispose();} }

????????總結(jié):本文主要是根據(jù)給同學(xué)講解ListView控件顯示圖片寫(xiě)的一篇文章,同時(shí)存在一個(gè)缺點(diǎn)圖片可能被扯拉變形,而且代碼中打開(kāi)ListView圖片時(shí)有個(gè)"打開(kāi)時(shí)間",主要是通過(guò)Stopwatch記錄批量打開(kāi)圖片所需時(shí)間,如果打開(kāi)大量圖片時(shí)我希望使用并行的方法實(shí)現(xiàn),與其進(jìn)行時(shí)間對(duì)比.同時(shí)如果對(duì)圖片處理感興趣的同學(xué)(C++通過(guò)Bitmap打開(kāi)變換)自己可以去研究.我希望的顯示效果想Google Picasa一樣快速批量顯示(研究ing).
????????下載地址:http://download.csdn.net/detail/eastmount/8021077
????????最后希望文章對(duì)大家有所幫助,如果有錯(cuò)誤或不足之處,請(qǐng)海涵~
????????(By:Eastmount 2014-10-10 中午13點(diǎn)?原創(chuàng)CSDN?http://blog.csdn.net/eastmount/)

總結(jié)

以上是生活随笔為你收集整理的C# 系统应用之ListView实现简单图片浏览器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。