-2
public Form1()
{
    String _inputfile = @"C:\Input\cea.tif";

    InitializeComponent();

    pb_picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
    pb_picturebox.ClientSize = new Size(515, 515);
    pb_picturebox.Image = null;

    pb_picturebox.Image = Image.FromFile(_inputfile);

    GeoTiffReader tiff = new GeoTiffReader(_inputfile); // <- IOException was...
    IList<IGeometry> result = tiff.ReadToEnd();
    tiff.Close();
}

I get an IOException was unhalted error. How should I handle this problem? It works with different file names.

This code sample is cause the exception in GeoTiffReader class:

// open the stream for reading
try
{
    _baseStream = FileSystem.GetFileSystemForPath(_path).OpenFile(_path.AbsolutePath, FileMode.Open, FileAccess.Read); // open the file specified by path
}
catch (Exception ex)
{
    throw new IOException("Exception occured during stream opening.", ex);
}

Watch: name: ex value: {"The process cannot access the file 'C:\Input\cea.tif' because it is being used by another process."}

BlackCat
  • 521
  • 3
  • 8
  • 22
  • `try { ... } catch(IOException) { ... }` maybe? – Lucas Trzesniewski Jul 03 '14 at 10:55
  • this line throws the exception: _baseStream = FileSystem.GetFileSystemForPath(_path).OpenFile(_path.AbsolutePath, FileMode.Open, FileAccess.Read); i think because the Image.FromFile locks the file or something. – BlackCat Jul 03 '14 at 11:01
  • you need to catch the exception and parse through it to find the cause. It'll provide more specific information. – user1666620 Jul 03 '14 at 11:03
  • 1
    See [http://stackoverflow.com/a/6576645/2655508](http://stackoverflow.com/a/6576645/2655508) – Heslacher Jul 03 '14 at 11:15

1 Answers1

1

Based on: https://stackoverflow.com/a/6576645/2655508

Replace this line

pb_picturebox.Image = Image.FromFile(_inputfile);

with this lines

using (FileStream stream = new FileStream(_inputfile, FileMode.Open, FileAccess.Read))
{
    pb_picturebox.Image = Image.FromStream(stream);
}

By using the using keyword the stream will be disposed as soon as the execution leaves the using block and the file isn`t locked any more.

Community
  • 1
  • 1
Heslacher
  • 2,167
  • 22
  • 37