2011-03-21 66 views
0

我想创建缩略图。我的原始文件夹的路径是:假设我:\我的图像**,我想生成它**我:\新图像。我有两个问题,第一问题是,如果我的图像文件夹包含子文件夹然后在新的图像它应该也在子文件夹不作为父文件夹缩略图生成错误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()); 
      } 
     } 
    } 
} 
+0

你究竟在哪里得到异常2和3?我记得在使用SQL Server Reporting Services构建充满图像的报告时,会遇到这些异常。该进程尝试在32位Windows安装上消耗超过2GB的内存后触发异常。请记住,进程仅限于他们可以处理多少内存(无论您的计算机实际拥有多少内存)。 此外,您可能想要考虑处置您的GDI对象。我以前使用过非托管GDI +,并且在使用这些对象后需要清理。 – enriquein 2011-03-21 20:32:43

+0

@Ernriquein我在处理某些文件后得到这些错误,比如说该文件夹包含30个图像,在处理了17张图像之后,它会出现此错误。我有64位Windows 7安装。 – safi 2011-03-21 20:37:12

回答

3

我刚才已经重构了一下代码,现在它的工作原理(在我的机器上):

private static void CreateThumbnail(string[] b, double wid, double hght, bool Isprint) 
{ 
    string saveAt = "D:\\check"; 
    foreach (string path in b) 
    { 
     var directory = new DirectoryInfo(path); 
     string outputPath = Path.Combine(saveAt, directory.Name); 
     foreach (FileInfo f in directory.GetFiles("*.*", SearchOption.AllDirectories)) 
     { 
      if (f.DirectoryName != directory.FullName) 
      { 
       outputPath = Path.Combine(saveAt, directory.Name, f.Directory.Name); 
      } 
      if (!Directory.Exists(outputPath)) 
      { 
       Directory.CreateDirectory(outputPath); 
      } 

      using (Image imagesize = Image.FromFile(f.FullName)) 
      using (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; 
       } 

       string fileName = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(f.Name) + ".jpeg"); 
       bitmapNew.GetThumbnailImage((int)newWidth, (int)newHeight,() => false, IntPtr.Zero) 
        .Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); 
      } 
     } 
    } 
} 

我得说几句gs关于您的旧代码:

  • 使用foreach时,尽可能;
  • 避免前三行,它们是无用的;
  • 避免未使用的变量;
  • 保持您的代码尽可能干净;
+0

@ As-CII非常感谢,我现在就试试,它是否也解决了子文件夹问题? – safi 2011-03-21 20:41:22

+0

是的,现在它确实:) – 2011-03-21 20:43:07

+0

@ AS-CII'位图bitmapNew = new Bitmap(imagesize);'这条线上的内存不足。 – safi 2011-03-21 20:47:20

1

关于“GDI +一般性错误”和“内存不足”异常,你应该看看这个链接上SO:What is the "best" way to create a thumbnail using ASP.NET?

必须使用C#using语句,以确保非托管的GDI +尽可能快地释放.NET表面下的资源,以避免内存不足错误。

例如,如果图像无法转换,GDI +通用会发生。

+0

谢谢,我会看看:) – safi 2011-03-21 20:39:49