调整图像大小并保存输出

时间:2017-08-28 12:28:49

标签: delphi delphi-10-seattle

我在SO上看到很多关于调整图像大小的问题/答案。

但我找不到适合我个人的正确方法。

In this post ,只有当您想从大图片中获得一张小图片时,它才有效。

但是,如果您的图片尺寸为24x24,并且想要将其尺寸调整为256x256尺寸,那么procedure将会失败并为您提供扭曲的图片。

以下代码是我尝试解决我的问题

  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;

原始图片:

enter image description here

结果(左上角有黑色部分的白色方块):

enter image description here

我们如何将图片加载到TImage并转换/调整大小并保存更改?

1 个答案:

答案 0 :(得分:3)

感谢 kobik 发表评论,这很有用。

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;
相关问题