📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

69行目: 69行目:
<br>
<br>
これらのクラスを使い分けることにより、効率的で柔軟な画像処理と表示が可能になる。<br>
これらのクラスを使い分けることにより、効率的で柔軟な画像処理と表示が可能になる。<br>
<br><br>
== QImageクラスの使用例 ==
==== 画像の拡大 ====
以下の例では、QImageクラスを使用して、画像を拡大している。<br>
<br>
<syntaxhighlight lang="c++">
// ImageResizer.hファイル
#include <QImage>
#include <QSize>
class ImageResizer
{
private:
    // エラーメッセージを保存する変数
    QString m_lastError;
    // エラーメッセージを設定するヘルパーメソッド
    void setError(const QString &error)
    {
      m_lastError = error;
    }
public:
    ImageResizer() {};
    ~ImageResizer() {};
    // 画像の拡大
    bool resizeImage(const QString &inputPath, const QString &outputPath, const QSize &newSize)
    {
      // 入力ファイルの存在確認
      if (!QFileInfo::exists(inputPath)) {
          setError("入力ファイルが存在しない: " + inputPath);
          return false;
      }
      // 画像の読み込み
      QImage image(inputPath);
      if (image.isNull()) {
          setError("画像の読み込みに失敗: " + inputPath);
          return false;
      }
      // 画像の拡大
      QImage resizedImage = image.scaled(newSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
      // 拡大した画像の保存
      if (!resizedImage.save(outputPath)) {
          setError("拡大した画像の保存に失敗: " + outputPath);
          return false;
      }
      return true;
    }
public:
    // エラーメッセージの取得
    QString lastError() const
    {
      return m_lastError;
    }
};
</syntaxhighlight>
<br>
<syntaxhighlight lang="c++">
// main.cppファイル
#include "ImageResizer.h"
int main()
{
    ImageResizer resizer;
    QString inputPath  = "input.jpg";
    QString outputPath = "output.jpg";
    QSize newSize(800, 600);
    if (resizer.resizeImage(inputPath, outputPath, newSize)) {
      qDebug() << "画像の拡大に成功";
    }
    else {
      qDebug() << "エラー:" << resizer.lastError();
    }
    return 0;
}
</syntaxhighlight>
<br><br>
<br><br>