在网上找的一个批量处理图片缩放的方法。进行图片的批量处理。发现处理的张数最多只能一起处理2000多张,电脑就报内存不足了。下面是主要方法,求大神指导。
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo cuurDir = new DirectoryInfo(currentDirPath);
FileInfo[] files = cuurDir.GetFiles(); //返回当前目录的文件列表
StringBuilder builder = new StringBuilder("<ul>");
Parallel.ForEach(files, file =>
{
string oldFileName = file.FullName;
string newFileName = GetSaveDirPath("D:\\VSTS\\JuFine\\03_Code\\DataFeed" + "\\i801") + "\\" + file.Name;
Bitmap srcBitmap = null;
Bitmap destBitmap = null;
Graphics graphic = null;
try
{
int width = Convert.ToInt32(TxtWidth.Text);
int height = Convert.ToInt32(TxtHeight.Text);
width = width == 0 ? 100 : width;
height = height == 0 ? 100 : height; GC.Collect(); GC.WaitForPendingFinalizers();
srcBitmap = new Bitmap(file.FullName); //加载原图
destBitmap = new Bitmap(width, height);
RectangleF srcRec = new RectangleF(0, 0, srcBitmap.Width, srcBitmap.Height);
RectangleF destRec = new RectangleF(0, 0, width, height);
graphic = Graphics.FromImage(destBitmap);
graphic.DrawImage(srcBitmap, destRec, srcRec, GraphicsUnit.Pixel); destBitmap.Save(newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);//保存较大Jpeg,如1280*1024图片时,比较消耗内存哦。
if (graphic != null) { graphic.Dispose(); graphic = null; }
if (destBitmap != null) { destBitmap.Dispose(); destBitmap = null; }
if (srcBitmap != null) { srcBitmap.Dispose(); srcBitmap = null; }
GC.Collect(); GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
if (graphic != null) { graphic.Dispose(); graphic = null; }
if (destBitmap != null) { destBitmap.Dispose(); destBitmap = null; }
if (srcBitmap != null) { srcBitmap.Dispose(); srcBitmap = null; }
GC.Collect(); GC.WaitForPendingFinalizers();
MessageBox.Show(ex.Message);
}
finally
{
FileInfo newFile = new FileInfo(newFileName);
builder.Append(string.Format(@"<li><img style='width:150px;height:200px;' alt='{0}' src='{1}' /><p>大小:{2}k</p></li>", newFile.Name, newFile.FullName, newFile.Length)).Append(Environment.NewLine);
} });
builder.Append("</ul>");
string template = TemplateHelper.ReadTemplate(TemplateHelper.GetTemplateDirPath(), "template.htm");
List<MatchItem> itemList = new List<MatchItem>();
itemList.Add(new MatchItem("$(imglist)", builder.ToString()));
webBrowser1.DocumentText = TemplateHelper.AnalyzeTemplate(itemList, template);
LblResult.Text = string.Format("共计{0}张图片", files.Length);
MessageBox.Show("请在桌面images文件夹下查看!", "提示信息");
}c#图片处理
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo cuurDir = new DirectoryInfo(currentDirPath);
FileInfo[] files = cuurDir.GetFiles(); //返回当前目录的文件列表
StringBuilder builder = new StringBuilder("<ul>");
Parallel.ForEach(files, file =>
{
string oldFileName = file.FullName;
string newFileName = GetSaveDirPath("D:\\VSTS\\JuFine\\03_Code\\DataFeed" + "\\i801") + "\\" + file.Name;
Bitmap srcBitmap = null;
Bitmap destBitmap = null;
Graphics graphic = null;
try
{
int width = Convert.ToInt32(TxtWidth.Text);
int height = Convert.ToInt32(TxtHeight.Text);
width = width == 0 ? 100 : width;
height = height == 0 ? 100 : height; GC.Collect(); GC.WaitForPendingFinalizers();
srcBitmap = new Bitmap(file.FullName); //加载原图
destBitmap = new Bitmap(width, height);
RectangleF srcRec = new RectangleF(0, 0, srcBitmap.Width, srcBitmap.Height);
RectangleF destRec = new RectangleF(0, 0, width, height);
graphic = Graphics.FromImage(destBitmap);
graphic.DrawImage(srcBitmap, destRec, srcRec, GraphicsUnit.Pixel); destBitmap.Save(newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);//保存较大Jpeg,如1280*1024图片时,比较消耗内存哦。
if (graphic != null) { graphic.Dispose(); graphic = null; }
if (destBitmap != null) { destBitmap.Dispose(); destBitmap = null; }
if (srcBitmap != null) { srcBitmap.Dispose(); srcBitmap = null; }
GC.Collect(); GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
if (graphic != null) { graphic.Dispose(); graphic = null; }
if (destBitmap != null) { destBitmap.Dispose(); destBitmap = null; }
if (srcBitmap != null) { srcBitmap.Dispose(); srcBitmap = null; }
GC.Collect(); GC.WaitForPendingFinalizers();
MessageBox.Show(ex.Message);
}
finally
{
FileInfo newFile = new FileInfo(newFileName);
builder.Append(string.Format(@"<li><img style='width:150px;height:200px;' alt='{0}' src='{1}' /><p>大小:{2}k</p></li>", newFile.Name, newFile.FullName, newFile.Length)).Append(Environment.NewLine);
} });
builder.Append("</ul>");
string template = TemplateHelper.ReadTemplate(TemplateHelper.GetTemplateDirPath(), "template.htm");
List<MatchItem> itemList = new List<MatchItem>();
itemList.Add(new MatchItem("$(imglist)", builder.ToString()));
webBrowser1.DocumentText = TemplateHelper.AnalyzeTemplate(itemList, template);
LblResult.Text = string.Format("共计{0}张图片", files.Length);
MessageBox.Show("请在桌面images文件夹下查看!", "提示信息");
}c#图片处理
解决方案 »
- 在datagridview中添加了一个DataGridViewTextBoxColumn,我想取出单元格的值,怎么做?详见帖子
- 【在线等。。。】Silverlight项目发布后操作反应慢和菜单无响应问题
- 俄语显示问题,系统自带的写字板显示正常,其他软件显示都不正常
- 求逻辑组合方法????
- 求一个函数 把日期转换成16进制(正则函数都行)
- [求助]程序退出报错
- 关于在分层架构下,Web Form和Window Form如何共享会话状态。 系统被设计为以下几个层次: 表现层。 UI components --- WebForm 或者Wind
- 网络数据 过滤
- 窗体的mouse事件被控件遮挡了,怎么办?
- vs2010旗舰版打开复杂的窗体设计器时显示内存不足
- 多线程端口扫描(求指点优化)
- C# 窗体怎么回传数据
{
Process A = Process.GetCurrentProcess();
A.MaxWorkingSet = Process.GetCurrentProcess().MaxWorkingSet;
A.Dispose();
}
if (destBitmap != null) { destBitmap.Dispose(); destBitmap = null; }
if (srcBitmap != null) { srcBitmap.Dispose(); srcBitmap = null; }
GC.Collect(); GC.WaitForPendingFinalizers();
这部分代码不也是释放资源的么?为什么还是会出现内存不足的那种情况
支持浏览器:IE6,IE7,IE8,IE8(x64),IE9(x64),Firefox,Chrome,360安全浏览器,360极速浏览器,Maxthon1.x,Maxthon2.x,Maxthon3.x,QQ浏览器
主界面图片列表添加图片编辑图片:提示信息:整合代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Web图片批量上传控件演示页面</title>
<script type="text/javascript" src="ImageUploader/ImageUploader.js" charset="utf-8"></script>
</head>
<body>
<div><a href="asp.net/images.aspx">查看上传的图片</a></div>
<div id="msg"></div>
<script language="javascript" type="text/javascript">
var imgUploader = new ImageUploader();
imgUploader.Config["PostUrl"] = "http://localhost/php/upload.php"; window.onload = function()
{
imgUploader.Init();
}
</script>
</body>
</html>
IE(x86)平台示例下载:
cab安装包
cab安装包(x64)
ASP示例
ASP.NET示例
JSP示例
PHP示例IE(x86),IE(x64),Firefox,Chrome平台示例下载:
xpi安装包
crx安装包
exe安装包
ASP示例
ASP.NET示例
JSP示例
PHP示例IE(x86),IE(x64),Firefox,Chrome企业版示例下载:
cab安装包
xpi安装包
crx安装包
exe安装包
ASP示例
ASP.NET示例
JSP示例
PHP示例