0

I've been working on a face recognition system. The image processor using the following code :

Bitmap bit = (Bitmap) System.Drawing.Image.FromFile (files [j].FullName, true);
Image <Bgr, Byte> img1 = new Image <Bgr, Byte>(bit); // path can be absolute or relative.
Image <Gray, Byte> grayFrem = img1.Convert <Gray, byte>();

I render and receive an increase in data and puts the data into the Dictionary.

After the income I want to actually do:

Img1.Dispose ();

I do it because I want to unlock the file and then perform a copy in a different class, this is the code that I use to copy.

File.Copy (Path.Combine (path, pic), Path.Combine (backupDir, pic), true);

I get still get this error :  

Unhandled Exception: System.IO.IOException: The process can not access the file 'C: \ xxxxxx \ xxxx \ xxx \ 95177.jpg' because it s being used by another process.

Can I use another method to unlock the image or the process?

Best regards

user2314737
  • 27,088
  • 20
  • 102
  • 114

2 Answers2

1

Here you need to dispose the object of MailMessage.

For Ex.

// Sends email using SMTP, Uses default network credentials
public static void SendEmail(string To, string From, string BCC, string    Subject, string Body, bool IsBodyHtml, string attachedPath = "") {
    char[] validSeperators = new char[] { ',', ':', ';' };
    string fromEmail = string.Empty;
    foreach (char seperator in validSeperators) {
        if (!string.IsNullOrEmpty(From) && From.Contains(seperator.ToString())) {
            fromEmail = From.Split(seperator)[0];
            break;
        }
    }

    //create mail message
    MailMessage message = !string.IsNullOrEmpty(fromEmail) ? new MailMessage(fromEmail, To) : new MailMessage(From, To);
    if (BCC.Length > 0) {
        message.Bcc.Add(BCC);
    }

    message.IsBodyHtml = IsBodyHtml;
    message.Subject = Subject;
    message.Body = Body;

    // Email Attachment
    if (!string.IsNullOrEmpty(attachedPath)) {
        Attachment attach = new Attachment(attachedPath);

        // Attach the created email attachment 
        message.Attachments.Add(attach);
    }

    //create mail client and send email
    SmtpClient emailClient = new SmtpClient();

    emailClient.Host = SMTPServer;
    emailClient.Port = SMTPPort;
    emailClient.Credentials = new NetworkCredential(SMTPUserName, SMTPPassword);
    emailClient.EnableSsl = EnableSSLForSMTP;
    emailClient.Send(message);
    //Here you can dispose it after sending the mail.
    message.Dispose();

    //Remove specific file after sending mail to customer
    if (!string.IsNullOrEmpty(attachedPath))
        DeleteAttachedFile(attachedPath);
}

//Method to remove attached file from specific path.
private static void DeleteAttachedFile(string attachedPath) {
    File.SetAttributes(attachedPath, FileAttributes.Normal);
    File.Delete(attachedPath);
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

You should dispose bit not img1. Besides, when having problems, load all data to memory and then make bitmap from that.

jjaskulowski
  • 2,524
  • 3
  • 26
  • 36
  • I tried to do bit.Dispose and still get :The process cannot access the file 'C:\RollcallPIc\LocalCamera\camera\95177.jpg' because it is being used by another process. – Rotem Emergi Jun 04 '16 at 08:53
  • then try system.drawing.image.fromstream and file.open enclosed in using – jjaskulowski Jun 04 '16 at 20:20