0

I am loading a Visual Studio Gridview cell with a thumbnail. I need to be able to delete or rename the source thumbnail file based on user input. When I try to rename or delete the code returns the error that the 'file is use' when I load the cell directly from the source file using:

fileGridView.Rows[dgIndex].Cells["thumbNail"].Value = Image.FromFile(tname);

I have tried to load the cell from cache with the following code, but get System.Format Exception. Cannot convert C:filename.jpg cannot be converted to type 'Image'.

        BitmapImage timage = new BitmapImage();
        timage.BeginInit();
        timage.CacheOption = BitmapCacheOption.OnLoad;
        timage.UriSource = new Uri(tname);
        timage.EndInit();
        fileGridView.Rows[dgIndex].Cells["thumbNail"].Value= timage;

What is the correct approach to loading the cell with the image without locking the source file (so I can rename or delete as required).

Gridview looks like: Gridview

Thank you in advance for your consideration of this issue!

HighwayRob
  • 119
  • 2
  • 11
  • Can you open the jpg file using a image viewer on the machine where failure is occurring? The image mode may not work with the graphic card on machine. You may need to upgrade the driver (or graphic card) on machine to view image. An image has an ascii text header that can be seen with notepad. Opening file with notepad will give the image mode. – jdweng Aug 23 '22 at 16:01
  • fileGridView.Rows[dgIndex].Cells["thumbNail"].Value = Image.FromFile(tname); code loads the file perfectly, Problem it locks the source file and until I exit the program, it cannot be renamed or deleted. – HighwayRob Aug 23 '22 at 16:06
  • Putting code in a using block will automatically dispose the object when code leaves the block which normally solves these types of issues. – jdweng Aug 23 '22 at 16:13
  • The gridview is loaded in a sub-routine that calls multiple other routines, including the one that resizes and loads the cell with the image. Once a cell is initialized with the value being a source file form the disc, it is locked (in use). – HighwayRob Aug 23 '22 at 18:34
  • Read file into memory stream. The use Image.FromStream. – jdweng Aug 23 '22 at 23:35

0 Answers0