2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

windowsに直接画像などを描画する方法

Posted at

この記事はwindows10にインストールしたvs2019と.NET Framework 4.7.2を使用しています。
完成形
yaa.PNG
てな感じです
こんなこともできます。nikoko.PNG

画像は大体さっき同じサイズにしたいなら
100px x 100px
ぐらいのを準備してください。

Program.cs
using System;
using System.Drawing;
using System.Runtime.InteropServices;


namespace ConsoleApp1
{
    class Program
    {
        [DllImport("User32.dll")]
        static extern IntPtr GetDC(IntPtr hwnd);
        [DllImport("User32.dll")]
        static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);
        static void Main(string[] args)
        {
            Random rnd = new Random();    //乱数インスタンス化
            IntPtr desktopDC = GetDC(IntPtr.Zero);
            using (Graphics g = Graphics.FromHdc(desktopDC))
            {
                //描画先とするImageオブジェクトを作成する
                Bitmap canvas = new Bitmap("画像のパス");
                while (true)
                {

                    //ImageオブジェクトのGraphicsオブジェクトを作成する
                    g.DrawImage(canvas, rnd.Next(0, 1920), rnd.Next(0, 1000));
                }
            }
        }
    }
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?