1

I see many questions/Answers about resizing images here on SO.

But I can't find the correct one that fit my case.

In this post, it works only when you want to have a small image from a big one.

However if you have a picture with 24x24 dimension and you want to resize it to 256x256 dimension, the procedure will fail and gives you a distorted picture.

The code below is my attempt to sort out my issue

  Graph := TBitmap.Create;
  try   // After loading a .bmp file to Image1 with 48x48 dimension 
    Graph.Assign( Image1.Picture.Bitmap );
    Graph.Canvas.StretchDraw(Rect(0, 0, 255, 255), Graph);
    Graph.SetSize(255,255);
    Graph.SaveToFile('Location\Resault.bmp');
  finally
    Graph.Free;
  end;

The original image:

enter image description here

The result (the white square with the black part on the upper left):

enter image description here

How can we load an image to a TImage and convert/resize it and save changes?

Andy K
  • 4,944
  • 10
  • 53
  • 82
Ilyes
  • 14,640
  • 4
  • 29
  • 55
  • 2
    Use a temp Bitmap with 255x255 and StretchDraw the small one on it. then assign back to `Graph` if you wish. do not expect to see great results when resizing a small image into a big one. – kobik Aug 28 '17 at 12:53
  • @kobik Thanks for the comment, I'll give it a try – Ilyes Aug 28 '17 at 12:56
  • or that @Sami http://chrislema.com/how-to-resize-images-to-make-them-larger-without-losing-quality/ – Andy K Aug 28 '17 at 12:57

1 Answers1

3

Thanks to kobik for the comment, it was useful.

var Graph : TBitmap; Conv : TBitmap;
begin

      Graph := TBitmap.Create;
      try
        Graph.Assign( Image1.Picture.Bitmap );
        Conv := TBitmap.Create;
        try
          Conv.SetSize(255,255);
          Conv.Canvas.StretchDraw(Rect(0, 0, 255, 255), Graph);
          Conv.SaveToFile('Location\Resault.bmp');   
        finally
          Conv.Free;
        end;

      finally
        Graph.Free;
      end;

end;
Ilyes
  • 14,640
  • 4
  • 29
  • 55