Click here to Skip to main content
15,389,177 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Richard Deeming25-Jan-22 21:51
mveRichard Deeming25-Jan-22 21:51 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Starwer26-Jan-22 9:10
MemberStarwer26-Jan-22 9:10 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Gerry Schmitz27-Jan-22 6:33
mveGerry Schmitz27-Jan-22 6:33 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Starwer27-Jan-22 20:29
MemberStarwer27-Jan-22 20:29 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
lmoelleb3-Feb-22 4:58
Memberlmoelleb3-Feb-22 4:58 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Starwer5-Feb-22 0:28
MemberStarwer5-Feb-22 0:28 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
lmoelleb6-Feb-22 3:27
Memberlmoelleb6-Feb-22 3:27 
AnswerRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Starwer30-Jan-22 5:04
MemberStarwer30-Jan-22 5:04 
I have eventually found a "solution" to this problem.
Be prepared: that empirical solution makes no real sense, but this works... OMG | :OMG:

Solution was to go through the following convertions:
- byte[] to Bitmap using GetConverter;
- Bitmap redrawn to a new Bitmap using GDI+ Graphics
- Bitmap to byte[] again using ImageConverter
- byte[] to ImageSource using the previously mentioned BitmapFromRaw Method.

Any attempt to simplify this code lead to the memory leak to come back.
No clue how this could be the way to get the GC do its job. Like in relativity, the shortest path is not always the straight one...

Here is the the extra code that works for me. It makes my eyes bleed and I'm not proud of it. So again, if anyone could get something cleaner working, please propose.

public static ImageSource? BitmapFromRawNoLeak(byte[]? imageData)
{
    if (imageData == null) return null;
    ImageSource? image = null;

    // byte[] to Bitmap
    TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
    using (Bitmap? bmp = (Bitmap?)tc.ConvertFrom(imageData))
    {
        if (bmp == null) return null;

        // Force DPI to normal (96 DPI = no rescale) to avoid ugly rescaling in WPF when image comes from format with DPI
        bmp.SetResolution(96, 96);

        using (Bitmap source = new Bitmap(bmp.Width, bmp.Height))
        {
            if (source == null) return null;
            using (Graphics g = Graphics.FromImage(source))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);

            }

            ImageConverter converter = new ImageConverter();
            var vect = (byte[])converter.ConvertTo(source, typeof(byte[]));
            image = BitmapFromRaw(vect);
            image?.Freeze();
        }
    }

    return image;
}

GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Gerry Schmitz30-Jan-22 5:23
mveGerry Schmitz30-Jan-22 5:23 
GeneralRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Starwer30-Jan-22 7:45
MemberStarwer30-Jan-22 7:45 
SuggestionRe: Converting a byte[] to a ImageSource means huge memory leak Pin
Richard Deeming30-Jan-22 21:52
mveRichard Deeming30-Jan-22 21:52 
Question(MahApp) not using style for control Pin
Super Lloyd19-Jan-22 23:07
MemberSuper Lloyd19-Jan-22 23:07 
AnswerRe: (MahApp) not using style for control Pin
Super Lloyd19-Jan-22 23:08
MemberSuper Lloyd19-Jan-22 23:08 
QuestionMarkupExtension, resource files and visual studio designer Pin
Super Lloyd16-Jan-22 6:40
MemberSuper Lloyd16-Jan-22 6:40 
QuestionTextBox Bound To Decimal - Can't Type Decimal Pin
Kevin Marois28-Dec-21 8:29
professionalKevin Marois28-Dec-21 8:29 
AnswerRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Mycroft Holmes28-Dec-21 10:05
professionalMycroft Holmes28-Dec-21 10:05 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Kevin Marois28-Dec-21 10:25
professionalKevin Marois28-Dec-21 10:25 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Super Lloyd16-Jan-22 6:45
MemberSuper Lloyd16-Jan-22 6:45 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Mycroft Holmes16-Jan-22 10:55
professionalMycroft Holmes16-Jan-22 10:55 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Eddy Vluggen9-Feb-22 2:40
professionalEddy Vluggen9-Feb-22 2:40 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Super Lloyd9-Feb-22 12:10
MemberSuper Lloyd9-Feb-22 12:10 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Eddy Vluggen9-Feb-22 12:13
professionalEddy Vluggen9-Feb-22 12:13 
AnswerRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Richard Deeming4-Jan-22 0:04
mveRichard Deeming4-Jan-22 0:04 
QuestionSetting DataGrid Cell Style From Code Behind Pin
Kevin Marois20-Dec-21 10:56
professionalKevin Marois20-Dec-21 10:56 
AnswerRe: Setting DataGrid Cell Style From Code Behind Pin
#realJSOP22-Dec-21 2:06
mva#realJSOP22-Dec-21 2:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.