内存泄漏加载imagebrush到grid.background

时间:2013-11-01 14:20:18

标签: c# wpf gridview

我有一个应用程序将屏幕截图发送到需要在WPF窗口中显示的另一个应用程序 除了每次添加新背景时代码在内存中建立的唯一问题之外,它的效果很好。

我该如何解决这个问题?

谢谢!

  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);
    }

1 个答案:

答案 0 :(得分:1)

经过数小时的研究和测试后,我终于修复了它。 问题出现在以下代码中:

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

解决方案是:

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;
}