delphi操作png的单元pngimage.pas包括引用的ZlibExApi.pas和 PngLang.pas

有需要的可以去这里下载源码。http://download.csdn.net/detail/sushengmiyan/4662715

delphi操作png是在delphi2009版本之后(网上这么说的)

于是乎。。

我找了源码来共享。。

希望对大家有帮助。

当你的程序找不到pngimage这个单元的时候。。。

这段代码会帮助到你。。。

当你的不使用delphi...

或者你的可以正常使用pngimage。。。

那么。。。。

这里你就可以忽略了。。。

下面就是一个简单的用法。可以参考下下~

  procedure doSomething(ms: TMemoryStream);
  var
    Buffer:Word;
    AjpgFile: TJPEGImage;
    ApngFile: TPNGObject;
  begin
    ms.Position := 0;  
    if ms.Size = 0 then
      Exit;
    ms.ReadBuffer(Buffer,2); //读取文件的前2个字节,放到Buffer里面
    if Buffer=$4D42 then //如果前两个字节是以4D42[低位到高位]
    begin
      ShowMessage('BMP'); //那么这个是BMP格式的文件
    end
    else if Buffer=$D8FF then //如果前两个字节是以D8FF[低位到高位]
    begin
      ms.Position := 0;
      AjpgFile := TJPEGImage.Create;
      AjpgFile.LoadFromStream(ms);
      Image1.Picture.Graphic := AjpgFile;
    end
    else if Buffer=$4947 then
    begin
      ShowMessage('GIF');
    end
    else if Buffer=$5089 then
    begin
      ms.Position := 0;
      ApngFile := TPNGObject.Create;
      ApngFile.LoadFromStream(ms);
      Image1.Picture.Graphic := ApngFile;
    end;
  end;

不好意思,写的太烂。内存泄露没处理

   else if Buffer=$D8FF then //如果前两个字节是以D8FF[低位到高位]
    begin
      ms.Position := 0;
      AjpgFile := TJPEGImage.Create;
      try
        AjpgFile.LoadFromStream(ms);
        Image1.Picture.Graphic := AjpgFile;
      finally
        AjpgFile.Free;
      end;
    end
    else if Buffer=$4947 then
    begin
      ShowMessage('GIF');
    end
    else if Buffer=$5089 then
    begin
      ms.Position := 0;
      ApngFile := TPNGObject.Create;
      try
        ApngFile.LoadFromStream(ms);
        Image1.Picture.Graphic := ApngFile;
      finally
        ApngFile.Free;
      end;
    end;


你可能感兴趣的:(image,buffer,Delphi)