2017-05-05 9 views
1

C#とVisual StudioからDelphi 10.1には非常に難しいですが、一部のパフォーマンスは非常に重要です。私はDelphiで長年(10年以上)私はブロックされています。ImageListを作成して実行時に作成する

実行時にImageListを作成してシングルトンオブジェクトに格納する必要がありますが、メモリを読み取っている間に例外が発生するため、その処理を行うことはできません。ここで

は、私のコードの抜粋である:ここ

ImagesRessource = class 
private 
    _owner: TComponent; 
    _imageList: TimageList; 
    _man24: TPngImage; 
    constructor Create; 
    function GetBmpOf(png: TPngImage): TBitmap; 
public 
    procedure Initialize(own: TComponent); 
end; 

implementation 

constructor ImagesRessource.Create; 
begin 
    ; 
end; 

procedure ImagesRessource.Initialize(owner: TComponent); 
var 
    bmp: TBitmap; 
    RS : TResourceStream; 
begin 
    try 
    _man24 := TPngImage.Create; 
    RS := TResourceStream.Create(hInstance, 'man_24', RT_RCDATA); 
    _man24.LoadFromStream(RS); 
    bmp := GetBmpOf(_man24); 
    _imageList := TimageList.Create(owner); 
    _imageList.Width := 24; 
    _imageList.Height := 24; 
    _imageList.AddMasked(Bmp, Bmp.TransparentColor); // exception read memory here 
    except 
    raise; 
    end; 
end; 

function ImagesRessource.GetBmpOf(png: TPngImage): TBitmap; 
var 
    bmp: TBitmap; 
begin 
    bmp := TBitmap.Create; 
    bmp.Width := png.Width; 
    bmp.Height := png.Height; 
    png.Draw(bmp.Canvas, bmp.Canvas.ClipRect); 
end; 

何が悪いですか?

+0

ImageListを作成し、ResourceStreamからイメージを読み取り、イメージリストに格納する必要があるのはなぜですか? ImageListをフォームまたはデータモジュールに入れ、デザイン時にイメージを追加する必要があります。 – Kohull

+3

@Kohullを行うには、リソースを使用することが賢明です。リビジョン管理下でアセットを別のファイルに保存することができます。一旦それをdfmファイルに入れると、維持するのがずっと難しくなります。 –

答えて

1

GetBmpOfからは何も返しません。もしResult変数に割り当てる必要があります。)

function ImagesRessource.GetBmpOf(png: TPngImage): TBitmap; 
begin 
    Result := TBitmap.Create; 
    Result.Width := png.Width; 
    Result.Height := png.Height; 
    png.Draw(Result.Canvas, Result.Canvas.ClipRect); 
end; 

はまた、いずれの場合にも、ローカル変数でなければならないPNG画像_man24を、リーク。あなたは24のサイズをいくつかの場所でハードコードしますが、他の場所ではハードコーディングしません。ブロックを除いてあなたの試みは無意味です。

+0

ありがとうございました。 –

関連する問題