sonia naidu

sonia naidu

  • NA
  • 12
  • 27.7k

The process cannot access the file because it is being used by another process.

Mar 24 2012 8:54 AM

im using microsoft visual studio 2010 c# express
m trying to upload,save the image and then want to move the image to another folder... 
only when the control goes to move function i get this error (The process cannot access the file because it is being used by another process.)
//this is my code 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OnBarcode.Barcode.BarcodeScanner;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.IO;
 
namespace AutoImageProcessing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

 
private void Form1_Load(object sender, EventArgs e)
{
}
 
private void label1_Click(object sender, EventArgs e)
{
 
}
 
private void button1_Click(object sender, EventArgs e)
{

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*.jpg|*.jpg";
ofd.Multiselect = false;
ofd.Title = "Select JPEG File";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;

}





 
}
 
private void button2_Click(object sender, EventArgs e)
{
string path = textBox1.Text;
//->if (textBox1.Text != string.Empty)
if (path != string.Empty)
{
 
try
{
pictureBox1.Image = Image.FromFile(path);
 
//->String[] barcodes = BarcodeScanner.Scan(textBox1.Text, BarcodeType.All);
String[] barcodes = BarcodeScanner.Scan(path, BarcodeType.All);
// MessageBox.Show(barcodes[0]);
 
// Specify a "currently active folder"
//string activeDir = @"f:";
 
//Create a new subfolder under the current active folder
//string newPath1 = System.IO.Path.Combine(activeDir, "export");
string newPath = @"f:/export/" + barcodes[1];
//string newPath = System.IO.Path.Combine(activeDir1, barcodes[1]);
 
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
 
// Create a new file name. This example generates
// a random string.
string newFileName = barcodes[1];
// Combine the new file name with the path
newPath = System.IO.Path.Combine(newPath, newFileName);

 
//cropping the signature and photograph
//saving the 2 cropped images into the folder
if (!System.IO.File.Exists(newPath))
{

try
{

Point start = new Point();
Point end = new Point();
int width;
int height;

using (Bitmap bitmap = new Bitmap(pictureBox1.Image))
{
bitmap.Save(newPath + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
}
MessageBox.Show("image saved!!!");

 

start.X = 305;
start.Y = 489;
end.X = 843;
end.Y = 569;
width = 538;
height = 80;
Rectangle imageRectangle = new Rectangle(start.X, start.Y, width, height);
 
if (start == new Point(0, 0) || end == new Point(0, 0))
{
MessageBox.Show("First select start and end point by clicking on the image");
}
else
{
try
{

pictureBox1.Image = cropImage(pictureBox1.Image, imageRectangle);

using (Bitmap bitmap = new Bitmap(pictureBox1.Image))
{
bitmap.Save(newPath + "_barcode.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
}
MessageBox.Show("image saved!!!");



}
catch (Exception ex)
{
MessageBox.Show("this is the error \n" + ex.Message);
}
 

}
 


}
catch (Exception ex)
{
MessageBox.Show("An Error has occured: \n" + ex.Message);
}


 

 
}

}
catch (Exception ex)
{
MessageBox.Show("An Error has occured: \n" + ex.Message);
return;
}
 
}
 
else
{
MessageBox.Show("Please Enter Location of Image");
}
try
{
Image.FromFile(path).Dispose();

MessageBox.Show("entering move function");
Process end=Process.GetCurrentProcess();
MessageBox.Show(end.ToString());
end.Dispose();
end.Close();



}
catch(IOException w) 

MessageBox.Show("error"+w.Message); 
}
try
{
System.IO.File.Move(path, @"f:\img_pr");
MessageBox.Show("move completed");
}
catch (IOException w)
{
MessageBox.Show("error" + w.Message);
}
 

}
 

 

 
private void pictureBox1_Click(object sender, EventArgs e)
{
 
}
 

 
private Image cropImage(Image image, Rectangle imageRectangle)
{
Bitmap bitmap1 = new Bitmap(image);
Bitmap cropedBitmap = bitmap1.Clone(imageRectangle, bitmap1.PixelFormat);
return (Image)(cropedBitmap);
}
 
private Image bitmap2(Image image, Rectangle rectangle)
{
throw new NotImplementedException();

 
}
}

Answers (3)