1

I'm saving a bitmap to a file on my hard drive inside of a loop (All the jpeg files within a directory are being saved to a database). The save works fine the first pass through the loop, but then gives the subject error on the second pass. I thought perhaps the file was getting locked so I tried generating a unique file name for each pass, and I'm also using Dispose() on the bitmap after the file get saved. Any idea what is causing this error?

Here is my code:

private string fileReducedDimName = @"c:\temp\Photos\test\filePhotoRedDim";
...
foreach (string file in files)
{
    int i = 0;

    //if the file dimensions are big, scale the file down
    Stream photoStream = File.OpenRead(file);
    byte[] photoByte = new byte[photoStream.Length];
    photoStream.Read(photoByte, 0, System.Convert.ToInt32(photoByte.Length));
    Image image = Image.FromStream(new MemoryStream(photoByte));
    Bitmap bm = ScaleImage(image);
    bm.Save(fileReducedDimName + i.ToString() + ".jpg", ImageFormat.Jpeg);//error occurs here
    Array.Clear(photoByte,0, photoByte.Length);
    bm.Dispose();

    i ++;
}
...

Thanks

Here's the scale image code: (this seems to be working ok)

protected Bitmap ScaleImage(System.Drawing.Image Image)
    {
        //reduce dimensions of image if appropriate
        int destWidth;
        int destHeight;
        int sourceRes;//resolution of image
        int maxDimPix;//largest dimension of image pixels
        int maxDimInch;//largest dimension of image inches
        Double redFactor;//factor to reduce dimensions by
        if (Image.Width > Image.Height)
        {
            maxDimPix = Image.Width;
        }
        else
        {
            maxDimPix = Image.Height;
        }
        sourceRes = Convert.ToInt32(Image.HorizontalResolution);
        maxDimInch = Convert.ToInt32(maxDimPix / sourceRes);

        //Assign size red factor based on max dimension of image (inches)
        if (maxDimInch >= 17)
        {
            redFactor = 0.45;
        }
        else if (maxDimInch < 17 && maxDimInch >= 11)
        {
            redFactor = 0.65;
        }
        else if (maxDimInch < 11 && maxDimInch >= 8)
        {
            redFactor = 0.85;
        }
        else//smaller than 8" dont reduce dimensions
        {
            redFactor = 1;
        }
        destWidth = Convert.ToInt32(Image.Width * redFactor);
        destHeight = Convert.ToInt32(Image.Height * redFactor);
        Bitmap bm = new Bitmap(destWidth, destHeight,
            PixelFormat.Format24bppRgb);
        bm.SetResolution(Image.HorizontalResolution, Image.VerticalResolution);
        Graphics grPhoto = Graphics.FromImage(bm);
        grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(Image,
            new Rectangle(0, 0, destWidth, destHeight),
            new Rectangle(0, 0, Image.Width, Image.Height),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        return bm;

    }
pvitt
  • 1,005
  • 4
  • 14
  • 31

2 Answers2

2

If I'm reading the code right, your i variable is zero every time through the loop.

1

It is hard to diagnose exactly what is wrong, I would recommend that you use using statements to ensure that your instances are getting disposed of properly, but it looks like they are.

I originally thought it might be an issue with the ScaleImage. So I tried a different resize function (C# GDI+ Image Resize Function) and it worked, but i is always set to zero at beginning of each loop. Once you move i's initialization outside of the loop your scale method works as well.

private void MethodName()
{
    string fileReducedDimName = @"c:\pics";

    int i = 0;

    foreach (string file in Directory.GetFiles(fileReducedDimName, "*.jpg"))
    {


        //if the file dimensions are big, scale the file down
        using (Image image = Image.FromFile(file))
        {
            using (Bitmap bm = ScaleImage(image))
            {
                bm.Save(fileReducedDimName + @"\" + i.ToString() + ".jpg", ImageFormat.Jpeg);//error occurs here

                //this is all redundant code - do not need
                //Array.Clear(photoByte, 0, photoByte.Length);
                //bm.Dispose();
            }
        }

        //ResizeImage(file, 50, 50, fileReducedDimName +@"\" + i.ToString()+".jpg");

        i++;
    }

}
Community
  • 1
  • 1
viggity
  • 15,039
  • 7
  • 88
  • 96