Pregunta

I'm loading all the images from a folder that match a particular filename mask (*.tif). One at a time, I'm pulling a section of the image and putting it into an output image and next to it I DrawString to label which file it came from. When I save the output image, I SOMETIMES get a generic GDI+ error.

Rectangle source = new Rectangle(0, 0, 2500, 50);
Font font = new Font("Arial", 12);
String filename = new DirectoryInfo(dir).Name;
Rectangle divider = new Rectangle(0, 50, 2900, 5);
Brush lightgray = new SolidBrush(Color.FromArgb(235, 235, 235));

Console.WriteLine("Looking in \"" + dir + "\" for \"" + mask + "\"");

List<string> files = Directory.EnumerateFiles(dir, mask).ToList();
using (Bitmap output = new Bitmap(2900, (source.Height + divider.Height) * (files.Count + 1)))
{
    using (Graphics g = Graphics.FromImage(output))
    {
        g.FillRectangle(Brushes.White, 0, 0, output.Width, output.Height);
        Rectangle target = new Rectangle(new Point(), source.Size);
        foreach (string file in files)
        {
            try
            {
                using (Bitmap cur = (Bitmap)Bitmap.FromFile(file))
                {
                    g.DrawImage(cur, target, source, GraphicsUnit.Pixel);
                }
            }
            catch
            {
                Point p1 = target.Location;
                p1.Offset(3, 3);
                g.DrawString("Not an Image File", font, Brushes.Black, p1);
            }
            finally
            {
                string name = Path.GetFileName(file);
                g.DrawString(name, font, Brushes.Black, target.X + target.Width, target.Y + 4);
                g.FillRectangle(lightgray, divider);
                divider.Y += source.Height + divider.Height;
                target.Y += source.Height + divider.Height;
            }
        }
        Point p2 = target.Location;
        p2.Offset(3, 3);
        String type = "images";
        if (mask == "*") type = "files";
        if (files.Count == 1) type = type.Substring(0, type.Length - 1);
        g.DrawString("Processed " + files.Count + " " + type, font, Brushes.Black, p2);
    }
    output.Save(filename + ".png", ImageFormat.Png);// GDI+ Error here
}

I'm having to search thousands of large images for a particular image. There's a generated region at the top of each of these images that I want this program to accumulate so I can easily, in one place, browse that region of all of the files. If I point the app to one folder, it works, but if I point it to another, it doesn't and throws a generic GDI+ error on save. What is causing this?

Could it be the size of the output image? I'm not 100% convinced of this because for some folders it was throwing an error at the Bitmap's constructor when creating my output bitmap. That seemed like a size issue. I imagine if the constructor can create it, shouldn't I be able to save it?

¿Fue útil?

Solución

Saving an image requires more memory that creating it, since memory is used to encode it. It's entirely possible that your image is just too large for the png processor to encode. Try saving it as a bitmap, just to see it will work.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top