首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用GDI+用阿尔法通道调整PNG图像的大小?

如何使用GDI+用阿尔法通道调整PNG图像的大小?
EN

Stack Overflow用户
提问于 2015-07-07 14:34:28
回答 2查看 2.7K关注 0票数 5

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

这是我的代码,它似乎运行良好。它将降低/增加一个巴布亚新几内亚的规模。到目前为止,在XP上进行了测试:

代码语言:javascript
运行
复制
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是正确的方法吗?我没有找到任何其他方法。

EN

Stack Overflow用户

回答已采纳

发布于 2015-07-07 15:51:18

我不认为GetThumbnailImage方法是一个很好的方法,因为我怀疑你会得到一个高质量的重放图像。在this article中,您可以找到如何重新绘制图像。他们使用的是DrawImage方法,所以我也会这么做。在此之前,我还会设置高质量的图形模式,以获得高质量的输出。下面是一个示例:

代码语言:javascript
运行
复制
procedure TForm1.Button1Click(Sender: TObject);
var
  Input: TGPImage;
  Output: TGPBitmap;
  Encoder: TGUID;
  Graphics: TGPGraphics;
begin
  Input := TGPImage.Create('C:\InputImage.png');
  try
    // create the output bitmap in desired size
    Output := TGPBitmap.Create(100, 100, PixelFormat32bppARGB);
    try
      // create graphics object for output image
      Graphics := TGPGraphics.Create(Output);
      try
        // set the composition mode to copy
        Graphics.SetCompositingMode(CompositingModeSourceCopy);
        // set high quality rendering modes
        Graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
        Graphics.SetPixelOffsetMode(PixelOffsetModeHighQuality);
        Graphics.SetSmoothingMode(SmoothingModeHighQuality);
        // draw the input image on the output in modified size
        Graphics.DrawImage(Input, 0, 0, Output.GetWidth, Output.GetHeight);
      finally
        Graphics.Free;
      end;
      // get encoder and encode the output image
      if GetEncoderClsid('image/png', Encoder) <> -1 then
        Output.Save('C:\OutputImage.png', Encoder)
      else
        raise Exception.Create('Failed to get encoder.');
    finally
      Output.Free;
    end;
  finally
    Input.Free;
  end;
end;
票数 7
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31271622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档