Jump to content
Sign in to follow this  
BennieC

bitmap to jpeg compression

Recommended Posts

Hi, I have been trying to compress bitmaps to jpegs into a database to save space.  However, I cannot get the codec to perform the compression.  My code looks as follows

var
  NewBitmap: TBitmap;
  CodecParams : TBitmapCodecSaveParams;
  MS1 : TMemoryStream;
  Surf: TBitmapSurface;
  JpgQuality : TBitmapCodecSaveParams;
 
code snippet
  JpgQuality.Quality := 100;

  MS1.Position := 0;
  Surf := TBitmapSurface.create;
  try
    Surf.assign(NewBitmap);
    // use the codec to save Surface to stream
      if not TBitmapCodecManager.SaveToStream(
                       MS1,
                       Surf,
//                       '.jpg', JpgQuality) then                                        // THIS DOES NOT WORK
                       '.jpg') then                                                             // THIS DOES WORK BUT NO COMPRESSION (FORMAT MAY NOT EVEN BE JPEG)
        raise EBitmapSavingFailed.Create(
              'Error saving Bitmap to jpg');
  finally
    Surf.Free;
  end;
 

Share this post


Link to post

Bernie, it seems that you're trying to maintain 100% quality which defeats the purpose of the JPEG format, since you'll get virtually no compression.

If you really don't want to sacrifice image quality, then you need to think of another image format, eg PNG or even QOI 😁, or just ZIP compress/decompress your BMP files.

Share this post


Link to post
52 minutes ago, angusj said:

Bernie, it seems that you're trying to maintain 100% quality which defeats the purpose of the JPEG format, since you'll get virtually no compression.

I would disagree here, my test bmp was 2.55MB in jpeg it's 277Kb.

 

16 hours ago, BennieC said:

However, I cannot get the codec to perform the compression.  My code looks as follows

You should give more details. For example what is the error message or exception you receive. Please do not forget we are no hackers and have no access to your environment (cannot hack into your computer to see your display). If it's possible always post a minimal test case that we can copy and compile.

 

I've tested with this code:

 

procedure TForm1.Button1Click(Sender: TObject);
var
  NewBitmap: TBitmap;
  CodecParams : TBitmapCodecSaveParams;
  MS1 : TMemoryStream;
  Surf: TBitmapSurface;
  JpgQuality : TBitmapCodecSaveParams;

begin
  ms1:=TMemoryStream.Create;
  try
    newBitmap:=Tbitmap.Create;
    newBitmap.LoadFromFile('d:\original.bmp');

    JpgQuality.Quality := 100;

    MS1.Position := 0;
    Surf := TBitmapSurface.Create;
    try
      Surf.assign(NewBitmap);
      // use the codec to save Surface to stream
        if not TBitmapCodecManager.SaveToStream(
                         MS1,
                         Surf,
                         '.jpg', @JpgQuality) then

          raise EBitmapSavingFailed.Create(
                'Error saving Bitmap to jpg');


      ms1.Position:=0;
      ms1.SaveToFile('d:\original_bmp.jpg');
    finally
      Surf.Free;
    end;
  finally
    ms1.Free;
  end;
end;

 

  • Like 1

Share this post


Link to post
1 hour ago, Lajos Juhász said:

I would disagree here, my test bmp was 2.55MB in jpeg it's 277Kb.

Thanks Lajos for the correction. Your are right 😁 and I was wrong 😱.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×