如何将读入的各种格式的文件在内存中转换成bmp文件?(100分)

  • 主题发起人 主题发起人 microluck
  • 开始时间 开始时间
M

microluck

Unregistered / Unconfirmed
GUEST, unregistred user!
比如:读入的是一个jpg,要在内存中转换成bmp,
读入的是一个gif,tif,....都能在内存中转换成bmp.
然后我想做出显示特效
请各位指点,谢谢了.
 
比如说你放了一个TImage,里面加载了一个jpg文件,程序运行后,在屏幕上显示出来了,那它已经在内存里了呀。
如果是bmp、jpg以外的格式,要用第三方控件才行。
 
procedure Jpg2Bmp(const source,dest:string);
var
MyJpeg: TJpegImage;
bmp: Tbitmap;
begin
bmp:=tbitmap.Create;
MyJpeg:= TJpegImage.Create;
try
myjpeg.LoadFromFile(source);
bmp.Assign(myjpeg);
bmp.SaveToFile(dest);
finally
bmp.free;
myjpeg.Free;
end;
end;

procedure Bmp2Jpg(const source,dest:string;const scale:byte);
var
MyJpeg: TJpegImage;
Image1: TImage;
begin
Image1:= TImage.Create(application);
MyJpeg:= TJpegImage.Create;
try
Image1.Picture.Bitmap.LoadFromFile(source);
MyJpeg.Assign(Image1.Picture.Bitmap);
MyJpeg.CompressionQuality:=scale;
MyJpeg.Compress;
MyJpeg.SaveToFile(dest);
finally
image1.free;
myjpeg.Free;
end;
end;
 
//只需调用LoadGraphicsFile函数就可以了,至于bmp->tiff有些麻烦

unit GraphicsConversionsLibrary;

interface

uses
Graphics;

function LoadGraphicsFile(const Filename: string): TBitmap;

implementation

uses
GIFImage, // TGIFImage
JPEG, // TJPEGImage
SysUtils, // FileExists
readtiff;

// Create TBitmap from BMP, JPG, WMF, EMF or GIF disk file.
// Could be easily extended to other image types.

function LoadGraphicsFile(const Filename: string): TBitmap;
var
Extension: string;
GIFImage: TGIFImage;
Icon: TIcon;
JPEGImage: TJPEGImage;
Metafile: TMetafile;
begin
RESULT := nil; // In case anything goes wrong

if FileExists(Filename) then
begin
Extension := UpperCase(COPY(Filename, LENGTH(Filename) - 2, 3));

// Quick and dirty check that file type is OK
if ((Extension = 'BMP') or (Extension = 'EMF') or

(Extension = 'GIF') or (Extension = 'TIF') or

(Extension = 'ICO') or (Extension = 'JPG') or (Extension = 'WMF')) then

RESULT := TBitmap.Create;

// BMP File -- no additional work to get TBitmap
if Extension = 'BMP' then
RESULT.LoadFromFile(Filename);

// GIF File
if Extension = 'GIF' then
begin
GIFImage := TGIFImage.Create;
try
GIFImage.LoadFromFile(Filename);
RESULT.Height := GIFImage.Height;
RESULT.Width := GIFImage.Width;
RESULT.PixelFormat := pf24bit;
RESULT.Canvas.Draw(0, 0, GIFImage)
finally
GIFImage.Free
end
end;
if Extension = 'TIF' then
begin
try
LoadTiffFromFile(FileName, result);
finally
end;
end;

// ICO File
if Extension = 'ICO' then
begin
Icon := TIcon.Create;
try
try
Icon.LoadFromFile(Filename);
RESULT.Height := Icon.Height;
RESULT.Width := Icon.Width;
RESULT.PixelFormat := pf24bit; // avoid palette problems
RESULT.Canvas.Draw(0, 0, Icon)
except
// Ignore problem icons, e.g., Stream read errors
end;

finally
Icon.Free
end
end;

// JPG File
if Extension = 'JPG' then
begin
JPEGImage := TJPEGImage.Create;
try
JPEGImage.LoadFromFile(Filename);
RESULT.Height := JPEGImage.Height;
RESULT.Width := JPEGImage.Width;
RESULT.PixelFormat := pf24bit;
RESULT.Canvas.Draw(0, 0, JPEGImage)
finally
JPEGImage.Free
end
end;

// Windows Metafiles, WMF or EMF
if (Extension = 'WMF') or
(Extension = 'EMF') then
begin
Metafile := TMetafile.Create;
try
Metafile.LoadFromFile(Filename);
RESULT.Height := Metafile.Height;
RESULT.Width := Metafile.Width;
RESULT.PixelFormat := pf24bit; // avoid palette problems
RESULT.Canvas.Draw(0, 0, Metafile)
finally
Metafile.Free
end
end;

end;

// If Graphic is missing or invalid, create the "Red X"
if RESULT = nil then
begin
RESULT.Height := 32;
RESULT.Width := 32;
RESULT.PixelFormat := pf24bit;
with RESULT.Canvas do
begin
Pen.Color := clRed;
Pen.Width := 3;
MoveTo(2, 2);
LineTo(29, 29);

MoveTo(2, 29);
LineTo(29, 2);
end
end

end {LoadGraphicFile};

end.
 
huazai,你的源码很有用,可是其中没有readtiff;这个单元呀
请您帮我找一下,好吗?
 
后退
顶部