Last active
May 7, 2017 09:16
-
-
Save ksasao/f62a95c0a35c56df527a8c309b447999 to your computer and use it in GitHub Desktop.
System.Drawing でベクターデータを描画してファイルに保存するサンプル。Windows 10 Creators Update 以降、.NET Framework 4.6.2 以降で利用してください。app.manifest ファイルはデスクトップのスケーリング設定が100%ではない場合に意図通り描画するために必要です(52-56行目)。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> | |
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/> | |
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> | |
<security> | |
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> | |
<!-- UAC マニフェスト オプション | |
Windows のユーザー アカウント制御のレベルを変更するには、 | |
requestedExecutionLevel ノードを以下のいずれかで置換します。 | |
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> | |
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> | |
<requestedExecutionLevel level="highestAvailable" uiAccess="false" /> | |
requestedExecutionLevel 要素を指定すると、ファイルおよびレジストリの仮想化が無効にされます。 | |
アプリケーションが下位互換性を保つためにこの仮想化を要求する場合、この要素を | |
削除します。 | |
--> | |
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> | |
</requestedPrivileges> | |
</security> | |
</trustInfo> | |
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> | |
<application> | |
<!-- このアプリケーションがテストされ、協働するようテストされた Windows バージョンの | |
一覧。適切な要素をコメント解除すると、最も互換性のある環境を Windows が | |
自動的に選択します。--> | |
<!-- Windows Vista --> | |
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />--> | |
<!-- Windows 7 --> | |
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />--> | |
<!-- Windows 8 --> | |
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />--> | |
<!-- Windows 8.1 --> | |
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />--> | |
<!-- Windows 10 --> | |
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />--> | |
</application> | |
</compatibility> | |
<!-- アプリケーションが DPI 対応であり、それ以上の DPI で Windows によって自動的にスケーリングされないことを | |
示します。Windows Presentation Foundation (WPF) アプリケーションは自動的に DPI に対応し、オプトインする必要は | |
ありません。さらに、この設定にオプトインする .NET Framework 4.6 を対象とする Windows Forms アプリケーションは、 | |
app.config ファイルで 'EnableWindowsFormsHighDpiAutoResizing' 設定を 'true' に設定する必要があります。--> | |
<application xmlns="urn:schemas-microsoft-com:asm.v3"> | |
<windowsSettings> | |
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware> | |
</windowsSettings> | |
</application> | |
<!-- Windows のコモン コントロールとダイアログのテーマを有効にします (Windows XP 以降) --> | |
<!-- | |
<dependency> | |
<dependentAssembly> | |
<assemblyIdentity | |
type="win32" | |
name="Microsoft.Windows.Common-Controls" | |
version="6.0.0.0" | |
processorArchitecture="*" | |
publicKeyToken="6595b64144ccf1df" | |
language="*" | |
/> | |
</dependentAssembly> | |
</dependency> | |
--> | |
</assembly> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace DrawFramerateBoard | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Bitmap dummy = new Bitmap(1, 1); | |
Graphics gd = Graphics.FromImage(dummy); | |
IntPtr ipHdc = gd.GetHdc(); | |
Pen penGray = new Pen(Color.Gray, 5); | |
Pen penRed = new Pen(Color.Red, 5); | |
Pen penBlue = new Pen(Color.Blue, 5); | |
int width = 1024; | |
int height = 1024; | |
int r1 = 500; | |
int r2 = 500; | |
int r3 = 100; | |
int cx = width / 2; | |
int cy = height / 2; | |
// 保存ファイル名、画像サイズを指定して初期化 | |
Metafile metafile = new Metafile("circle.emf", ipHdc, new Rectangle(0, 0, width, height), MetafileFrameUnit.Pixel); | |
// 描画の仕方は Bitmap の時と同じ | |
using (Graphics g = Graphics.FromImage(metafile)) | |
{ | |
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; | |
for(int i = 0; i < 120; i++) | |
{ | |
float x1 = (float)(cx + r2 * Math.Cos(i / 120f * 2f * Math.PI)); | |
float y1 = (float)(cy + r2 * Math.Sin(i / 120f * 2f * Math.PI)); | |
float x2 = (float)(cx + r3 * Math.Cos(i / 120f * 2f * Math.PI)); | |
float y2 = (float)(cy + r3 * Math.Sin(i / 120f * 2f * Math.PI)); | |
g.DrawLine(penGray, x1, y1, x2, y2); | |
} | |
for (int i = 0; i < 12; i++) | |
{ | |
float x1 = (float)(cx + r2 * Math.Cos(i / 12f * 2f * Math.PI)); | |
float y1 = (float)(cy + r2 * Math.Sin(i / 12f * 2f * Math.PI)); | |
float x2 = (float)(cx + r3 * Math.Cos(i / 12f * 2f * Math.PI)); | |
float y2 = (float)(cy + r3 * Math.Sin(i / 12f * 2f * Math.PI)); | |
g.DrawLine(penBlue, x1, y1, x2, y2); | |
} | |
g.DrawEllipse(penGray, new Rectangle(cx - r1, cy - r1, r1 * 2, r1 * 2)); | |
g.DrawLine(penGray, cx - 10, cy - 10, cx + 10, cy + 10); | |
g.DrawLine(penGray, cx + 10, cy - 10, cx - 10, cy + 10); | |
g.DrawLine(penRed, cx, cy, cx, cy - r1); | |
} | |
// Disposeを忘れずに | |
metafile.Dispose(); | |
gd.ReleaseHdc(ipHdc); | |
gd.Dispose(); | |
dummy.Dispose(); | |
penGray.Dispose(); | |
penRed.Dispose(); | |
penBlue.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment