2012-04-13 22 views
3

我可以使用具有部分(屏蔽)透明度的GDIPlus在TImage中顯示32位圖像,但alpha值爲0或255沒有值之間。我已經嘗試加載PngImage,32位位圖和圖標都產生相同的...蒙面透明度,但不是完全透明。是否可以使用TImage和GDIPlus打開圖像並保持完全透明

是否有另一種方法讓TImage能夠顯示具有完全透明度的GDI +圖形,如所需結果圖像所示?

enter image description here GDI加開

enter image description here 所需的結果後

procedure TFormMain.Open1Click (Sender: TObject); 
// Load a GDIPlus Bitmap into TImage 
var 
    GPBitmap: TGPBitmap; 
    iHBitmap: HBITMAP; 
    iStatus: TStatus; 
const 
    TRANS_COLOR = clBlack; 
begin 

    if OpenPictureDialog1.Execute then 
    begin 

    FilePath := OpenPictureDialog1.FileName; 
    begin 

     GPBitmap := TGpBitmap.Create (FilePath); 
     try 

     iStatus := GPBitmap.GetHBITMAP (aclBlack, iHBitmap); 
     // As best as I can determine from the internet, the GetHBitmap which is needed to assign a GPbitmap to TImage 
     // does not hold an alphachannel, so loaded images alpha are either 0 or 255, but drawing with alphachannel values does work. 
     if iStatus = Ok then 
     begin 

      Image1.Picture.Bitmap.Handle := iHBitmap; 
      Image1.Picture.Bitmap.TransparentColor := Image1.Picture.Bitmap.Canvas.Pixels [ 0, Image1.Picture.Bitmap.Height - 1 ]; 
      StatusBar1.Panels [ 0 ].Text := FileCtrl.MinimizeName (ExtractFileDir (FilePath), Canvas, 200); // Folder 
      StatusBar1.Panels [ 1 ].Text := FileCtrl.MinimizeName (ExtractFileName (FilePath), Canvas, 75); // Filename 
      StatusBar1.Panels [ 2 ].Text := 'Width: ' + IntegerToString (Image1.Picture.Bitmap.Width); // Width 
      StatusBar1.Panels [ 3 ].Text := 'Height: ' + IntegerToString (Image1.Picture.Bitmap.Height); // Height 
      StatusBar1.Panels [ 4 ].Text := BitDepthToColorString (GetPixelFormatSize (GPBitmap.GetPixelFormat)); // Bitdepth 

      Image1.Refresh; 

     end; 

     finally 
     GPBitmap.Free; 
     end; 

    end; 

    end; 

end; 
+0

不確定如果我對TImage(不是使用GDI +渲染到您的畫布)與TGPBitmap無法正常工作感到驚訝。相反,請查看這裏的其他問題,以顯示如何將帶​​有透明度的Png加載到TImage中。這個問題就像是在問爲什麼我的Commodore 64不理解IP V6。 – 2012-04-13 15:12:29

+0

@沃倫 - 我搜查了但沒有發現任何顯示如何解決這個問題。你有任何鏈接? TImage或HBitmap是否會導致問題? – Bill 2012-04-13 15:37:34

+0

看到這個問題的一種方式,「TImage.Picture'的PNG支持可能被破壞。您可能安裝了僞造組件集或安裝了替代PNG組件集,實際上是對Delphi/VCL PNG的支持。看到這個鏈接:http://stackoverflow.com/questions/7163594/a-button-control-and-underlying-replacement-for-tbitmap-that-properly-handles-pn – 2012-04-13 16:39:36

回答

1

你需要做的是選擇背景顏色將與面融合的圖像被描繪時,被轉移。即而不是

iStatus := GPBitmap.GetHBITMAP (aclBlack, iHBitmap); 

使用

iStatus := GPBitmap.GetHBITMAP(ColorRefToARGB(ColorToRGB(clBtnFace)), iHBitmap); 

如果圖像是例如默認有色形式。

此時您已經知道該圖像沒有Alpha通道,但已使用合適的顏色將其平放。 「TBitmap」的Transparent..屬性對部分透明度無幫助。如果您將TBitmap的屬性設置爲Transparent,那麼當您將其放置在不同的彩色背景上時,它仍然會像您的問題的第一個圖像,可能會帶有更好的邊緣顏色。無論如何,如果您使用它,請不要忘記將'TransparentColor'設置爲'clBtnFace'而不是'clBlack'。

+0

我嘗試了你的建議,但仍然無法使用它。請張貼您用來打開圖片的代碼? – Bill 2012-04-14 12:49:32

+0

@Bill - 你沒有看到任何區別?我使用了你發佈的代碼。說,如果你在你的原始代碼中使用'aclRed'而不是aclBlack不是圖片的邊緣變成紅色? – 2012-04-14 13:39:42

+0

好吧,它現在正在爲png和ico工作,但不是bmp。 32位位圖根本不顯示任何透明度。我stll無法獲得光標下的alpha值。你知道怎麼做嗎? – Bill 2012-04-14 16:04:28