FileOnQ / Imaging.Heif

A C#/.NET wrapper around libheif for decoding and processing high efficiency image formats (heif, heic).
GNU Lesser General Public License v3.0
15 stars 4 forks source link

Original image is in use after conversion HEIC to JPEG #97

Closed JefEe closed 2 years ago

JefEe commented 2 years ago

Hi,

When I save an HEIC image as JPEG i cannot delete the file during the runtime. The file stays in use by another process.

        Dim oHEICimage As New HeifImage(FullPath)
        oHEICimage.PrimaryImage.Write(oNewPath)
        oHEICimage.Dispose()

Any suggestions how to remove the image after making the conversion (to avoid duplicates)

SkyeHoefling commented 2 years ago

Can you provide more details on the project? Framework version will be helpful. If you provide a reproduction project it'll be really easy to look and see what's wrong

Your code looks correct but I have not used this on VB only C#

JefEe commented 2 years ago

Hi,

I uploaded to WE-transfer a copy of the program (visual studio) https://we.tl/t-F72pFSTdJG

Is this sufficient as "reproduction project"?

SkyeHoefling commented 2 years ago

Please upload the project directly to this GitHub thread. You can drag and drop into a comment

JefEe commented 2 years ago

FileStructure.zip

Secondly, this is a batch program for mass converting. When processing a large set 500+ images i get this error. I believe it's same reason why the files are still in use.

ex.Message = "Heif image exception - Input file does not exist: Unspecified: Error opening file: Too many open files (24)" & vbLf

SkyeHoefling commented 2 years ago

I think I see the problem, didn't notice it until I pulled the code. In the snippet above you grab the PrimaryImage and never dispose of it. Looking at the README getting started docs (copied below)

using (var image = new HeifImage("image.heic"))
using (var primary = image.Primary())
{
    primary.Write("output.jpeg", 90);
}

Your code needs to store the PrimaryImage. Using your provided code I was able to convert 10 heic images in a folder to jpeg. Here is the updated HeifImage code

Dim oHEICimage As New HeifImage(FullPath)
Dim oNewPath As String = System.IO.Path.Combine(Directory, Name & ".jpeg")
Dim oPrimary as IImage = oHEICimage.PrimaryImage()
oPrimary.Write(oNewPath)
oPrimary.Dispose()
oHEICimage.Dispose()
Return oNewPath

Hope this helps - closing issue out as there is no bug with the library.

JefEe commented 2 years ago

Can you check again? I altered the code as recomended, but thet error persists.

        Dim oNewPath As String = System.IO.Path.Combine(Directory, Name & ".jpeg")
        Dim oHEICimage As New HeifImage(FullPath)
        Dim oPrimary As IImage = oHEICimage.PrimaryImage
        oHEICimage.PrimaryImage.Write(oNewPath)
        oPrimary.Dispose()
        oHEICimage.Dispose()
        System.IO.File.Delete(FullPath)

Deletion is not possible due to base file in use.

SkyeHoefling commented 2 years ago

I just tried the code you provided in the sample project and I am unable to reproduce your issue. What version of .NET are you using? Is it framework of the modern .NET? Something may be getting cached, maybe try cleaning the solution and manually deleting your bin/obj folders

JefEe commented 2 years ago

Target framework is .NET 6.0 When I clean bin/obj folders the error persists

SkyeHoefling commented 2 years ago

🤔🤔

I am running your project with the same code and it works for me and not you. We are both running .NET 6.0 and yours throws an exception and mine doesn't. I am not really sure what else to say, if I can't reproduce the problem you are seeing then it is difficult for me to provide anymore advice, sorry 😢