c#实现winform下显示批量缩略图形式的图片

作者&投稿:邲贴 (若有异议请与网页底部的电邮联系)
c#实现winform下显示批量缩略图形式的图片~

前两天刚给别人做了一个类似的
用lilstview显示缩略图,picturebox显示大图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private string[] files;
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
this.files = this.openFileDialog1.FileNames;
for(int i=0;i<files.Length;i++)
{
this.imageList1.Images.Add(Image.FromFile(this.files[i]));
this.listView1.Items.Add(this.files[i].Substring(this.files[i].LastIndexOf(@"\")+1), i);
}
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listView1.SelectedItems.Count > 0)
this.pictureBox1.ImageLocation = this.files[this.listView1.SelectedItems[0].Index];
}
}
}

给picturebox指定新图片前,要主动释放旧图片占用的内存
(vb代码)
If Not (Me.PictureBox1.Image Is Nothing) Then PictureBox1.Image.Dispose() PictureBox1.Image = NothingEnd IfC#
if ((this.PictureBox1.Image != null)) { PictureBox1.Image.Dispose(); PictureBox1.Image = null;}

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms.Design;

namespace Image
{
public class ImageConvert
{
public static void ShowThumbnail(string oldfile, string newfile, int h, int w)
{

System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

int oldh = img.Height;
int oldw = img.Width;

int newh,neww;

double h1 = oldh*1.0/h;
double w1 = oldw*1.0/w;

double f = (h1>w1)? h1:w1;

if(f < 1.0)
{
newh = oldh;
neww = oldw;
}
else
{
newh = (int)(oldh/f);
neww = (int)(oldw/f);
}

System.Drawing.Image myThumbnail = img.GetThumbnailImage(neww, newh, myCallback, IntPtr.Zero);

myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);

img.Dispose();
myThumbnail.Dispose();
}
private static bool ThumbnailCallback()
{
return false;
}
}

public class FolderDialog : FolderNameEditor
{
FolderNameEditor.FolderBrowser fDialog = new System.Windows.Forms.Design.FolderNameEditor.FolderBrowser();
public FolderDialog()
{
}
public DialogResult DisplayDialog()
{
return DisplayDialog("请选择一个文件夹");
}

public DialogResult DisplayDialog(string description)
{
fDialog.Description = description;
return fDialog.ShowDialog();
}
public string Path
{
get
{
return fDialog.DirectoryPath;
}
}
~FolderDialog()
{
fDialog.Dispose();
}
}

/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.LinkLabel linkLabel1;
private System.Windows.Forms.LinkLabel linkLabel2;
private System.Windows.Forms.TextBox path1;
private System.Windows.Forms.TextBox path2;
private System.Windows.Forms.TextBox width;
private System.Windows.Forms.TextBox height;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.LinkLabel linkLabel3;
private System.Windows.Forms.LinkLabel linkLabel4;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;
private System.Windows.Forms.ColumnHeader columnHeader6;

private ArrayList threads = new ArrayList();
private ArrayList images = new ArrayList();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.linkLabel2 = new System.Windows.Forms.LinkLabel();
this.path1 = new System.Windows.Forms.TextBox();
this.path2 = new System.Windows.Forms.TextBox();
this.width = new System.Windows.Forms.TextBox();
this.height = new System.Windows.Forms.TextBox();
this.listView1 = new System.Windows.Forms.ListView();
this.linkLabel3 = new System.Windows.Forms.LinkLabel();
this.linkLabel4 = new System.Windows.Forms.LinkLabel();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// linkLabel1
//
this.linkLabel1.Location = new System.Drawing.Point(456, 16);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(32, 16);
this.linkLabel1.TabIndex = 0;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "浏览";
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// linkLabel2
//
this.linkLabel2.Location = new System.Drawing.Point(456, 48);
this.linkLabel2.Name = "linkLabel2";
this.linkLabel2.Size = new System.Drawing.Size(32, 16);
this.linkLabel2.TabIndex = 1;
this.linkLabel2.TabStop = true;
this.linkLabel2.Text = "浏览";
this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);
//
// path1
//
this.path1.Location = new System.Drawing.Point(16, 16);
this.path1.Name = "path1";
this.path1.Size = new System.Drawing.Size(432, 21);
this.path1.TabIndex = 2;
this.path1.Text = "";
//
// path2
//
this.path2.Location = new System.Drawing.Point(16, 48);
this.path2.Name = "path2";
this.path2.Size = new System.Drawing.Size(432, 21);
this.path2.TabIndex = 3;
this.path2.Text = "";
//
// width
//
this.width.Location = new System.Drawing.Point(80, 80);
this.width.Name = "width";
this.width.Size = new System.Drawing.Size(72, 21);
this.width.TabIndex = 4;
this.width.Text = "";
//
// height
//
this.height.Location = new System.Drawing.Point(216, 80);
this.height.Name = "height";
this.height.Size = new System.Drawing.Size(72, 21);
this.height.TabIndex = 5;
this.height.Text = "";
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4,
this.columnHeader5,
this.columnHeader6});
this.listView1.Location = new System.Drawing.Point(16, 112);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(472, 152);
this.listView1.TabIndex = 6;
this.listView1.View = System.Windows.Forms.View.Details;
//
// linkLabel3
//
this.linkLabel3.Location = new System.Drawing.Point(304, 80);
this.linkLabel3.Name = "linkLabel3";
this.linkLabel3.Size = new System.Drawing.Size(32, 16);
this.linkLabel3.TabIndex = 7;
this.linkLabel3.TabStop = true;
this.linkLabel3.Text = "添加";
this.linkLabel3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel3_LinkClicked);
//
// linkLabel4
//
this.linkLabel4.Location = new System.Drawing.Point(376, 80);
this.linkLabel4.Name = "linkLabel4";
this.linkLabel4.Size = new System.Drawing.Size(32, 16);
this.linkLabel4.TabIndex = 8;
this.linkLabel4.TabStop = true;
this.linkLabel4.Text = "开始";
this.linkLabel4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel4_LinkClicked);
//
// columnHeader1
//
this.columnHeader1.Text = "源";
this.columnHeader1.Width = 122;
//
// columnHeader2
//
this.columnHeader2.Text = "目的";
this.columnHeader2.Width = 121;
//
// columnHeader3
//
this.columnHeader3.Text = "宽";
this.columnHeader3.Width = 49;
//
// columnHeader4
//
this.columnHeader4.Text = "高";
this.columnHeader4.Width = 42;
//
// columnHeader5
//
this.columnHeader5.Text = "文件数";
//
// columnHeader6
//
this.columnHeader6.Text = "完成数";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 294);
this.Controls.Add(this.linkLabel4);
this.Controls.Add(this.linkLabel3);
this.Controls.Add(this.listView1);
this.Controls.Add(this.height);
this.Controls.Add(this.width);
this.Controls.Add(this.path2);
this.Controls.Add(this.path1);
this.Controls.Add(this.linkLabel2);
this.Controls.Add(this.linkLabel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
FolderDialog dlg = new FolderDialog();
if(dlg.DisplayDialog()==DialogResult.OK)
path1.Text = dlg.Path;

}

private void linkLabel2_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
FolderDialog dlg = new FolderDialog();
if(dlg.DisplayDialog()==DialogResult.OK)
path2.Text = dlg.Path;
}

private void linkLabel3_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
string[] img = Directory.GetFiles(path1.Text, "*.jpg");

listView1.Items.Add(new ListViewItem(new string[]{path1.Text, path2.Text, width.Text, height.Text, img.Length.ToString(), "0"}));
}

private void linkLabel4_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
foreach(ListViewItem item in listView1.Items)
{
Thread t = new Thread(new ThreadStart(Process));
t.Start();
threads.Add(t);
}
}

private void Process()
{
int index = threads.IndexOf(Thread.CurrentThread);
ListViewItem item = listView1.Items[index];
string path1 = item.SubItems[0].Text;
string path2 = item.SubItems[1].Text;
int width = int.Parse(item.SubItems[2].Text);
int height = int.Parse(item.SubItems[3].Text);
int i = 0;
foreach(string file1 in Directory.GetFiles(path1, "*.jpg"))
{
i++;
string file2 = path2 + @"\" + Path.GetFileName(file1);
ImageConvert.ShowThumbnail(file1,file2, height, width);
lock(listView1)
{
listView1.Items[index].SubItems[5].Text = i.ToString();
}
}
}
}
}
Release版下载地址:http://asp2004.net/temp/Image.exe

ListView应该可以实现你的功能..
有五种显示的方式

用datalist控件也可以


澄海区15327566233: c#实现winform下显示批量缩略图形式的图片 -
濯叛舒降: 前两天刚给别人做了一个类似的 用lilstview显示缩略图,picturebox显示大图 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System....

澄海区15327566233: c#实现winform下显示批量缩略图形式的图片
濯叛舒降:ListView应该可以实现你的功能.. 有五种显示的方式

澄海区15327566233: C#的WinForm中如何实现图片缩略显示 -
濯叛舒降: 使用ListView和ImageList控件:ImageList的Images属性加载所需图片.ListView的LargeImageList,StaticImageList,SmallImageList分别绑定一个ImageList(也可绑定同一个ImageList).ListView的Items属性点开"...",自己看吧有你想要的....

澄海区15327566233: C# winform如何通过按钮事件实现图片的放大与缩小
濯叛舒降: 1、 如果是 直接再 界面上就呈现出来 图片的大小的改变的话,你直接更改 图片控件的高度和宽度就OK了 2、如果是想 等比例放大和缩小后并保存 图片的话,请查看 一下 bitmap 的应用吧 , 没有在自己电脑旁边 代码没发复制!

澄海区15327566233: C#如何获取系统显示的缩放比例 -
濯叛舒降: DPI is how scaling setting is implemented. Their relationship is:96 DPI = 100% scaling120 DPI = 125% scaling144 DPI = 150% scaling192 DPI = 200% scaling Read the DPI setting like so: // C# code to get dpi setting:float dpiX, dpiY; Graphics ...

澄海区15327566233: C# 无边框WinForm 点击任务栏图标时,如何实现放大缩小操作?
濯叛舒降: 可以在右上角放两个图片控件,一个作最小化、一个做最大化使用,然后在控件的click事件里用代码控制. 如果需要窗体大小可调整,需要捕获鼠标,判断按下、拖动、放开,根据鼠标移动距离对窗体的大小进行控制.有点麻烦.

澄海区15327566233: 请问C# winform使用pcitruebox控件查看图片如何实现放大,缩小功能? -
濯叛舒降: 设定 this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 然后通过按钮或者其他期望的操作方式,比如鼠标滚轮进行放大缩小 private void pictureBox1_MouseWheel(object sender, System.Windows.Forms....

澄海区15327566233: c#怎样用代码实现最小化窗口 -
濯叛舒降: 1.设置WinForm窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标. 3.添加窗体最小化事件(首先需要添加事件引用): this.SizeChanged += new System.EventHandler(this.Form1_...

澄海区15327566233: C# winform 放大缩小图像,我这里有缩小部分的代码,但是图像没有实现缩小功能 ,求高手指点,在线等 -
濯叛舒降: Graphics绘制完后 要调用g.Save() 使其生效

澄海区15327566233: visual c# 中windows窗体应用程序 实现图片的放大缩小? -
濯叛舒降: 很好解决的,我记得PictureBox有个属性是指示如何显示图片的,例如是平铺还是拉伸还是适合屏幕.你把这个属性设置为拉伸,然后就可以通过调节PictureBox的大小来控制图片的放大与缩小.在代码编辑器里输入个PictureBox然后按F12转到定义去找找那个属性

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网