c# - Release original ImageSource after a TransformedBitmap is constructed from it -
background:
i'm building application open potentially large number of photos, generate thumbnail present user, allow things exif data viewing/clearing , minor post processing. want allow user scroll through images without pausing load each 1 becomes visible, don't want keep dozens or hundreds of full size bitmap images in memory.
i had built prototype of task using system.drawing
using image
objects , generatethumbnailimage
method, decided move wpf , use system.windows.media.imagesource
derived objects , transformedbitmap
scaledtransform
generate thumbnail.
what found, though, when create transformedbitmap
, has reference source image, available , still present in memory. how release source object?
some relevant c# code:
using system.windows.media; using system.windows.media.imaging; using system.io; using system; ... private void loadimage(){ //called class internally handle generating thumbnail //intent keep metadata , thumbnail bitmap in memory stream handle = file.openread(filename); bitmapdecoder source = bitmapdecoder.create(handle,bitmapcreateoptions.none,bitmapcacheoption.onload); handle.dispose(); //determine scaling ratio force larger of height or width fit inside desired thumbnail size (int)maxdim. scaleratio = math.min(maxdim/math.max(source.frames[0].pixelheight,source.frames[0].pixelwidth),1); //a public member of class, double _imagesource = new transformedbitmap(source.frames[0],new scaletransform(scaleratio,scaleratio)); //private member of class, imagesource _imagesource.freeze(); _exif = source.frames[0].metadata; //private member of class, imagemetadata }
the problem here while hoped (bitmapdecoder)source
released, can still access object via _imagesource.source
.
i have considered using copypixels
or encoding transformedbitmap
byte[]
stream create new, unattached bitmap, both of methods seem unnecessary reprocessing if can abandon or dispose of source or if there simple , fast way create shallow clone haven't discovered. attempt @ shallow clone using bitmapframe.create(transformedbitmap)
doesn't free memory either, doesn't leave me obvious reference.
some testing watching memory consumption shows each image loaded costs 30mb. approximately 200x200@32bpp image should 160kb, not counting overhead.
the question again tl;dr: how release reference source bitmap after transformedbitmap uses it?
Comments
Post a Comment