1

I'm new to Pascal and Delphi and I'm trying to find out how to compress and/or resize an image to get it below 300kb. I don't have a clue how to start and where to find an example. Does anyone know if this is possible? And maybe show me or point me to an example? I'm using RAD Studio XE4 for development.

Let's say I got an image of one of the most used types like png or jpeg and it's larger then 1024 x 768. How do I make it 1024 x 768?

Ken White
  • 123,280
  • 14
  • 225
  • 444
MmynameStackflow
  • 1,251
  • 3
  • 19
  • 39
  • 1
    You need to decide how to tackle the problem first. 1. Do you want to downsize or compress? Downsizing will definitely lose information. Compression may be lossy or lossless. 2. What sort of image do you have? A photo tends to be best compressed using JPEG (lossy). Screenshots are often best compressed using PNG (lossless). – David Heffernan May 22 '15 at 09:33
  • It must be possible to upload most used image types like png, jpeg... So I can limit the type of images. But then? Let's say I want the image to be 1024 x768 if it's larger. How can I do this? – MmynameStackflow May 22 '15 at 09:38
  • 1
    Get an image library that supports quality image resizing – David Heffernan May 22 '15 at 09:40
  • Well, I found PascalMagick but I don't know how to use it – MmynameStackflow May 22 '15 at 09:42
  • 1
    You'll likely have to spend some time evaluating candidate libraries. There are many. A question here isn't the way to do that evaluation. – David Heffernan May 22 '15 at 09:44
  • I found more libraries but I'm to new to Pascal to determine which one is easy to use and to find useful documentation for it. http://wiki.lazarus.freepascal.org/PascalMagick – MmynameStackflow May 22 '15 at 09:49
  • 1
    @MmynameStackflow: Why don't you first use Google? It should be your first move! See this topic (I think first in the results and complete solution) ... http://stackoverflow.com/questions/8111447/how-to-shrink-a-picture – smooty86 May 22 '15 at 09:49
  • I used Google a lot but I don't know where to search for. Using pascal + the thing I search for doesn't give useful results. Thanks for the link! Shrinking is a word I didn't use yet. – MmynameStackflow May 22 '15 at 09:53
  • Really, you are looking for a library recommendation and those questions are off topic here. You'd be better asking at the Google+ Delphi Developers group. – David Heffernan May 22 '15 at 09:57
  • I'm not looking for a specific library. I'm looking for a way to do it. If the answer is a library I still don't know how to use that library to get the job done. But thanks to the link smooty86 gave me I probably can figure it out. – MmynameStackflow May 22 '15 at 10:03
  • If you don't want to use a library, then you'll have to write your own resizing algorithm. That's complex. Not a subject for a question here. Do you want to write that yourself? – David Heffernan May 22 '15 at 10:32

1 Answers1

1

The most simple way to resize an image is probably the following :

procedure ResizeImage(Image : TGraphic; AWidth, AHeight : Integer);
var bmp : TBitmap;
begin
  bmp := TBitmap.Create;
  try
    bmp.PixelFormat := pf32bit;
    bmp.Width := AWidth;
    bmp.Height := AHeight;
    bmp.Canvas.StretchDraw(Rect(0,0,AWidth, AHeight), Image);
    Image.Assign(bmp);
  finally
    bmp.Free;
  end;
end;

Note that, while simple, it has many flaws. You will probably lose transparency information. I am unsure whether or not meta information of the original image will be preserved.

Maybe not all graphic class support stretchdraw. I tested with JPEG, but maybe png or tif don't support it.

The routine also assume that the TGraphic class knows how to assign itself a bitmap (which most, if not all, should do).

As for compression, many image format already compress the data, JPEG, PNG, some TIFF. Coming with a more efficient compression algorithm than what is already available would be no easy feat.

Ken Bourassa
  • 6,363
  • 1
  • 19
  • 28