c++ builder XE4, 10.2 Tokyo > TImage > 読込む画像サイズに合わせてTImageサイズを変更する


動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2017/12/28)

関連 http://qiita.com/7of9/items/9edf309c5f58cec126ff
関連 http://qiita.com/7of9/items/bce8dab6226cec14dc3a

pngやjpg画像を読込むとき、Stretchを使うと画像が粗くなる。これを回避する方法として、元画像のサイズのまま読込む方法を調べた。

参考 http://stackoverflow.com/questions/15209076/how-to-get-dimensions-of-image-file-in-delphi

一旦TPicture型で読込んで、そのWidth, Heightプロパティを使うという方法。

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <pngimage.hpp>
//#include <Jpeg.hpp>
#include <memory>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    // Image1->Stretch = true;

    String filename = L"site-logo.png";

    std::unique_ptr<TPicture> pict(new TPicture);
    pict->LoadFromFile(filename);

    Image1->Width = pict->Width;
    Image1->Height = pict->Height;

    Image1->Picture->LoadFromFile(filename);
}
//---------------------------------------------------------------------------

画像のサイズそのままが使われるようになった。