请问如何在Delphi中读写.bmp,.jpg,.gif等图形文件?(不用TBitmap 等)(100分)

  • 主题发起人 主题发起人 weibin
  • 开始时间 开始时间
W

weibin

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何在Delphi中读写.bmp,.jpg,.gif等图形文件?(不用TBitmap 等)
 
那就用Picture的LoadFromFile吧。
 
那用什么呢? 直接用内存,分析文件格式?
为什么不要TBITMAP? 给个理由先.....
 
分析文件格式
 
可以采用流的技术tstream,流可以进行压缩,放大等处理,功能很酷
具体请参考delphi的联机帮助
编写程序时请小心tstream的参数的使用,很容易出错。
 
那就用流把
 
给个理由塞,为什么不用 TBitmap。流技术也可以,但比较麻烦。
 
什么是流技术呀
 
请仔细看LoadAnyImageToBMP, 下面代码有点多,慢慢看吧

unit hhx_files;

interface
uses SysUtils,
classes,
Windows,
JPEG,
Graphics,
io_files,
hhx_basedef,
GIFImage,
hhx_errors,
hhx_tiff,
hhx_tga,
hhx_pcd,
hhx_pcx;
{$R-}

//---------------------------------------------------------------------------
//
// Global
//
// -----------------------------------------------------------------------------

//ModeA and ModeB are reserved for parameters passed to the converters

procedure LoadAnyImageToBMP(Bitmap:TBitmap;Filename:String;AsThumb:Boolean;ModeA,ModeB:Integer);
procedure LoadJPEGExplicit(Graphic:TGraphic;FileName:String;AsThumb:Boolean);
procedure SaveBMPToAnyFile(Bitmap:TBitmap;Filename:String;ModeA,ModeB:Integer);
procedure SetJPEGParams(PixelFormat:TJPEGPixelFormat;Performance:TJPEGPerformance;
Scale:TJPEgScale;Grayscale:Boolean;Smoothing:Boolean;ProgressiveDispl:Boolean;SaveQuality:TJPEGQualityRange);
procedure SetJPEGDefaults;

// ** Internal procedures
procedure _LoadJPEG(Bitmap:TBitmap;FileName:String;AsThumb:Boolean);
procedure _LoadBMP(Bitmap:TBitmap;FileName:String);
procedure _LoadPCX(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
procedure _LoadTGA(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
procedure _LoadWMF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
procedure _LoadPCD(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
procedure _LoadGIF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
procedure _SaveGIF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
procedure _SaveJPEG(Bitmap:TBitmap;FileName:String);
procedure _SaveBMP(Bitmap:TBitmap;FileName:String);
procedure _LoadTIFF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
procedure _SaveTIFF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
procedure _SavePCX(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
procedure _SaveTGA(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);

function GetProcessedWidth:Integer;
function GetProcessedHeight:Integer;
function GetProcessedCRC:Integer;
function GetProcessedDPI:Integer;
function GetProcessedPlanes:Integer;
function GetGenerateCRCState:Boolean;
function SetGenerateCRCState(State:Boolean):Boolean;

PROCEDURE PrintBitmap(Canvas: TCanvas; DestRect: TRect; Bitmap: TBitmap);

Var _defJPEGPixelFormat:TJPEGPixelformat;
_defJPEGPerformance:TJPEGPerformance;
_defJPEGScale: TJPEGScale;
_defJPEGGrayScale: Boolean;
_defJPEGPrgrDisplay:Boolean;
_defJPEGSmoothing: Boolean;
_defJPEGProgressEvent:TProgressEvent;
_defJPEGQualityRange:TJPEGQualityRange;
_ProcessedImgWidth,
_ProcessedImgHeight,
_ProcessedImgDPI,
_ProcessedImgPlanes:Integer;
_GenerateCheckSums:Boolean;
_ProcessedCRC32:DWORD;




const _OpenFilterString ='All Imagefiles|*.bmp;*.emf;*.ico;*.jpg;*.pcd;*.pcx;*.tga;*.wmf|'+
'Bitmap (*.bmp)|*.bmp|'+
'Extended Metafile (*.emf)|*.emf|'+
'Icon (*.ico)|*.ico|'+
'JPEG (*.jpg)|*.jpg|'+
'GIF (*.gif)|*.gif|'+
'Photo CD (*.pcd)|*.pcd|'+
'PCX (*.pcx)|*.pcx|'+
'Targa (*.tga)|*.tga|'+
'TIFF (*.tif)|*.tif|'+
'Metafile (*.wmf)|*.wmf|';
const _SaveFilterString = 'Bitmap (*.bmp)|*.bmp|'+
'Extended Metafile (*.emf)|*.emf|'+
'Icon (*.ico)|*.ico|'+
'JPEG (*.jpg)|*.jpg|'+
'GIF (*.gif)|*.gif|'+
'PCX (*.pcx)|*.pcx|'+
'Targa (*.tga)|*.tga|'+
'TIFF (*.tif)|*.tif|'+
'Metafile (*.wmf)|*.wmf|';
const _GfxFileMask = '*.jpg;*.jpeg;*.bmp;*.tga;*.pcx;*.pcd'+
';*.gif'+
';*.tif';


implementation





//***********************************************************
//
// Global
//
//***********************************************************


//---------------------------------------------------------------------------
//
// LoadAnyImageToBMP
//
//-----------------------------------------------------------------------------
procedure LoadAnyImageToBMP(Bitmap:TBitmap;Filename:String;AsThumb:Boolean;ModeA,ModeB:Integer);
var FileExt:String;
begin

_ProcessedImgWidth:=0;
_ProcessedImgHeight:=0;
_ProcessedCRC32:=0;
if FileExists(FileName) then
begin
FileExt:=LowerCase(ExtractFileExt(FileName));
if FileExt='.bmp' then _LoadBMP(Bitmap,FileName) else
if FileExt='.jpg' then _LoadJPEG(Bitmap,FileName,AsThumb) else
if FileExt='.gif' then _LoadGIF(Bitmap,FileName,ModeA,ModeB) else
if FileExt='.pcx' then _LoadPCX(Bitmap,FileName,ModeA,ModeB) else
if FileExt='.tga' then _LoadTGA(Bitmap,FileName,ModeA,ModeB) else
if FileExt='.wmf' then _LoadWMF(Bitmap,FileName,ModeA,ModeB) else
if FileExt='.pcd' then _LoadPCD(Bitmap,FileName,ModeA,ModeB) else
if FileExt='.tif' then _LoadTIFF(Bitmap,FileName,ModeA,ModeB) else

if _GenerateCheckSums then _ProcessedCRC32:=CRC32File(FileName);
end;
end;

//---------------------------------------------------------------------------
//
// SaveBMPToAnyFile
//
//-----------------------------------------------------------------------------
procedure SaveBMPToAnyFile(Bitmap:TBitmap;Filename:String;ModeA,ModeB:Integer);
var FileExt:String;
begin
FileExt:=LowerCase(ExtractFileExt(FileName));
if FileExt='.bmp' then _SaveBMP(Bitmap,FileName) else
if (FileExt='.jpg') or (FileExt='.jpeg') then _SaveJPEG(Bitmap,FileName) else
if FileExt='.gif' then _SaveGIF(Bitmap,FileName,ModeA,ModeB) else
if FileExt='.pcx' then _SavePCX(Bitmap,FileName,ModeA,ModeB) else
if FileExt='.tif' then _SaveTIFF(Bitmap,FileName,ModeA,ModeB) else
if FileExt='.tga' then _SaveTGA(Bitmap,FileName,ModeA,ModeB);
end;

//---------------------------------------------------------------------------
//
// LoadJPEGExplicit
//
//-----------------------------------------------------------------------------
procedure LoadJPEGExplicit(Graphic:TGraphic;FileName:String;AsThumb:Boolean);
var FileExt:String;
begin
FileExt:=LowerCase(ExtractFileExt(FileName));
if (FileExt='.jpg') or (FileExt='.jpeg') then
begin
Graphic.LoadFromFile(FileName);
end;
end;

//---------------------------------------------------------------------------
//
// SetJPEGParams
//
//-----------------------------------------------------------------------------
procedure SetJPEGParams(PixelFormat:TJPEGPixelFormat;Performance:TJPEGPerformance;
Scale:TJPEgScale;Grayscale:Boolean;Smoothing:Boolean;
ProgressiveDispl:Boolean;SaveQuality:TJPEGQualityRange);
begin
_defJPEGSmoothing:=Smoothing;
_defJPEGPrgrDisplay:=ProgressiveDispl;
_defJPEGGrayScale:=Grayscale;
_defJPEGScale:=Scale;
_defJPEGPerformance:=Performance;
_defJPEGPixelFormat:=PixelFormat;
_defJPEGQualityRange:=SaveQuality;
end;

//---------------------------------------------------------------------------
//
// SetJPEGDefaults
//
//---------------------------------------------------------------------------
procedure SetJPEGDefaults;
begin
_defJPEGPixelFormat:=jf24Bit;
_defJPEGPerformance:=jpBestQuality;
_defJPEGScale :=jsFullSize;
_defJPEGGrayScale :=False;
_defJPEGPrgrDisplay:=False;
_defJPEGSmoothing :=True;
_defJPEGProgressEvent:=nil;
_defJPEGQualityRange:=90;
_ProcessedImgWidth :=0;
_ProcessedImgHeight :=0;
_ProcessedImgDPI :=0;
_ProcessedImgPlanes :=0;
end;

//---------------------------------------------------------------------------
//
// _LoadJPEG
//
//---------------------------------------------------------------------------
procedure _LoadJPEG(Bitmap:TBitmap;FileName:String;AsThumb:Boolean);
var TempJPEG:TJPEGImage;
begin
TempJPEG:=TJPEGImage.Create;
try
if Assigned(_defJPEGProgressEvent) then TempJPEG.OnProgress:=_defJPEGProgressEvent;
try
TempJPEG.LoadFromFile(FileName);
TempJPEG.PixelFormat:=jf24Bit;
TempJPEG.Grayscale:=_defJPEGGrayScale;
if (TempJPEG.Width>0) and (TempJPEG.Height>0) then
with TempJPEG do
begin
_ProcessedImgWidth:=Width;
_ProcessedImgHeight:=Height;
Case PixelFormat of
jf24Bit: _ProcessedImgPlanes:=24;
jf8Bit: _ProcessedImgPlanes:=8;
end;
PixelFormat:=_defJPEGPixelFormat;
ProgressiveDisplay:=_defJPEGPrgrDisplay;
if AsThumb then
begin
if (Width/8)>70 then Scale:=jsEighth else
if (Width/4)>70 then Scale:=jsQuarter else
if (Width/2)>70 then Scale:=jsHalf;
end
else Scale:=_defJPEGScale;
Smoothing:=_defJPEGSmoothing;
Performance:=_defJPEGPerformance;
Bitmap.Width:=TempJPEG.Width;
Bitmap.Height:=TempJPEG.Height;
case TempJPEG.PixelFormat of
jf24Bit:begin
Bitmap.PixelFormat:=pf24Bit;
Bitmap.Assign(TJPEGImage(TempJPEG));
end;
jf8Bit: begin
TempJPEG.DIBNEeded;
Bitmap.PixelFormat:=pf24Bit;
Bitmap.Assign(TJPEGImage(TempJPEG));
end;
end;
end else
begin
Bitmap.Width:=1;
Bitmap.Height:=1;
end;
except
Bitmap.Width:=1;
Bitmap.Height:=1;
TempJPEG.Free;
if GFXRaiseErrors then raise EGraphicFormat.Create('JPEG loading error');
GFXFileErrorList.Add('JPEG loading error');
end;
finally
TempJPEG.Free;
end;

end;

//---------------------------------------------------------------------------
//
// _LoadBMP
//
//---------------------------------------------------------------------------
procedure _LoadBMP(Bitmap:TBitmap;FileName:String);
var tempBitmap:TBitmap;
begin
tempBitmap:=TBitmap.Create;
try
tempBitmap.LoadFromFile(FileName);
_ProcessedImgWidth:=tempBitmap.Width;
_ProcessedImgHeight:=tempBitmap.Height;
Bitmap.Assign(tempBitmap);
finally
tempBitmap.Free;
end;
end;

//---------------------------------------------------------------------------
//
// _LoadGIF
//
//---------------------------------------------------------------------------
procedure _LoadGIF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
var GifImage:TGifImage;
begin
GIFImage := TGIFImage.Create;
GifImage.Transparent:=False;
TRY
try
GIFImage.LoadFromFile(Filename);
except
if GFXRaiseErrors then raise EGraphicFormat.Create('GIF loading error');
end;
Bitmap.Height := GIFImage.Height;
Bitmap.Width := GIFImage.Width;
Bitmap.PixelFormat := pf24bit;
Bitmap.Canvas.Draw(0,0, GIFImage.Bitmap);
_ProcessedImgWidth:=Bitmap.Width;
_ProcessedImgHeight:=Bitmap.Height;
FINALLY
GIFImage.Free
end;
end;

//---------------------------------------------------------------------------
//
// _LoadTIFF
//
//---------------------------------------------------------------------------
procedure _LoadTIFF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
var aTIFF:TTIFFFile;
begin
aTIFF:=TTIFFFile.Create;
aTIFF.LoadFromFile(FIleName);
Bitmap.Assign(aTIFF.Bitmap);
_ProcessedImgWidth:=Bitmap.Width;
_ProcessedImgHeight:=Bitmap.Height;
aTIFF.Free;
end;

procedure _LoadPCX(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
var aPcx:TPCXFile;
begin
aPcx:=TPCXFile.Create;
aPcx.LoadFromFile(FIleName);
Bitmap.Assign(aPcx.Bitmap);
_ProcessedImgWidth:=Bitmap.Width;
_ProcessedImgHeight:=Bitmap.Height;
aPcx.Free;
end;

procedure _LoadTGA(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
var aTga:TTGAFile;
begin
atga:=TtgaFile.Create;
atga.LoadFromFile(FIleName);
Bitmap.Assign(atga.Bitmap);
_ProcessedImgWidth:=Bitmap.Width;
_ProcessedImgHeight:=Bitmap.Height;
atga.Free;
end;

//---------------------------------------------------------------------------
//
// _LoadWMF
//
//---------------------------------------------------------------------------
procedure _LoadWMF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
var aWMF:TMetaFile;
begin
aWMF:=TMetaFile.Create ;
aWMF.LoadFromFile(FIleName);
Bitmap.Width:=aWMF.Width;
Bitmap.Height:=aWMF.Height;
_ProcessedImgWidth:=Bitmap.Width;
_ProcessedImgHeight:=Bitmap.Height;
Bitmap.Canvas.Draw(0,0,aWMF);
aWMF.Free;
end;

procedure _LoadPCD(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
var aPCD:TPCDFile;
begin
aPCD:=TPCDFile.Create;
if ModeA=0 then ModeA:=3;
PCDSize:=ModeA;
aPCD.LoadFromFile(FIleName);
Bitmap.Assign(aPCD.Bitmap);
_ProcessedImgWidth:=Bitmap.Width;
_ProcessedImgHeight:=Bitmap.Height;
aPCD.Free;
end;

//---------------------------------------------------------------------------
//
// _SaveBMP
//
//---------------------------------------------------------------------------
procedure _SaveBMP(Bitmap:TBitmap;FileName:String);
begin
Bitmap.SaveToFile(FileName);
end;

//---------------------------------------------------------------------------
//
// _SaveJPEG
//
//---------------------------------------------------------------------------
procedure _SaveJPEG(Bitmap:TBitmap;FileName:String);
Var TempJPEG:TJPegImage;
begin
TempJPEG:=TJPEGImage.Create;
try
TempJPEG.Assign(Bitmap);
TempJPEG.CompressionQuality:=_defJPEGQualityRange;
TempJPEG.SaveToFile(FileName);
finally
TempJPEG.Free;
end;
end;

//---------------------------------------------------------------------------
//
// _SaveGIF
//
//---------------------------------------------------------------------------
procedure _SaveGIF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
begin
//Not implemented yet
end;

//---------------------------------------------------------------------------
//
// _SaveTIFF
//
//---------------------------------------------------------------------------
procedure _SaveTIFF(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
var aTIFF:TTIFFFile;
begin
aTIFF:=TTIFFFile.Create;
aTIFF.AssignBitmap(Bitmap);
aTIFF.SaveToFile(FIleName);
aTIFF.Free;
end;

//---------------------------------------------------------------------------
//
// _SavePCX
//
//---------------------------------------------------------------------------
procedure _SavePCX(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
var aPcx:TPCXFile;
begin
aPcx:=TPCXFile.Create;
aPCX.AssignBitmap(Bitmap);
aPcx.SaveToFile(FIleName);
aPcx.Free;
end;

//---------------------------------------------------------------------------
//
// _SaveTGA
//
//---------------------------------------------------------------------------
procedure _SaveTGA(Bitmap:TBitmap;FileName:String;ModeA,ModeB:Integer);
var aTGA:TTGAFile;
begin
aTGA:=TTGAFile.Create;
aTGA.AssignBitmap(Bitmap);
aTGA.SaveToFile(FIleName);
aTGA.Free;
end;

//---------------------------------------------------------------------------
//
// GetProcessedWidth
//
//---------------------------------------------------------------------------
function GetProcessedWidth:Integer;
begin
GetProcessedWidth:=_ProcessedImgWidth;
end;

//---------------------------------------------------------------------------
//
// _GetProcessedHeight
//
//---------------------------------------------------------------------------
function GetProcessedHeight:Integer;
begin
GetProcessedHeight:=_ProcessedImgHeight;
end;

//---------------------------------------------------------------------------
//
// GetProcessedCRC
//
//---------------------------------------------------------------------------
function GetProcessedCRC:Integer;
begin
GetProcessedCRC:=_ProcessedCRC32;
end;

//---------------------------------------------------------------------------
//
// GetProcessedDPI
//
//---------------------------------------------------------------------------
function GetProcessedDPI:Integer;
begin
GetProcessedDPI:=_ProcessedImgDPI;
end;

//---------------------------------------------------------------------------
//
// GetProcessedPlanes
//
//---------------------------------------------------------------------------
function GetProcessedPlanes:Integer;
begin
GetProcessedPlanes:=_ProcessedImgPlanes;
end;

//---------------------------------------------------------------------------
//
// GetGenerateCRCState
//
//---------------------------------------------------------------------------
function GetGenerateCRCState:Boolean;
begin
GetGenerateCRCState:=_GenerateCheckSums;
end;

//---------------------------------------------------------------------------
//
// SetGenerateCRCState
//
//---------------------------------------------------------------------------
function SetGenerateCRCState(State:Boolean):Boolean;
begin
_GenerateCheckSums:=State;
SetGenerateCRCState:=_GenerateCheckSums;
end;


//---------------------------------------------------------------------------
//
// PrintBitmap
//
//---------------------------------------------------------------------------
PROCEDURE PrintBitmap(Canvas: TCanvas; DestRect: TRect; Bitmap: TBitmap);
VAR
BitmapHeader: pBitmapInfo;
BitmapImage : POINTER;
HeaderSize : DWORD;
ImageSize : DWORD;
BEGIN
GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
GetMem(BitmapHeader, HeaderSize);
GetMem(BitmapImage, ImageSize);
TRY
GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
StretchDIBits(Canvas.Handle,
DestRect.Left, DestRect.Top, // Destination Origin
DestRect.Right - DestRect.Left, // Destination Width
DestRect.Bottom - DestRect.Top, // Destination Height
0, 0, // Source Origin
Bitmap.Width, Bitmap.Height, // Source Width & Height
BitmapImage,
TBitmapInfo(BitmapHeader^),
DIB_RGB_COLORS,
SRCCOPY)
FINALLY
FreeMem(BitmapHeader);
FreeMem(BitmapImage)
END
END;

//initialization
// SetJPEGDefaults;
// _GenerateCheckSums:=False;
// _ProcessedCRC32:=0;

//finalization
// GFXFileErrorList.Free;

end.
















 
或者看这个

procedure TfrmColor.mnuFileOpenClick(Sender: TObject);
Var
jpeg: TJPEGImage;
bmp: TBitmap;

FileExt: string[4];
begin
if OpenDialog1.Execute then { Display Open dialog box }
begin
FileExt := AnsiUpperCase(ExtractFileExt(OpenDialog1.Filename));
if (FileExt = '.BMP') or (FileExt = '.ICO') or (FileExt = '.WMF') or
(FileExt = '.EMF') or (FileExt = '.JPG') then
begin
Image1.Picture.LoadFromFile(OpenDialog1.Filename);
if (FileExt = '.BMP') then
begin
Caption := Caption +
Format(' (%d x %d)', [Image1.Picture.Width, Image1.Picture.Height]);
Image1.Picture := Image1.Picture;
end
else
if (FileExt = '.JPG') then
begin
Caption := Caption +
Format(' (%d x %d)', [Image1.Picture.Width, Image1.Picture.Height]);
Image1.Picture := Image1.Picture;
jpeg:= TJPEGImage.Create;
try
jpeg.LoadFromFile(OpenDialog1.Filename);
try
image1.Picture.bitmap.Assign( jpeg );
// image1.Picture.bmp.SaveTofile( ChangeFileExt( filename, '.BMP' ));
finally
end;
finally
jpeg.free
end;

end
else
if FileExt = '.ICO' then
begin
Icon := Image1.Picture.Icon;
Image1.Picture.Icon := Icon;
end;
if (FileExt = '.WMF') or (FileExt = '.EMF') then
Image1.Picture.Metafile := Image1.Picture.Metafile;
end;
end;
end;
 
htw:应该很有价值的。
有谁试一下,行的话给分,我没时间了。:-)
 
[red]OOP![/red]
 
wa,写了这么多,怎么也得给点分
 
如果将一幅3434x2345的BMP图像转换JPG格式就会发生错误或很慢系统没反映
如何解决?
 
to htw:
到哪里下载 hhx_tiff,
hhx_tga,
hhx_pcd,
hhx_pcx;
等控件啊?
 
to htw:
你说的hhx_tiff,
hhx_tga,
hhx_pcd,
hhx_pcx;
这些单元哪里有下载?? 或请发给我! kiss2@yeah.net
谢谢!
 
我也想要这些单元 dodo505@sohu.com
 
后退
顶部