Board index » delphi » How do i save a BMP as JPEG using jpeg.pas

How do i save a BMP as JPEG using jpeg.pas

Hi there

can anybody help me with this?

Thanx

Mariano

 

Re:How do i save a BMP as JPEG using jpeg.pas


I wrote a small interface for our core app, which takes a directory of JPegs
and converts them all into BMP's in a separate directory. I have cut &
pasted two routines from my code for you. You should be able to get the idea
from these.

Note, you will see a significant difference in size. I lowered the pixel
format to 8bit to help decrease the size of the BMP's, since I wasn't doing
anything dramatic that I needed nything better than that.

Shane Holmes
Delphi Function Junction
www.shane.holmes.net/dfj

procedure TForm1.ConvertJpgToBmp(const Jpg: TFileName; const BmpDir:
String);
var
 JpgImage: TJPEGImage;
 BmpImage: TBitMap;
begin
 JpgImage:= TJPEGImage.Create;
 JpgImage.LoadFromFile(Jpg);
 BmpImage:= TBitMap.Create;
 BmpImage.Assign(JpgImage);
 BmpImage.PixelFormat:= pf8Bit;
 BmpImage.SaveToFile(BmpDir+ChangeFileExt(ExtractFileName(Jpg),'.bmp'));
 JpgImage.free; BmpImage.Free;
end;

procedure TForm1.Convert(const JpgDir: String);
var
 SearchRec: TSearchRec;
 Found: Integer;
begin
 try
  Found:= FindFirst(JpgDir + '\*.Jpg',0,SearchRec);
  while Found = 0 do
  begin
   ConvertJpgToBmp(JpgDir + '\'+SearchRec.Name, OutputDir);
   UpdateStatus('Converting...',SearchRec.Name);
   Found:= FindNext(SearchRec);
  end;
 finally
  FindClose(SearchRec);
  UpdateStatus('Done!','');
 end;//try
end;

Quote
Mweckbart wrote in message <19990823134216.08461.00003...@ng-fa1.aol.com>...
>Hi there

>can anybody help me with this?

>Thanx

>Mariano

Re:How do i save a BMP as JPEG using jpeg.pas


OOPS! - You will need to also add JPeg to your uses clause...

Shane

Quote
>procedure TForm1.ConvertJpgToBmp(const Jpg: TFileName; const BmpDir:
>String);
>var
> JpgImage: TJPEGImage;
> BmpImage: TBitMap;
>begin
> JpgImage:= TJPEGImage.Create;
> JpgImage.LoadFromFile(Jpg);
> BmpImage:= TBitMap.Create;
> BmpImage.Assign(JpgImage);
> BmpImage.PixelFormat:= pf8Bit;
> BmpImage.SaveToFile(BmpDir+ChangeFileExt(ExtractFileName(Jpg),'.bmp'));
> JpgImage.free; BmpImage.Free;
>end;

>procedure TForm1.Convert(const JpgDir: String);
>var
> SearchRec: TSearchRec;
> Found: Integer;
>begin
> try
>  Found:= FindFirst(JpgDir + '\*.Jpg',0,SearchRec);
>  while Found = 0 do
>  begin
>   ConvertJpgToBmp(JpgDir + '\'+SearchRec.Name, OutputDir);
>   UpdateStatus('Converting...',SearchRec.Name);
>   Found:= FindNext(SearchRec);
>  end;
> finally
>  FindClose(SearchRec);
>  UpdateStatus('Done!','');
> end;//try
>end;

Re:How do i save a BMP as JPEG using jpeg.pas


var
b: TBitmap;
j: TJPEGImage;
begin
b:= TBitmap.Create;
j:= TJPEGImage.Create;
try
b.LoadFromFile(...);
j.Assign(b);
j.SaveToFile(...);
finally
b.Free;
j.Free;
end;

    There should really be two nested try-finally blocks.

Quote
Mweckbart wrote:
> Hi there

> can anybody help me with this?

> Thanx

> Mariano

Other Threads