2013-07-12 9 views
3

알파 채널이있는 반투명 이미지가 포함 된 TBitmap이 있습니다 (이 예에서는 TPngImage에서 가져옴). 나는 TestIn.bmp 파일이은 TBitmap을 저장하고 모든 이미지 뷰어를 열 때TImage에 알파 채널이있는 TBitmap을 올바르게 표시하는 방법은 무엇입니까?

var 
    SourceBitmap: TBitmap; 
    PngImage: TPngImage; 
begin 
    PngImage := TPngImage.Create(); 
    SourceBitmap := TBitmap.Create(); 
    try 
    PngImage.LoadFromFile('ImgSmallTransparent.png'); 
    SourceBitmap.Assign(PngImage); 
    SourceBitmap.SaveToFile('TestIn.bmp'); 
    imgSource.Picture.Assign(SourceBitmap); 
    finally 
    PngImage.Free(); 
    SourceBitmap.Free(); 
    end; 

, 나는 투명성을 볼 수 있습니다. 그러나 TImage에 할당하면 투명한 픽셀이 검은 색으로 표시됩니다 (TImage는 Transparent = True 임).

투명도가있는 TBitmap을 TImage에 올바르게 표시하는 방법은 무엇입니까?

+0

복사 및 실제 코드를 붙여 부품 바랍니다 표시. – TLama

+0

@ TLama code added – Andrew

+0

Doublebuffered = true이고 이미지가 투명하게 설정되지 않은 경우 기본 설정의 wincontrol 중 아무 것도 예상대로 작동하지 않습니다. – bummi

답변

5

imgSource에 Transparent = false를 사용하면 표시된 코드가 내 시스템에서 올바르게 작동합니다.
파일에서 비트 맵을로드하면 검정색 픽셀로 동작을 재현 할 수 있습니다.

다른 설정에 영향 enter image description here enter image description here enter image description here enter image description here

procedure TForm3.SetAlphaFormatClick(Sender: TObject); 
begin 
    if SetAlphaFormat.Checked then 
    ToggleImage.Picture.Bitmap.alphaformat := afDefined 
    else 
    ToggleImage.Picture.Bitmap.alphaformat := afIgnored; 
end; 

procedure TForm3.SetImageTransparentClick(Sender: TObject); 
begin 
    ToggleImage.Transparent := SetImageTransparent.Checked; 
    Image1.Transparent := SetImageTransparent.Checked; 
end; 

procedure TForm3.LoadPngTransform2BitmapClick(Sender: TObject); 
Const 
    C_ThePNG = 'C:\temp\test1.png'; 
    C_TheBitMap = 'C:\temp\TestIn.bmp'; 
var 
    SourceBitmap, TestBitmap: TBitmap; 
    pngImage: TPngImage; 
begin 

    Image1.Transparent := SetImageTransparent.Checked; 
    pngImage := TPngImage.Create; 
    SourceBitmap := TBitmap.Create; 
    TestBitmap := TBitmap.Create; 
    try 
    pngImage.LoadFromFile(C_ThePNG); 
    SourceBitmap.Assign(pngImage); 

    {v1 with this version without the marked* part, I get the behavoir you described 
     SourceBitmap.SaveToFile(C_TheBitMap); 
     TestBitmap.LoadFromFile(C_TheBitMap); 
     TestBitmap.PixelFormat := pf32Bit; 
     TestBitmap.HandleType := bmDIB; 
     TestBitmap.alphaformat := afDefined; //* 
     Image1.Picture.Assign(TestBitmap); 
    } 
    //> v2 
    SourceBitmap.SaveToFile(C_TheBitMap); 
    SourceBitmap.PixelFormat := pf32Bit; 
    SourceBitmap.HandleType := bmDIB; 
    SourceBitmap.alphaformat := afDefined; 
    Image1.Picture.Assign(SourceBitmap); 
    //< v2 
    finally 
    pngImage.Free; 
    SourceBitmap.Free; 
    TestBitmap.Free; 
    end; 
end; 
+1

감사합니다. TImage 투명도가 비활성화되었으므로 이제 작동합니다! – Andrew