Reputation: 9960
for (int i = 0; i < PrefixDistinctCount; i++)
{
string FirstPolicy = "";
FirstPolicy = OriginalPics[0].ToString().Substring(26, 7);
var SamePoliciesQuery = OriginalPics.Where(x => Path.GetFileName(x).StartsWith(FirstPolicy));
foreach (string picture in SamePoliciesQuery)
{
File.Move(picture, AppVars.ProcessingPolicyImagesDirectory + picture.Substring(26, 12) + ".jpg");
}
string[] SamePolicyPics = Directory.GetFiles(AppVars.ProcessingPolicyImagesDirectory);
GenerateTiffFile(SamePolicyPics);
Directory.Delete(AppVars.ProcessingPolicyImagesDirectory, true);
Directory.CreateDirectory(AppVars.ProcessingPolicyImagesDirectory);
OriginalPics = Directory.GetFiles(SelectedDirectory);
}
Towards the end where it says "Directory.Delete(AppVars.ProcessingPolicyImagesDirectory, true);" i'm getting an error that it's not allowing me to delete the file. I'm GUESSING it's because something in the code is not closing the file or what not. does anyone know what could be the case? or how I would go about using "using" to fix this problem?
IOException was unhandled. The process cannot access the file 'blah blah' because it is being used by another process.
This is the code for GenerateTiffFile -
public void GenerateTiffFile(string[] SamePolicyPics)
{
ImageCodecInfo info = null;
foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
if (ice.MimeType == "image/tiff")
info = ice;
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
Bitmap pages = null;
int frame = 0;
foreach (string picture in SamePolicyPics)
{
if (frame == 0)
{
pages = (Bitmap)Image.FromFile(picture);
//pages.Save(picture.Substring(0, picture.Length - 3) + "tiff", info, ep);
pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7) + ".tiff", info, ep);
}
else
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
Bitmap bm = (Bitmap)Image.FromFile(picture);
pages.SaveAdd(bm, ep);
}
if (frame == SamePolicyPics.Length - 1)
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
}
Upvotes: 1
Views: 2101
Reputation: 4797
using(Bitmap pages = (Bitmap)Image.FromFile(SamePolicyPics[0])){
int frame = 0;
foreach (string picture in SamePolicyPics)
{
if (frame == 0)
{
pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7) + ".tiff", info, ep);
}
else
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
using(Bitmap bm = (Bitmap)Image.FromFile(picture)){
pages.SaveAdd(bm, ep);
}
}
if (frame == SamePolicyPics.Length - 1)
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}}
Upvotes: 1
Reputation: 10792
It'll require some rewriting of your code cause the object will only exist inside the using scope. But the idea is something like this
foreach (string picture in SamePolicyPics)
{
using (pages = (Bitmap)Image.FromFile(picture))
{
if (frame == 0)
{
pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7) + ".tiff", info, ep);
}
else
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
using (Bitmap bm = (Bitmap)Image.FromFile(picture))
{
pages.SaveAdd(bm, ep);
}
}
if (frame == SamePolicyPics.Length - 1)
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
}
frame++;
}
Upvotes: 0
Reputation: 151720
Bitmap bm = (Bitmap)Image.FromFile(picture);
This will keep the file locked until bm
is disposed, as the manual says. Wrap it in a using
block:
using (Bitmap bm = (Bitmap)Image.FromFile(picture))
{
// your code
}
Upvotes: 2