2012-03-29 6 views
4

Mit Delphi 2010 bekommen Sie mitWie das Pixelformat oder bitdepth von TPngImage

TJPEGImage (Image.Picture.Graphic).PixelFormat 

Gibt es einen Weg, um das Pixelformat einer jpg-Datei bekommen können das Pixelformat oder bitdepth von TPngImage zu bekommen?

habe ich versucht, diese aber es gibt falsche bitdepth:

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

Antwort

6

Sie das BitDepth Feld

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

und mit dem ColorType Feld können Sie wirte eine Funktion wie diese

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; 
verwenden müssen

und verwenden wie so

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

Aber wenn ein ARGB32-Bit-Png geladen wird IntToStr (TPNGImage (Image1.Picture.Graphic) .Header.BitDepth) gibt 8 statt 32 zurück? – Bill

+1

Oops @UweRaabe gibt Ihnen die wichtigsten Einblicke, während ich meine aktualisierte Antwort schreibe. – RRUZ

6

Als Rodrigo wies darauf hin, ist Header.BitDepth der Wert zu verwenden. Der Haken ist, dass Sie es abhängig vom ColorType interpretieren müssen. Sie können einige Hinweise in den Kommentaren innerhalb Funktion BytesForPixels in PngImage.pas finden:

{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; 

Wie Sie sehen, für ARGB (= COLOR_RGBALPHA) der bitdepth Wert für jede Farbe Teil genommen wird und der Alpha-Wert einzeln. So ergibt BitDepth = 8 einen 32-Bit-Wert für jedes Pixel.

Verwandte Themen