我想创建缩略图。我的原始文件夹的路径是:假设我:\我的图像**,我想生成它**我:\新图像。我有两个问题,第一问题是,如果我的图像文件夹包含子文件夹然后在新的图像它应该也在子文件夹不作为父文件夹。缩略图生成错误c#
秒我得到一个错误。**在GDI +中发生了一个通用错误。
3日我收到此错误:内存不足。**
它是一个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 +,并且在使用这些对象后需要清理。 – enriquein 2011-03-21 20:32:43
@Ernriquein我在处理某些文件后得到这些错误,比如说该文件夹包含30个图像,在处理了17张图像之后,它会出现此错误。我有64位Windows 7安装。 – safi 2011-03-21 20:37:12