BitBlt WindowsAPIを用いて画面にビットマップ画像を描画する - C#

BitBlt Windows APIを使って画面にビットマップイメージを描画します。

事前準備:画像の用意

画面に表示させるビットマップ画像として、下図の画像を準備します。

プログラム

Windowsフォームアプリケーションを作成します。

コード

下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace BitBlt
{
  public partial class Form1 : Form
  {
    enum TernaryRasterOperations : uint
    {
      SRCCOPY = 0x00CC0020,
      SRCPAINT = 0x00EE0086,
      SRCAND = 0x008800C6,
      SRCINVERT = 0x00660046,
      SRCERASE = 0x00440328,
      NOTSRCCOPY = 0x00330008,
      NOTSRCERASE = 0x001100A6,
      MERGECOPY = 0x00C000CA,
      MERGEPAINT = 0x00BB0226,
      PATCOPY = 0x00F00021,
      PATPAINT = 0x00FB0A09,
      PATINVERT = 0x005A0049,
      DSTINVERT = 0x00550009,
      BLACKNESS = 0x00000042,
      WHITENESS = 0x00FF0062,
      CAPTUREBLT = 0x40000000
    }
    
    [DllImport("gdi32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight,
      IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);

    [DllImport("user32.dll")]
    static extern IntPtr GetWindowDC(IntPtr hWnd);
    
    [DllImport("user32.dll")]
    private static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    static extern IntPtr CreateCompatibleDC(IntPtr hdc);

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    static extern bool DeleteDC(IntPtr hdc);

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
      Bitmap bmp = new Bitmap("ball.bmp");
     
      IntPtr hdc = e.Graphics.GetHdc();
      IntPtr hsrc = CreateCompatibleDC(hdc);
      IntPtr porg = SelectObject(hsrc, bmp.GetHbitmap());

      BitBlt(hdc, 80, 30, bmp.Width, bmp.Height, hsrc, 0, 0, TernaryRasterOperations.SRCCOPY);

      DeleteDC(hsrc);
      e.Graphics.ReleaseHdc(hdc);
    }
  }
}

解説

Bitmapオブジェクトを作成します。コンストラクタに画像ファイル名を与え指定したビットマップファイルを読み込みます。
  Bitmap bmp = new Bitmap("ball.bmp");

CreateCompatibleDC()関数を呼び出し、キャンバスのデバイスコンテキストを取得します。CreateCompatibleDC()関数にはデバイスコンテキストのハンドルを与える必要があります。デバイスコンテキストのハンドルは、GraphicsオブジェクトのGetHdc()メソッドで取得します。
  IntPtr hdc = e.Graphics.GetHdc();
  IntPtr hsrc = CreateCompatibleDC(hdc);

SelectObject関数を呼び出し、キャンバスのデバイスコンテキストを選択します。選択後 BitBlt関数を呼び出しキャンバスにビットマップ画像を描画します。
  IntPtr porg = SelectObject(hsrc, bmp.GetHbitmap());

  BitBlt(hdc, 80, 30, bmp.Width, bmp.Height, hsrc, 0, 0, TernaryRasterOperations.SRCCOPY);

DeleteDC()関数を呼び出しキャンバスのデバイスコンテキストを開放します。また、GraphicsオブジェクトのGetHdc()で取得したデバイスコンテキストのハンドルをRelaseHdc()メソッドを呼び出して開放します。
  DeleteDC(hsrc);
  e.Graphics.ReleaseHdc(hdc);

実行結果

アプリケーションを実行すると下図の画面が表示されます。


BitBitを用いて画像をフォームに表示できました。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2018-03-10
作成日: 2012-01-29
iPentec all rights reserverd.