| | |
File being used by another process.
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Jan 2008
Posts: 27
Reputation:
Solved Threads: 0
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?
•
•
Join Date: Jan 2008
Posts: 27
Reputation:
Solved Threads: 0
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:
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...
C# Syntax (Toggle Plain Text)
string OCRProcess(int StartX, int StartY, int EndX, int EndY) { string file = @"c:\Bitonal-Out.tif"; Bitmap CutImage = new Bitmap(EndX - StartX, EndY - StartY); for (int x = StartX; x < EndX; x++) { for (int i = StartY; i < EndY; i++) CutImage.SetPixel(x - StartX, i - StartY, bmpScreenshot.GetPixel(x, i)); } Bitmap rgbBitmap = Converter.ConvertToRGB(CutImage); Bitmap bitonalBitmap = Converter.ConvertToBitonal(rgbBitmap); ImageCodecInfo imageCodecInfo = GetEncoderInfo("image/tiff"); System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Compression; EncoderParameters encoderParameters = new EncoderParameters(1); EncoderParameter encoderParameter = new EncoderParameter(encoder, (long)EncoderValue.CompressionCCITT4); encoderParameters.Param[0] = encoderParameter; bitonalBitmap.Save(file, imageCodecInfo, encoderParameters); MODI.Document doc = new MODI.Document(); doc.Create(file); doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false); MODI.Image img = (MODI.Image)doc.Images[0]; doc.Close(false); encoderParameter.Dispose(); CutImage.Dispose(); bitonalBitmap.Dispose(); return img.Layout.Text; }
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...
add
after
and see if that works.
C# Syntax (Toggle Plain Text)
file.Close();
after
C# Syntax (Toggle Plain Text)
bitonalBitmap.Dispose();
and see if that works.
•
•
Join Date: Jan 2008
Posts: 27
Reputation:
Solved Threads: 0
Dont work... heres the rest of the code that will allow you to run the program.
C# Syntax (Toggle Plain Text)
private static Bitmap bmpScreenshot; private static Graphics gfxScreenshot; public Form1() { InitializeComponent(); } private void convert_Click(object sender, EventArgs e) { string file = @"c:\Temp.tif"; this.Hide(); bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); gfxScreenshot = Graphics.FromImage(bmpScreenshot); gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); this.Show(); label1.Text = OCRProcess(807, 74, 947, 92,file); label2.Text = OCRProcess(807, 92, 947, 110,file); } private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null; }
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Open a file. (C#)
- Saving to a file from an array in C ? (C)
- Windows programming - C - Save file function (C++)
- PLEASE HELP about Using C# to add an executable file to my program! (C#)
- Can't Delete File!!! (Windows NT / 2000 / XP)
- Help with File Reading loop (C++)
- Unable to execute a video file in browser using Java (Java)
- End of file function (C)
Other Threads in the C# Forum
- Previous Thread: Division method
- Next Thread: Keeping a form focused (not a question)
| Thread Tools | Search this Thread |
.net access algorithm animation array bitmap box c# check checkbox client combobox control conversion csharp customactiondata database datagrid datagridview dataset datastructure date/time datetime datetimepicker degrees directrobot dll draganddrop drawing encryption enum excel file filename files form format formbox forms function gdi+ gis gtk hash image input install java label list mandelbrot math mouseclick mp3 mysql native operator outlook2003 packaging path photoshop picturebox pixelinversion pixelminversion post print process programming radians regex remoting richtextbox safari server sleep snooze socket sql statistics string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser webcam wfa wia winforms wpf xml





