2012-03-29 53 views
4

的與2010年德爾福,你可以得到一個jpg文件的像素格式與如何獲得的像素格式或比特深度TPNGImage

TJPEGImage (Image.Picture.Graphic).PixelFormat 

有沒有辦法讓像素格式或比特深度TPNGImage的?

我試過,但它返回不正確的位深:

if Lowercase (ExtractFileExt (FPath)) = '.png' then 
    StatusBar1.Panels [ 4 ].Text := ' Color Depth: ' + IntToStr(TPNGImage (Image.Picture.Graphic).Header.ColorType) + '-bit'; 

回答

6

必須使用BitDepth

TPNGImage(Image.Picture.Graphic).Header.BitDepth) 

,並使用ColorType場可以WIRTE這樣的函數

function BitsForPixel(const AColorType, ABitDepth: Byte): Integer; 
begin 
    case AColorType of 
    COLOR_GRAYSCALEALPHA: Result := (ABitDepth * 2); 
    COLOR_RGB: Result := (ABitDepth * 3); 
    COLOR_RGBALPHA: Result := (ABitDepth * 4); 
    COLOR_GRAYSCALE, COLOR_PALETTE: Result := ABitDepth; 
    else 
     Result := 0; 
    end; 
end; 

,並使用像這樣

procedure TForm72.Button1Click(Sender: TObject); 
begin 
    ShowMessage(IntToStr(BitsForPixel(
    TPNGImage (Image1.Picture.Graphic).Header.ColorType, 
    TPNGImage (Image1.Picture.Graphic).Header.BitDepth 
    ))); 
end; 
+0

但是,當一個ARGB32位png被加載IntToStr(TPNGImage(Image1.Picture.Graphic).Header.BitDepth)返回8而不是32? – Bill 2012-03-29 16:40:39

+1

哎呀@UweRaabe爲您提供關鍵的見解,而我寫我更新的答案。 – RRUZ 2012-03-29 17:10:45

6

作爲羅德里戈指出,Header.BitDepth是使用的值。缺點是你必須根據ColorType來解釋它。您可能會發現裏面的功能BytesForPixels評論一些提示在PngImage.pas:

{Calculates number of bytes for the number of pixels using the} 
{color mode in the paramenter} 
function BytesForPixels(const Pixels: Integer; const ColorType, 
    BitDepth: Byte): Integer; 
begin 
    case ColorType of 
    {Palette and grayscale contains a single value, for palette} 
    {an value of size 2^bitdepth pointing to the palette index} 
    {and grayscale the value from 0 to 2^bitdepth with color intesity} 
    COLOR_GRAYSCALE, COLOR_PALETTE: 
     Result := (Pixels * BitDepth + 7) div 8; 
    {RGB contains 3 values R, G, B with size 2^bitdepth each} 
    COLOR_RGB: 
     Result := (Pixels * BitDepth * 3) div 8; 
    {Contains one value followed by alpha value booth size 2^bitdepth} 
    COLOR_GRAYSCALEALPHA: 
     Result := (Pixels * BitDepth * 2) div 8; 
    {Contains four values size 2^bitdepth, Red, Green, Blue and alpha} 
    COLOR_RGBALPHA: 
     Result := (Pixels * BitDepth * 4) div 8; 
    else 
     Result := 0; 
    end {case ColorType} 
end; 

正如你看到的,對於ARGB(= COLOR_RGBALPHA)位深值取每個顏色部分加上個別alpha值。所以位深度= 8將導致針對每個像素的32位的值。