【发布时间】:2011-07-19 23:06:45
【问题描述】:
我正在尝试创建缩略图。我的原始文件夹路径是:假设 I:\my images**,我想将它生成到 **i:\new images。我遇到了两个问题,第一个问题是如果我的图片文件夹包含子文件夹,那么在新图片中它也应该在子文件夹中,而不是作为父文件夹 .
-
第二次出现错误。**在 GDI+ 中出现一般错误。
第三次出现此错误:内存不足。**
它是一个 csharp 控制台应用程序
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at ConsoleApplication1.Program.CreateThumbnail(String[] b, Double wid, Double hght, Boolean Isprint)
public void CreateThumbnail(string[] b, double wid, double hght, bool Isprint)
{
string[] path;
path = new string [64];
path = b;
string saveath = "i:\\check\\a test\\";
for (int i = 0; i < b.Length; i++)
{
DirectoryInfo dir = new DirectoryInfo(path[i]);
string dir1 = dir.ToString();
dir1 = dir1.Substring(dir1.LastIndexOf("\\"));
FileInfo[] files1 = dir.GetFiles();
foreach (FileInfo f in files1)
{
string gh = f.ToString();
try
{
System.Drawing.Image myThumbnail150;
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image imagesize = System.Drawing.Image.FromFile(f.FullName);
Bitmap bitmapNew = new Bitmap(imagesize);
double maxWidth = wid;
double maxHeight = hght;
int w = imagesize.Width;
int h = imagesize.Height;
// Longest and shortest dimension
int longestDimension = (w > h) ? w : h;
int shortestDimension = (w < h) ? w : h;
// propotionality
float factor = ((float)longestDimension) / shortestDimension;
// default width is greater than height
double newWidth = maxWidth;
double newHeight = maxWidth / factor;
// if height greater than width recalculate
if (w < h)
{
newWidth = maxHeight / factor;
newHeight = maxHeight;
}
myThumbnail150 = bitmapNew.GetThumbnailImage((int)newWidth, (int)newHeight, myCallback, IntPtr.Zero);
string ext = Path.GetExtension(f.Name);
if (!Directory.Exists(saveath + dir1))
{
Directory.CreateDirectory(saveath + dir1);
myThumbnail150.Save(saveath + dir1 + "\\" + f.Name.Replace(ext, ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if(Directory.Exists(saveath+dir1))
{
myThumbnail150.Save(saveath + dir1+" \\"+ f.Name.Replace(ext, ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
catch (Exception ex)
{
Console.WriteLine("something went wrong" + ex.ToString());
}
}
}
}
【问题讨论】:
-
您在哪里得到异常 2 和 3?我记得在使用 SQL Server Reporting Services 构建充满图像的报告时遇到了这些异常。在进程尝试在 32 位 Windows 安装上消耗超过 2gb 的内存后触发的异常。请记住,进程受限于它们可以寻址的内存量(无论您的计算机中实际拥有多少内存)。此外,您可能想研究如何处理您的 GDI 对象。我之前使用过非托管 GDI+,您需要在使用对象后进行清理。
-
@Ernriquein 在处理某些文件后出现这些错误,假设该文件夹包含 30 张图像,在处理 17 张图像后会出现此错误。我安装了 64 位 Windows 7。
标签: c#