如何使用GDI +使用Alpha通道调整PNG图像的大小?

时间:2015-07-07 14:34:29

标签: delphi resize png delphi-7 gdi+

在寻找调整TPngObject大小并保持透明度+ alpha通道无效的方法后,我正在尝试使用GDI +

这是我的代码,似乎可以正常工作。它将向下/向上缩放PNG。 到目前为止在XP上测试过:

uses GDIPAPI, GDIPOBJ, GDIPUTIL; 

procedure TForm1.Button1Click(Sender: TObject);
var
  encoderClsid: TGUID;
  stat: TStatus;
  img, img_out: TGPImage;
begin
  img := TGPImage.Create('in.png'); // 200 x 200  
  img_out := img.GetThumbnailImage(100, 100, nil, nil);
  GetEncoderClsid('image/png', encoderClsid);
  img_out.Save('out.png', encoderClsid);
  img_out.free;
  img.Free;
end;

我的问题:正在使用GetThumbnailImage正确的方法吗?我没有找到任何其他方法。

2 个答案:

答案 0 :(得分:7)

我不认为int X, Y; int bitmapin(FILE* fp,/* int height, int width, */ pixel** img1){ long n; int t; fseek(fp, 10, SEEK_SET); fread(&t, 1, 4, fp); //reads the offset and puts it in t fseek(fp, t, SEEK_SET); int i, p, e; e=4-((X*3)%4); if (e==4) { e=0; } for (i=Y-1; i>=0; i-- ) { for (p=0; p<X; p++) { n=fread(&img1[i][p].blue, 1, 1, fp); if (n!=1) { return 29; } n=fread(&img1[i][p].green, 1, 1, fp); if (n!=1) { return 30; } n=fread(&img1[i][p].red, 1, 1, fp); if (n!=1) { return 31; } } fseek(fp, e, SEEK_CUR); } return 0; } pixel** make2Dpixelarray(/*int y, int x*/){ pixel** theArray; theArray=(pixel**) malloc(X*sizeof(pixel*)); for (int i=0; i<X; i++) { theArray[i]=(pixel*) malloc(Y*sizeof(pixel)); } return theArray; } int main(int argc, const char * argv[]) { FILE* fp; fp=fopen("/Users/admin/desktop/Immagine5.bmp", "r"); if (fp==NULL) { return 20; } setWidthHeight(fp); //Puts X=width and Y=height, it works pixel** img1=make2Dpixelarray(); //Creates 2D pixel array and get the memory for it bitmapin(fp, img1); //this function should put the values of RGB pixel into the matrix CreateNewImage(fp, img1); //This function creates a new image. return 0; } 方法是一种很好的方法,因为我怀疑你会获得高质量的重采样图像。在this article中,您可以找到如何重新缩放图像。他们正在使用DrawImage方法,所以我也会这样做。就在此之前,我还将设置高质量的图形模式以获得高质量的输出。这是一个例子:

GetThumbnailImage

答案 1 :(得分:2)

我不认为使用GetThumbnailImage方法是正确的方法。为什么呢?

GetThumbnailImage方法的主要用途是获取一个缩略图,您可以将其用作某些更高分辨率图像的预览。

因此,我假设后面使用的算法开发得尽可能快,但它可能并不关心最终结果质量。因此,使用此方法可能会导致质量非常差的图像重新调整。

现在,如果您真的对使用Delphi的图像处理感兴趣,那么您一定要检查Graphics32库(http://graphics32.org/wiki/)。

它支持Delphi 7及更高版本的所有Delphi版本。它提供了许多先进的图像处理算法。最重要的是它支持硬件加速,这意味着它实际上可以利用您的GPU处理能力来进行图像处理。