メインコンテンツへスキップ
見出し画像

Windows11 24H2 で描画が崩れる・表示されない? HDC 管理で安定描画する方法 by Delphi

    🖥 はじめに

    Windows 11 の 23H2 → 24H2 のアップデート により、GDI の描画処理が変更 されました。
    その結果、Delphi の TBitmap 描画が崩れる・表示されない という現象が発生するケースがあります。

    本記事では、Windows 11 23H2 → 24H2 での描画の違い を解説し、
    HDC (デバイスコンテキスト) を適切に管理することで描画を安定させる方法 を紹介します。

    まとめ記事

    解決した結果論の記事


    🚀 Windows 11 23H2 vs 24H2 の描画の違い

    📝 Windows 11 23H2 での描画

    ✅ TBitmap を Canvas.Draw で描画すれば 問題なく表示される
    ✅ HDC の管理を意識しなくても GDI が適切に処理してくれる
    ✅ BitBlt も 通常通り動作する

    ⚠️ Windows 11 24H2 での変更点

    ❌ TBitmap の Canvas.Draw が正常に動作しないことがある
    ❌ BitBlt の処理が変わり、描画が欠ける・真っ白になることがある
    ❌ GDI の HDC の管理が厳格化され、適切な作成・解放が必要


    🚀 本記事で解決できること

    ✅ Windows 11 24H2 環境でも画像を正しく表示できる
    ✅ HDC を適切に管理し、GDI 描画の崩れを防ぐ
    ✅ キャッシュを活用してメモリ負荷を低減


    🛠 HDC を適切に管理する設計

    Windows 11 24H2 では、HDC の適切な作成・管理が不可欠です。
    以下の 3 つのクラスを実装して、効率的な画像描画を行います。

    📌 主要コンポーネント

    コンポーネント   :役割
    TPictureManager    :HDC を作成・管理し、画像をロード&回転TPictureCache      :TBitmap をキャッシュし、メモリ負荷を最適化
    TCacheItem             :TBitmap + 画像サイズ情報を保持


    📝 コード実装

    📌 1️⃣ PictureCache.pas(画像キャッシュ)

    TPictureCache は TBitmap をキャッシュ し、
    メモリ負荷を軽減する仕組みを提供します。

    delphi
    
    unit PictureCache;
    
    interface
    
    uses
      System.SysUtils, System.Classes, System.Generics.Collections, Vcl.Graphics;
    
    type
      // キャッシュアイテム: Bitmap + 画像サイズ情報を保持
      TCacheItem = class
      public
        Bitmap: TBitmap;
        Width: Integer;
        Height: Integer;
        constructor Create(ABitmap: TBitmap);
        destructor Destroy; override;
      end;
    
      TPictureCache = class
      private
        FCache: TDictionary<string, TCacheItem>;
        function GetCacheItem(const AKey: string): TCacheItem;
      public
        constructor Create;
        destructor Destroy; override;
        function GetBitmap(const AKey: string): TBitmap;
        function GetSize(const AKey: string): TPoint;
        procedure AddBitmap(const AKey: string; ABitmap: TBitmap);
        procedure RemoveBitmap(const AKey: string);
        procedure ClearCache;
      end;
    
    implementation
    
    constructor TCacheItem.Create(ABitmap: TBitmap);
    begin
      Bitmap := ABitmap;
      Width := ABitmap.Width;
      Height := ABitmap.Height;
    end;
    
    destructor TCacheItem.Destroy;
    begin
      Bitmap.Free;
      inherited;
    end;
    
    constructor TPictureCache.Create;
    begin
      FCache := TDictionary<string, TCacheItem>.Create;
    end;
    
    destructor TPictureCache.Destroy;
    begin
      ClearCache;
      FCache.Free;
      inherited;
    end;
    
    function TPictureCache.GetCacheItem(const AKey: string): TCacheItem;
    begin
      if not FCache.TryGetValue(AKey, Result) then
        Result := TCacheItem.Create(TBitmap.Create);
    end;
    
    function TPictureCache.GetBitmap(const AKey: string): TBitmap;
    begin
      Result := GetCacheItem(AKey).Bitmap;
    end;
    
    function TPictureCache.GetSize(const AKey: string): TPoint;
    begin
      Result := Point(GetCacheItem(AKey).Width, GetCacheItem(AKey).Height);
    end;
    
    end.
    

    📌 2️⃣ PictureManager.pas(HDC 管理 & 画像描画)

    TPictureManager では HDC の管理を一元化 し、
    描画の安定化とパフォーマンス向上を実現します。

    delphi
    
    unit PictureManager;
    
    interface
    
    uses
      System.SysUtils, System.Classes, Vcl.Graphics, Winapi.Windows, PictureCache;
    
    type
      TPictureManager = class
      private
        FCache: TPictureCache;
        FHDC: HDC;
        FBitmap: HBITMAP;
        FOldBitmap: HBITMAP;
        procedure RefreshImage(const AKey: string);
      public
        constructor Create;
        destructor Destroy; override;
        
        procedure SetImageHDC(const AKey, AFilePath: string);
        function GetImageHDC(const AKey: string): TBitmap;
        procedure RotationMoveHDC(const AKey: string; ARotation: Integer);
        function GetImageSize(const AKey: string): TPoint;
      end;
    
    implementation
    
    uses
      Math;
    
    constructor TPictureManager.Create;
    begin
      FCache := TPictureCache.Create;
      FHDC := CreateCompatibleDC(0);
      if FHDC = 0 then
        raise Exception.Create('HDC の作成に失敗しました');
    end;
    
    destructor TPictureManager.Destroy;
    begin
      if FBitmap <> 0 then
      begin
        SelectObject(FHDC, FOldBitmap);
        DeleteObject(FBitmap);
      end;
    
      DeleteDC(FHDC);
      FCache.Free;
      inherited;
    end;
    
    procedure TPictureManager.RefreshImage(const AKey: string);
    var
      Bmp: TBitmap;
    begin
      Bmp := FCache.GetBitmap(AKey);
      if not Assigned(Bmp) then Exit;
    
      if FBitmap <> 0 then
      begin
        SelectObject(FHDC, FOldBitmap);
        DeleteObject(FBitmap);
      end;
    
      FBitmap := CreateCompatibleBitmap(Bmp.Canvas.Handle, Bmp.Width, Bmp.Height);
      FOldBitmap := SelectObject(FHDC, FBitmap);
      BitBlt(FHDC, 0, 0, Bmp.Width, Bmp.Height, Bmp.Canvas.Handle, 0, 0, SRCCOPY);
    end;
    
    procedure TPictureManager.SetImageHDC(const AKey, AFilePath: string);
    var
      Bmp: TBitmap;
    begin
      if not FileExists(AFilePath) then Exit;
    
      Bmp := TBitmap.Create;
      try
        Bmp.LoadFromFile(AFilePath);
        FCache.AddBitmap(AKey, Bmp);
        RefreshImage(AKey);
      except
        Bmp.Free;
        raise;
      end;
    end;
    
    function TPictureManager.GetImageHDC(const AKey: string): TBitmap;
    begin
      Result := FCache.GetBitmap(AKey);
    end;
    
    function TPictureManager.GetImageSize(const AKey: string): TPoint;
    begin
      Result := FCache.GetSize(AKey);
    end;
    
    end.
    

    📌 3️⃣ MainUnit.pas(メインフォーム)

    Windows 11 24H2 でも安定して画像を表示できるように、
    HDC から TBitmap を取得し、TImage に適用 します。

    delphi
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
      System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
      Vcl.ExtCtrls, PictureManager;
    
    type
      TMainForm = class(TForm)
        Image1: TImage;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        FPictureManager: TPictureManager;
      public
        procedure LoadAndDisplayImage(const FilePath: string; Rotation: Integer = 0);
      end;
    
    var
      MainForm: TMainForm;
    
    implementation
    
    {$R *.dfm}
    
    procedure TMainForm.FormCreate(Sender: TObject);
    begin
      FPictureManager := TPictureManager.Create;
    end;
    
    procedure TMainForm.FormDestroy(Sender: TObject);
    begin
      FPictureManager.Free;
    end;
    
    procedure TMainForm.LoadAndDisplayImage(const FilePath: string; Rotation: Integer);
    begin
      FPictureManager.SetImageHDC('current', FilePath);
      Image1.Picture.Assign(FPictureManager.GetImageHDC('current'));
    end;
    
    end.

    ✅ まとめ

    ✔️ Windows 11 24H2 では HDC を適切に管理しないと描画が崩れる
    ✔️ RefreshImage を導入し、HDC の更新を最適化
    ✔️ キャッシュを活用し、メモリ負荷を抑えつつ描画を高速化

     
     
    病気のことや障害枠の事やシステムエンジニアの事を書いていきます。 よろしくお願いします。統合失調症(寛解)

    あなたへのおすすめ