■ ShartDX를 사용해 WinForm 컨트롤 표면에 DirectX 비트맵을 그리는 방법을 보여준다.
▶ DXHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
using System; using System.Windows.Forms; using SharpDX.Direct2D1; using SharpDX.Direct3D; using SharpDX.Direct3D11; using SharpDX.DXGI; using SharpDX.Mathematics.Interop; using SharpDX.WIC; namespace TestProject { /// <summary> /// 다이렉트 X 헬퍼 /// </summary> public class DXHelper : IDisposable { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 팩토리 2 /// </summary> private SharpDX.Direct2D1.Factory2 factory2 = null; /// <summary> /// 장치 /// </summary> private SharpDX.Direct3D11.Device device = null; /// <summary> /// 스와프 체인 /// </summary> private SwapChain swapChain = null; /// <summary> /// 렌더링 타겟 /// </summary> private RenderTarget renderTarget = null; /// <summary> /// 비트맵 /// </summary> private SharpDX.Direct2D1.Bitmap bitmap = null; /// <summary> /// 타겟 사각형 /// </summary> private RawRectangleF targetRectangle; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 픽처 박스 초기화하기 - InitializePictureBox(pictureBox) /// <summary> /// 픽처 박스 초기화하기 /// </summary> /// <param name="pictureBox">픽처 박스</param> public void InitializePictureBox(PictureBox pictureBox) { SwapChainDescription swapChainDescription = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription ( pictureBox.Width, pictureBox.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm ), IsWindowed = true, OutputHandle = pictureBox.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; this.targetRectangle.Left = 0; this.targetRectangle.Top = 0; this.targetRectangle.Right = pictureBox.Width; this.targetRectangle.Bottom = pictureBox.Height; SharpDX.Direct3D11.Device.CreateWithSwapChain ( DriverType.Hardware, DeviceCreationFlags.BgraSupport, swapChainDescription, out this.device, out this.swapChain ); this.factory2 = new SharpDX.Direct2D1.Factory2(); Surface backSurface = Surface.FromSwapChain(this.swapChain, 0); RenderTargetProperties properties = new RenderTargetProperties ( new SharpDX.Direct2D1.PixelFormat ( Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied ) ); this.renderTarget = new RenderTarget(this.factory2, backSurface, properties); } #endregion #region 비트맵 로드하기 - LoadBitmap(filePath) /// <summary> /// 비트맵 로드하기 /// </summary> /// <param name="filePath">파일 경로</param> public void LoadBitmap(string filePath) { ImagingFactory2 imagingFactory2 = new ImagingFactory2(); BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory2, filePath, DecodeOptions.CacheOnDemand); BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0); FormatConverter formatConverter = new FormatConverter(imagingFactory2); formatConverter.Initialize(bitmapFrameDecode, SharpDX.WIC.PixelFormat.Format32bppPRGBA); this.bitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(this.renderTarget, formatConverter); formatConverter.Dispose(); bitmapFrameDecode.Dispose(); bitmapDecoder.Dispose(); imagingFactory2.Dispose(); } #endregion #region 렌더링하기 - Render() /// <summary> /// 렌더링하기 /// </summary> public void Render() { this.renderTarget.BeginDraw(); this.renderTarget.Clear(new RawColor4(1.0f, 1.0f, 1.0f, 1.0f)); this.renderTarget.DrawBitmap(this.bitmap, this.targetRectangle, 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear); //this.renderTarget.DrawBitmap(this.bitmap, 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear); this.renderTarget.EndDraw(); this.swapChain.Present(1, PresentFlags.None); } #endregion #region 리소스 해제하기 - Dispose() /// <summary> /// 리소스 해제하기 /// </summary> public void Dispose() { this.renderTarget?.Dispose(); this.renderTarget = null; this.swapChain?.Dispose(); this.swapChain = null; this.device?.Dispose(); this.device = null; this.factory2?.Dispose(); this.factory2 = null; this.bitmap?.Dispose(); this.bitmap = null; } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
using System; using System.Drawing; using System.Threading; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// DX 헬퍼 /// </summary> private DXHelper dxHelper; /// <summary> /// 비트맵 /// </summary> private Bitmap bitmap = null; /// <summary> /// 그래픽스 /// </summary> private Graphics graphics = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.dxHelper = new DXHelper(); this.dxHelper.InitializePictureBox(this.pictureBox); this.graphics = this.pictureBox.CreateGraphics(); FormClosing += Form_FormClosing; Load += Form_Load; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { this.dxHelper.LoadBitmap("IMAGE\\sample.png"); Thread thread = new Thread(ProcessRender); thread.Start(); } #endregion #region 폼을 닫을 경우 처리하기 - Form_FormClosing(sender, e) /// <summary> /// 폼을 닫을 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_FormClosing(object sender, FormClosingEventArgs e) { this.dxHelper?.Dispose(); this.dxHelper = null; this.bitmap?.Dispose(); this.bitmap = null; } #endregion //////////////////////////////////////////////////////////////////////////////// Event #region 렌더링 처리하기 - ProcessRender() /// <summary> /// 렌더링 처리하기 /// </summary> private void ProcessRender() { Thread.Sleep(30); this.dxHelper.Render(); } #endregion } } |