Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak loading imagebrush to grid.background

Tags:

c#

wpf

gridview

I have one app sending screenshots to another app that needs to present them in the WPF window it works great except for the only probelm which is that the code builds up in the memory each time i add a new background.

how do I solve this problem ?

Thanks!

  private void GetSnapshots(object state)
    {
        using (var socket=new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {

            socket.Connect(new IPEndPoint(IPAddress.Parse("10.0.0.9"), 8081));
            while (Connected)
            {


                    var lengthData = new byte[4];
                    var lengthBytesRead = 0;
                    while (lengthBytesRead < lengthData.Length)
                    {
                        var read = socket.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);
                        if (read == 0) return;
                        lengthBytesRead += read;
                    }
                    var length = BitConverter.ToInt32(lengthData, 0);
                    var imageData = new byte[length];
                    var imageBytesRead = 0;
                    while (imageBytesRead < imageData.Length)
                    {
                        var read = socket.Receive(imageData, imageBytesRead, imageData.Length - imageBytesRead, SocketFlags.None);
                        if (read == 0) return;
                        imageBytesRead += read;
                    }

                    using (var stream = new MemoryStream(imageData))
                    {
                        var bitmap = new Bitmap(stream);
                        Dispatcher.Invoke(new ImageCompleteDelegate(ImageComplete), new object[] { bitmap });
                        stream.Dispose();
                        bitmap.Dispose();
                    }

            }
            socket.Disconnect(false);
        }
    }

    public static System.Windows.Media.Brush CreateBrushFromBitmap(Bitmap bmp)
    {
        return new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
    }
    private delegate void ImageCompleteDelegate(Bitmap bitmap);
    private void ImageComplete(Bitmap bitmap)
    {
        if (_buffer != null)
        {
            _buffer = null;
        }
        _buffer = new Bitmap(bitmap);
        bitmap.Dispose();
        //ScreenShotG is a Grid Element inside the XAML
        ScreenShotG.Background = CreateBrushFromBitmap(_buffer);
    }
like image 341
lior y Avatar asked Mar 25 '26 00:03

lior y


1 Answers

I finally fixed it after many hours of researching and testing. The problem was in the following code :

public static System.Windows.Media.Brush CreateBrushFromBitmap(Bitmap bmp)
{
    return new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
}

The solution was :

public static System.Windows.Media.Brush CreateBrushFromBitmap(Bitmap bmp)
{
     IntPtr hBitMap = bmp.GetHbitmap();
     ImageBrush b = new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
     DeleteObject(hBitMap);
     return b;
}
like image 164
lior y Avatar answered Mar 26 '26 12:03

lior y



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!