ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 If the image size is larger than 150 KB then resize it to less than 150 KB

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

View 
UserName:
Password:
Format  Bold Italicized Underline  Align Left Centered Align Right  Horizontal Rule  Insert Hyperlink   Browse for an image to attach to your post Browse for a zip to attach to your post Insert Code  Insert Quote Insert List
   
Message 

 

Emoji
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Black Eye [B)]
Frown [:(] Shocked [:0] Angry [:(!] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
abbasiali1010@gmail.com Posted - Oct 16 2025 : 17:24:37
Hi

I want to If the image size is larger than 150 KB then resize it to less than 150 KB.
I'll give you a sample code below, and what should I write in the "Change Other" section to make it work?
Is it possible to directly change the image size to below 150KB in DBImage1 without assigning it and then put it back in the same DBImage1?

//----------------------------------------------------------
procedure TPictureForm.btnAdjustClick(Sender: TObject);
var
  bmp: TBitmap;
begin

  try
    // Directly assign image from DBImage to Image1
    Image1.Picture.Assign(DBImage1.Picture.Graphic);

    // Create Bitmap and setup image processor
    bmp := TBitmap.Create;
    try
      ImageEnProc1.AttachedBitmap := bmp;
      ImageEnIO1.AttachedBitmap := bmp;

      // Copy image to ImageEnIO for processing
      ImageEnIO1.Bitmap.Assign(Image1.Picture.Graphic);

      //If the image is larger than 150 KB, resize it to less than 150 KB
      //......

      // Return the result back to Image1
      Image1.Picture.Bitmap.Assign(bmp);
      Image1.Update;
    finally
      bmp.Free;
    end;
  except
    on E: Exception do
      ShowMessage('Error in image processing: ' + E.Message);
  end;
end;
//-------------------------------------------------------------


Regards

Ali Abbasi
4   L A T E S T    R E P L I E S    (Newest First)
xequte Posted - Oct 25 2025 : 20:23:30
Hi Ali

Do you mean by scaling the whole image to the new size, or just by cropping it?

For scaling just use:

https://www.imageen.com/help/TIEBitmap.Resample.html

For cropping, use:

http://www.imageen.com/help/TIEBitmap.Resize.html

Nigel
Xequte Software
www.imageen.com
abbasiali1010@gmail.com Posted - Oct 24 2025 : 11:57:36
Hi Nigel

I using bmp photo files of people with a size of about 800 KB to 1 MB.

Can I reduce the size of the bmp file to, say, 300 KB?

Can I reduce the height and width, which are large and about 300 pixels to 700 pixels, and the height to 300 pixels and the width to 300 pixels?

Regards

Ali Abbasi
abbasiali1010@gmail.com Posted - Oct 17 2025 : 00:16:58
Hi Nigel

Thank you for your help.
I will check it and I think it good sulution.

Regards

Ali Abbasi
xequte Posted - Oct 16 2025 : 18:31:50
Hi Ali

Here is a quick example of how do do it:


procedure SaveToStreamAtMaxSize(Bitmap: TIEBitmap; Dest: TStream; MaxSizeBytes: Int64; Format: TIOFileType; JPEGQuality: Integer = 75; ResampleFilter: TResampleFilter = rfFastLinear);
const
  Scale_Reduction_Per_Pass = 0.25; // Reduce size of image by 25% each pass
var
  currScale: Double;
  tempBmp: TIEBitmap;
begin
  currScale := 1.0;
  tempBmp := TIEBitmap.Create();
  try
    While True do
    begin
      Dest.Size := 0;
      tempBmp.Assign( Bitmap );

      if ( tempBmp.Width < 50 ) or ( tempBmp.Height < 50 ) then
        raise Exception.create( 'Cannot resample further' );

      tempBmp.Resample( currScale, ResampleFilter );
      tempBmp.Params.JPEG_Quality := JPEGQuality;
      tempBmp.SaveToStream( Dest, Format );

      if Dest.Size <= MaxSizeBytes then
        EXIT; // SUCCESS

      if Dest.Size > MaxSizeBytes * 4 then
        currScale := currScale * ( 1.0 / Sqrt(Dest.Size / MaxSizeBytes ))   // Image is huge? Reduce more quickly
      else
        currScale := currScale * ( 1.0 - Scale_Reduction_Per_Pass );          // Image is close to size? Fine tuning...
    end;

  finally
    tempBmp.Free();
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
const
  MAX_SIZE_BYTES = 150 * 1024;
var
  bmp: TIEBitmap;
  ms: TMemoryStream;
begin
  bmp := TIEBitmap.Create();
  ms  := TMemoryStream.Create();
  try
    bmp.LoadFromFile( 'D:\Testing_Multimedia\Big_Big_Pix\570130.jpg' );

    SaveToStreamAtMaxSize( bmp, ms, MAX_SIZE_BYTES, ioJPEG, 80 );

    // Do something with ms, e.g. output to database blob field

    // Test output
    // ms.Position := 0;
    // ImageEnView1.IO.LoadFromStream( ms );

  finally
    ms.Free();
    bmp.Free();
  end;
end;


Nigel
Xequte Software
www.imageen.com