2012-08-02 110 views
1

我用這個代碼來繪製圖像,並將其與2006年德爾福保存爲TIFF:如何使用Delphi設置TIFF圖像的DPI分辨率?

var Bmp: TBitmap; 
    MF: TMetaFile; 
    MetafileCanvas: TMetafileCanvas; 
begin 
    Gdip := TGDIPlusFull.Create('gdiplus.dll'); 
    MF := TMetaFile.Create; 

    MF.Width := 1000; 
    MF.Height := 1100; 

    MetafileCanvas := TMetafileCanvas.Create(MF, 0); 
    MetafileCanvas.Brush.Color := clRed; 
    MetafileCanvas.Brush.Style := bsDiagCross; 
    MetafileCanvas.Ellipse(50, 50, 300 - 50, 200 - 50); 
    MetafileCanvas.Free; 

    Bmp := Gdip.DrawAntiAliased(MF); 

    Image1.Picture.Assign(Bmp); 
    SynGDIPlus.SaveAs(Bmp, 'c:\test.tif', gptTIF); 
    Bmp.Free; 

    MF.Free; 
    FreeAndNil(GdiP); 
end; 

注意我用免費的框架fromhttp://www.synopse.info。

該代碼工作得很好。但是我有一個問題。我如何設置TIFF分辨率。 我的test.tif圖片有96 DPI(屏幕解析),但我需要200 DPI。 注意我不希望更改圖像尺寸(寬度和高度),因爲有正確的,我想更改只有DPI分辨率。

我已經找到了很多關於這個問題的答案,但沒有提到Delphi。

回答

3

我已經添加了以下方法:

procedure TSynPicture.BitmapSetResolution(DPI: single); 
begin 
    if (fImage<>0) and fAssignedFromBitmap and (DPI<>0) then 
    Gdip.BitmapSetResolution(fImage,DPI,DPI); 
end; 

,它將調用相應的GDI + API用於設置位圖分辨率。

那麼就應該保存時指定:

procedure SaveAs(Graphic: TPersistent; const FileName: TFileName; 
    Format: TGDIPPictureType; CompressionQuality: integer=80; 
    MaxPixelsForBiggestSide: cardinal=0; BitmapSetResolution: single=0); overload; 
var Stream: TStream; 
begin 
    Stream := TFileStream.Create(Filename, fmCreate); 
    try 
    SaveAs(Graphic,Stream,Format,CompressionQuality,MaxPixelsForBiggestSide, 
     BitmapSetResolution); 
    finally 
    Stream.Free; 
    end; 
end; 

所以,你可能能夠在您的代碼來寫:

Bmp := Gdip.DrawAntiAliased(MF); 
    Image1.Picture.Assign(Bmp); 
    SynGDIPlus.SaveAs(Bmp, 'c:\test.tif', gptTIF, 80, 0, 200); // force 200 DPI 
    Bmp.Free; 

this commit

0

TWICImage類能夠保存TIF文件的DPI信息,但乍看之下對此功能的訪問並不明顯。只需調用Handle的SetResolution功能即可。

tif := TWICImage.Create; 
... 
tif.Handle.SetResolution(DPI_X, DPI_Y); 
相關問題