c#遍历文件夹下所有图片生成缩略图,并覆盖原文件
展开全部
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
if (dir.Exists == false)
return;
Size sizeView = new Size(32, 32);
Bitmap tmpBmp = new Bitmap(sizeView.Width, sizeView.Height);
Graphics g = Graphics.FromImage(tmpBmp);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Rectangle destRect = new Rectangle(0, 0, sizeView.Width, sizeView.Height);
Rectangle srcRect;
string[] filetypeList = new string[] { "*.jpg", "*.jpeg", "*.png", "*.bmp" };
try
{
foreach (string filetype in filetypeList)
{
string[] filelist = Directory.GetFiles(dir.FullName, filetype);
System.Drawing.Imaging.ImageFormat format;
if (filetype.Equals(filetypeList[0]) || filetype.Equals(filetypeList[1]))
format = System.Drawing.Imaging.ImageFormat.Jpeg;
else if (filetype.Equals(filetypeList[2]))
format = System.Drawing.Imaging.ImageFormat.Png;
else if (filetype.Equals(filetypeList[3]))
format = System.Drawing.Imaging.ImageFormat.Bmp;
else
return;
foreach (string filename in filelist)
{
Image srcImg = Image.FromFile(filename);
g.Clear(Color.White);
srcRect = new Rectangle(0, 0, srcImg.Width, srcImg.Height);
g.DrawImage(srcImg, destRect, srcRect, GraphicsUnit.Pixel);
srcImg.Dispose();
tmpBmp.Save(filename);
}
}
MessageBox.Show("生成成功!");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
{
if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
if (dir.Exists == false)
return;
Size sizeView = new Size(32, 32);
Bitmap tmpBmp = new Bitmap(sizeView.Width, sizeView.Height);
Graphics g = Graphics.FromImage(tmpBmp);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Rectangle destRect = new Rectangle(0, 0, sizeView.Width, sizeView.Height);
Rectangle srcRect;
string[] filetypeList = new string[] { "*.jpg", "*.jpeg", "*.png", "*.bmp" };
try
{
foreach (string filetype in filetypeList)
{
string[] filelist = Directory.GetFiles(dir.FullName, filetype);
System.Drawing.Imaging.ImageFormat format;
if (filetype.Equals(filetypeList[0]) || filetype.Equals(filetypeList[1]))
format = System.Drawing.Imaging.ImageFormat.Jpeg;
else if (filetype.Equals(filetypeList[2]))
format = System.Drawing.Imaging.ImageFormat.Png;
else if (filetype.Equals(filetypeList[3]))
format = System.Drawing.Imaging.ImageFormat.Bmp;
else
return;
foreach (string filename in filelist)
{
Image srcImg = Image.FromFile(filename);
g.Clear(Color.White);
srcRect = new Rectangle(0, 0, srcImg.Width, srcImg.Height);
g.DrawImage(srcImg, destRect, srcRect, GraphicsUnit.Pixel);
srcImg.Dispose();
tmpBmp.Save(filename);
}
}
MessageBox.Show("生成成功!");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询