Skip to content
This repository was archived by the owner on Mar 30, 2019. It is now read-only.
This repository was archived by the owner on Mar 30, 2019. It is now read-only.

TextureLoader.LoadBitmap() does not dispose of resources correctly #45

Description

@LordBenjamin

Calling TextureLoader.LoadBitmap() results in the target file being locked for the lifetime of the process. This is because it does not dispose of the BitmapDecoder or BitmapDecodeFrame objects that it creates.

The code below fixed for me:

    public static BitmapSource LoadBitmap(ImagingFactory factory, string filename) {
        using (var bitmapDecoder = CreateBitmapDecoder(factory, filename)) {
            using (var frame = bitmapDecoder.GetFrame(0)) {
                var formatConverter = new FormatConverter(factory);

                formatConverter.Initialize(
                    frame,
                    PixelFormat.Format32bppPRGBA,
                    BitmapDitherType.None,
                    null,
                    0.0,
                    BitmapPaletteType.Custom);

                return formatConverter;
            }
        }
    }

    private static BitmapDecoder CreateBitmapDecoder(ImagingFactory factory, string filename) {
        return new BitmapDecoder(
            factory,
            filename,
            DecodeOptions.CacheOnDemand);
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions