zoukankan      html  css  js  c++  java
  • TWICImage.SaveToStream内存泄漏的解决办法

    这个BUG从2010到XE5一直没改.....只能自己写个函数来搞了

    uses
        ActiveX;
    
    procedure WICImageSaveToStream(AWICImage: TWICImage; AStream: TStream);
    var
      nEncoder: IWICBitmapEncoder;
      nFrame: IWICBitmapFrameEncode;
      nProps: IPropertyBag2;
      nLStreamAdapter: TStreamAdapter;
      nPixelFormat: TGUID;
      nLStream: IWICStream;
      nPalette: IWICPalette;
      nData: TMemoryStream;
    begin
      nData:=TMemoryStream.Create;
      nLStreamAdapter := TStreamAdapter.Create(nData);
      IUnknown(nLStreamAdapter)._AddRef;
      try
        nData.Clear;
        AWICImage.ImagingFactory.CreateStream(nLStream);
        nLStream.InitializeFromIStream(nLStreamAdapter);
        AWICImage.ImagingFactory.CreateEncoder(AWICImage.EncoderContainerFormat, guid_null, nEncoder);
        nEncoder.Initialize(nLStream, WICBitmapEncoderNoCache);
        nEncoder.CreateNewFrame(nFrame, nProps);
        nFrame.Initialize(nProps);
        AWICImage.Handle.GetPixelFormat(nPixelFormat);
        nFrame.SetPixelFormat(nPixelFormat);
        nFrame.SetSize(AWICImage.Width, AWICImage.Height);
        AWICImage.ImagingFactory.CreatePalette(nPalette);
        AWICImage.Handle.CopyPalette(nPalette);
        nFrame.SetPalette(nPalette);
        nFrame.WriteSource(AWICImage.Handle, nil);
        nFrame.Commit;
        nEncoder.Commit;
        nData.Position := 0;
        AStream.CopyFrom(nData, nData.Size);
      finally
        IUnknown(nLStreamAdapter)._Release; {相对于VCL代码 , 这里要释放}
        nData.Free;
      end;
    end;
  • 相关阅读:
    MMU讲解
    MiniCRT 64位 linux 系统移植记录:64位gcc的几点注意
    gprof使用介绍
    SQLite在多线程环境下的应用
    C++ Boost库简介
    GDB调试手册[转]
    data-ng-show 指令
    AngularJS THML DOM
    AngularJS 表格
    AngularJS XMLHttpRequest
  • 原文地址:https://www.cnblogs.com/lzl_17948876/p/3327878.html
Copyright © 2011-2022 走看看