Open image file with dotNet and cannot save it


#1

Hi all!

On scrip i’m working on, i need to display some image in my WPF UI.
I load my image, when i want to replace it by saving from Max or other software, i’ve an I/O error.
I guess, it caused by opening file with dotNet but i don’t know how to solve it…

Here is the code:

for _d in _SLib do (
    _item = AddLibraryItem _panel
    _item.Content.Child.Children.Item[1].Text = _d.DisplayName
    _imgLoc = _d.image.Path +"\\"+ _d.image.Name
    if not doesFileExist _imgLoc do (
        _imgLoc = "img.png"
    )
    _imgO = dotNetOBject "system.windows.media.imaging.bitmapimage"
    _imgO.BeginInit()
    _imgO.CacheOptions = (dotNetClass "system.windows.media.imaging.bitmapcahceoptions") 2
    _imgO.UriSource = (dotNetObject "system.uri" _imgLoc)
    _imgO.EndInit()
		
    _item.Content.Child.Children.Item[0].Source = _imgO
    _imgO = undefined
	)

Someone know how to free a opened image?

Regards


#2

try…

_imgO.CacheOptions = (dotNetClass "system.windows.media.imaging.bitmapcahceoptions") 1

cache the entire image @ load time (OnLoad)


#3

Thanks @Klvnk for your answer! It help me :slight_smile:
But it wasn’t the entire solution, i found how to do.
Solution is to define path to file not with uri ans UriSource but with StreamSource.
Like that i can close stream after image loaded.

Here the new code :

for _d in _SLib do (
    _item = AddLibraryItem _panel
    _item.Content.Child.Children.Item[1].Text = _d.DisplayName
    _imgLoc = _d.image.Path + "\\" + _d.image.Name
    if not doesFileExist _imgLoc do (
       _imgLoc = "img.png"
    )
    _stream = dotNetObject "System.IO.FileStream" _imgLoc (dotNetClass "system.IO.FileMode").Open
    _imgO = dotNetOBject "system.windows.media.imaging.bitmapimage"
    _imgO.BeginInit()
    _cacheOpts = dotNetClass "system.windows.media.imaging.BitmapCacheOption"
    _imgO.CacheOption = _cacheOpts.OnLoad
    _imgO.StreamSource = _stream
    _imgO.EndInit()
   
    _item.Content.Child.Children.Item[0].Source = _imgO
    _imgO = undefined
    _stream.close()
)

Regards