2 using System.Collections.Generic;
3 using System.Diagnostics;
8 public static class Utilities
11 /// Dispose して、null を代入する。
13 /// <param name="obj">IDisposable を実装するオブジェクト。</param>
14 public static void 解放する<T>( ref T obj ) where T : IDisposable
16 ( obj as IDisposable )?.Dispose();
21 /// 画像ファイルからシェーダリソースビューを作成して返す。
22 /// D3DX11CreateShaderResourceViewFromFile() の代わり。
24 public static SharpDX.Direct3D11.ShaderResourceView CreateShaderResourceViewFromFile(
25 SharpDX.Direct3D11.Device d3dDevice, string 画像ファイルパス, out SharpDX.Size2F ビューのサイズ )
27 ビューのサイズ = new SharpDX.Size2F( 0, 0 );
28 var srv = (SharpDX.Direct3D11.ShaderResourceView) null;
29 using( var image = new System.Drawing.Bitmap( 画像ファイルパス ) )
31 var 画像の矩形 = new System.Drawing.Rectangle( 0, 0, image.Width, image.Height );
32 using( var bitmap = image.Clone( 画像の矩形, System.Drawing.Imaging.PixelFormat.Format32bppArgb ) )
34 var ロック領域 = bitmap.LockBits( 画像の矩形, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat );
35 var dataBox = new[] { new SharpDX.DataBox( ロック領域.Scan0, bitmap.Width * 4, bitmap.Height ) };
36 var textureDesc = new SharpDX.Direct3D11.Texture2DDescription() {
38 BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
39 CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
40 Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
41 Height = bitmap.Height,
44 OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
45 SampleDescription = new SharpDX.DXGI.SampleDescription( 1, 0 ),
46 Usage = SharpDX.Direct3D11.ResourceUsage.Default
48 using( var texture = new SharpDX.Direct3D11.Texture2D( d3dDevice, textureDesc, dataBox ) )
50 bitmap.UnlockBits( ロック領域 );
51 srv = new SharpDX.Direct3D11.ShaderResourceView( d3dDevice, texture );
55 ビューのサイズ = new SharpDX.Size2F( 画像の矩形.Width, 画像の矩形.Height );