File being used by another process.

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 27
Reputation: Barbarrosa is an unknown quantity at this point 
Solved Threads: 0
Barbarrosa Barbarrosa is offline Offline
Light Poster

File being used by another process.

 
0
  #1
Mar 30th, 2008
Does anyone know how to close a file? I saved an image... then when i try and save the image to the same filename.. or delete the file after i saved it... i get an error which states that the file is being used by another process. Has anyone come across a problem like this and if so how could I get around it?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 6
Reputation: zAndrew is an unknown quantity at this point 
Solved Threads: 0
zAndrew zAndrew is offline Offline
Newbie Poster

Re: File being used by another process.

 
0
  #2
Mar 30th, 2008
I'm sure you checked, but did you make sure to close the resource for that file after you were finished with saving it? If not, that would certainly make it hard to do much of anything with it.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 27
Reputation: Barbarrosa is an unknown quantity at this point 
Solved Threads: 0
Barbarrosa Barbarrosa is offline Offline
Light Poster

Re: File being used by another process.

 
0
  #3
Mar 30th, 2008
I am fairly new to C# lol so can you please write down in code by what you mean. I've been trying to chase down the source of this "generic error in GDI+" for over a week. I tried to delete the file after I saved it which is when i get the "file being used...". this is the code that I am using:

  1. string OCRProcess(int StartX, int StartY, int EndX, int EndY)
  2. {
  3.  
  4. string file = @"c:\Bitonal-Out.tif";
  5. Bitmap CutImage = new Bitmap(EndX - StartX, EndY - StartY);
  6.  
  7. for (int x = StartX; x < EndX; x++)
  8. {
  9. for (int i = StartY; i < EndY; i++)
  10. CutImage.SetPixel(x - StartX, i - StartY, bmpScreenshot.GetPixel(x, i));
  11. }
  12.  
  13. Bitmap rgbBitmap = Converter.ConvertToRGB(CutImage);
  14.  
  15. Bitmap bitonalBitmap = Converter.ConvertToBitonal(rgbBitmap);
  16.  
  17. ImageCodecInfo imageCodecInfo = GetEncoderInfo("image/tiff");
  18. System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Compression;
  19. EncoderParameters encoderParameters = new EncoderParameters(1);
  20.  
  21. EncoderParameter encoderParameter = new EncoderParameter(encoder, (long)EncoderValue.CompressionCCITT4);
  22. encoderParameters.Param[0] = encoderParameter;
  23.  
  24. bitonalBitmap.Save(file, imageCodecInfo, encoderParameters);
  25.  
  26. MODI.Document doc = new MODI.Document();
  27. doc.Create(file);
  28.  
  29. doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);
  30.  
  31. MODI.Image img = (MODI.Image)doc.Images[0];
  32.  
  33. doc.Close(false);
  34. encoderParameter.Dispose();
  35. CutImage.Dispose();
  36. bitonalBitmap.Dispose();
  37.  
  38. return img.Layout.Text;
  39. }

The above function simply simply cuts an image from the screenshot, converts it to tiff, and does OCR on it. when i try to delete the file "bitonal-out.tif" i get the error message being use... Your help is greatly appreciated...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 24
Reputation: david.crawford is an unknown quantity at this point 
Solved Threads: 2
david.crawford's Avatar
david.crawford david.crawford is offline Offline
Newbie Poster

Re: File being used by another process.

 
0
  #4
Mar 30th, 2008
add

  1. file.Close();

after

  1. bitonalBitmap.Dispose();

and see if that works.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 27
Reputation: Barbarrosa is an unknown quantity at this point 
Solved Threads: 0
Barbarrosa Barbarrosa is offline Offline
Light Poster

Re: File being used by another process.

 
0
  #5
Mar 30th, 2008
Dont work... heres the rest of the code that will allow you to run the program.

  1. private static Bitmap bmpScreenshot;
  2. private static Graphics gfxScreenshot;
  3.  
  4. public Form1()
  5. {
  6. InitializeComponent();
  7. }
  8.  
  9. private void convert_Click(object sender, EventArgs e)
  10. {
  11. string file = @"c:\Temp.tif";
  12. this.Hide();
  13. bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
  14. gfxScreenshot = Graphics.FromImage(bmpScreenshot);
  15. gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
  16. this.Show();
  17.  
  18. label1.Text = OCRProcess(807, 74, 947, 92,file);
  19. label2.Text = OCRProcess(807, 92, 947, 110,file);
  20. }
  21.  
  22. private static ImageCodecInfo GetEncoderInfo(String mimeType)
  23. {
  24. int j;
  25. ImageCodecInfo[] encoders;
  26. encoders = ImageCodecInfo.GetImageEncoders();
  27. for (j = 0; j < encoders.Length; ++j)
  28. {
  29. if (encoders[j].MimeType == mimeType)
  30. return encoders[j];
  31. }
  32. return null;
  33. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 27
Reputation: Barbarrosa is an unknown quantity at this point 
Solved Threads: 0
Barbarrosa Barbarrosa is offline Offline
Light Poster

Re: File being used by another process.

 
0
  #6
Mar 30th, 2008
oo wait your going to have to modify it a little.. i was playing around with it so your going to have to add a string parameter to the OCRprocess and delete the string file already in place.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 24
Reputation: david.crawford is an unknown quantity at this point 
Solved Threads: 2
david.crawford's Avatar
david.crawford david.crawford is offline Offline
Newbie Poster

Re: File being used by another process.

 
0
  #7
Mar 30th, 2008
its the tif that wont open later right?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 27
Reputation: Barbarrosa is an unknown quantity at this point 
Solved Threads: 0
Barbarrosa Barbarrosa is offline Offline
Light Poster

Re: File being used by another process.

 
0
  #8
Mar 31st, 2008
its the tif that i cant delete after I've saved it and used it for MODI. basically look at the bitonalImage.Save line
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 24
Reputation: david.crawford is an unknown quantity at this point 
Solved Threads: 2
david.crawford's Avatar
david.crawford david.crawford is offline Offline
Newbie Poster

Re: File being used by another process.

 
0
  #9
Apr 1st, 2008
I dont understand why it wouldnt work. Does anyone else have an idea for this?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: Denis St Flour is an unknown quantity at this point 
Solved Threads: 0
Denis St Flour Denis St Flour is offline Offline
Newbie Poster

Re: File being used by another process.

 
0
  #10
May 20th, 2008
I just had the same problem and

I added
GC.Collect();
GC.WaitForPendingFinalizers();

after the doc.Close(true) and that fixed the problem.

I only found that by accident. I was in debug mode with a break point after doc.Close. I noticed if I waited for a while and resumed debugging the next iteration would work sometimes. So I decided to form GC to release whatever resources that was holding the lock and it worked.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC