缩略图生成错误c#

时间:2011-03-21 19:50:02

标签: c#

我正在尝试创建缩略图。我原始文件夹的路径是:假设 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 个答案:

答案 0 :(得分: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);
            }
        }
    }
}

我不得不说一些关于旧代码的事情:

  • 尽可能使用foreach;
  • 避免前三行,它们没用;
  • 避免使用未使用的变量;
  • 尽可能保持代码清洁;

答案 1 :(得分:1)

关于“GDI +一般错误”和“Out Of Memory”异常,您应该看一下SO上的链接:What is the "best" way to create a thumbnail using ASP.NET?

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

例如,如果无法转换图像,则可能会发生GDI +泛型。