/// <summary> /// 指定したサイズの、空の描画可能画像を作成する。 /// </summary> public 描画可能画像(SharpDX.Direct3D11.Device1 d3dDevice1, SharpDX.Direct2D1.DeviceContext d2dDeviceContext, Size2F サイズ, BindFlags bindFlags = BindFlags.ShaderResource) : base(d3dDevice1, サイズ, bindFlags | BindFlags.RenderTarget) { //using var _ = new LogBlock( Log.現在のメソッド名 ); this.Bitmap = this._作成したテクスチャとデータを共有するビットマップターゲットを作成する(d2dDeviceContext); }
/// <summary> /// Конструктор /// </summary> /// <param name="BackBuffer">Буффер на который будем рисовать, наш холст</param> /// <param name="Width">Ширина области в которую будем рисовать</param> /// <param name="Height">Высота объласти в которую будем рисовать</param> public TextWirter(SharpDX.Direct3D11.Device2 device, SwapChain2 swapChain, Color color, float dpi = 96f, string font = "Calibri", int size = 14) { #if DEBUG var debug = SharpDX.Direct2D1.DebugLevel.Error; #else var debug = SharpDX.Direct2D1.DebugLevel.None; #endif _Factory2D = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debug); using (var dxgiDevice = device.QueryInterface <SharpDX.DXGI.Device>()) { d2dDevice = new SharpDX.Direct2D1.Device(_Factory2D, dxgiDevice); } _RenderTarget2D = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); BitmapProperties1 properties = new BitmapProperties1( new PixelFormat( SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), dpi, dpi, BitmapOptions.Target | BitmapOptions.CannotDraw); Surface backBuffer = swapChain.GetBackBuffer <Surface>(0); d2dTarget = new Bitmap1(_RenderTarget2D, backBuffer, properties); this.TextFont = font; this.TextSize = size; _FactoryDWrite = new SharpDX.DirectWrite.Factory(); _SceneColorBrush = new SolidColorBrush(_RenderTarget2D, color); InitTextFormat(); _RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype; }
private async void MainWindow_Load(object sender, EventArgs e) { if (_dxgiDevice != null) { CleanUp(); } _dxgiDevice = DxgiDevice.CreateDevice(); _factory = Direct2DFactory.CreateFactory(FactoryType.SingleThreaded, DebugLevel.None); using (Device device = _factory.CreateDevice(_dxgiDevice)) { _deviceContext = device.CreateDeviceContext(DeviceContextOptions.None); using (DxgiAdapter adapter = _dxgiDevice.GetAdapter()) using (DxgiFactory factory = adapter.GetFactory()) { _swapChain = factory.CreateSwapChainForHwnd(_dxgiDevice, Handle); _dxgiDevice.MaximumFrameLatency = 1; _swapChain.GetBuffer(0, out _surface); _bitmap = _deviceContext.CreateBitmapFromDxgiSurface(_surface); _deviceContext.SetTarget(_bitmap); _brush = _deviceContext.CreateSolidColorBrush(Color.FromRGB(0.1f, 0.75f, 0.5f, 1f)); _brush1 = _deviceContext.CreateSolidColorBrush(Color.FromRGB(0.1f, 0.5f, 0.75f, 1f)); } } _directWriteFactory = DirectWriteFactory.Create(DirectWriteFactoryType.Shared); await CreatePoints(); }
public static BitmapBrush1 GetBrush(D2D.DeviceContext context, Bitmap1 bitmap, ExtendMode mode) { return(new BitmapBrush1(context, bitmap) { ExtendModeX = mode, ExtendModeY = mode }); }
private Stream EncodeBitmap(Bitmap1 bitmap, Guid containerFormat) { using (var bitmapEncoder = new BitmapEncoder(WicFactory, containerFormat)) { var memoryStream = new MemoryStream(); bitmapEncoder.Initialize(memoryStream); using (var frameEncode = new BitmapFrameEncode(bitmapEncoder)) { frameEncode.Initialize(); var wicPixelFormat = ImagePixelFormat; frameEncode.SetPixelFormat(ref wicPixelFormat); var imageParams = new ImageParameters() { PixelFormat = DevicePixelFormat, DpiX = Dpi, DpiY = Dpi, PixelWidth = bitmap.PixelSize.Width, PixelHeight = bitmap.PixelSize.Height }; WicImageEncoder.WriteFrame(bitmap, frameEncode, imageParams); frameEncode.Commit(); bitmapEncoder.Commit(); } memoryStream.Position = 0; return(memoryStream); } }
/// <summary> /// 创建具有指定角度凸起效果的图像。 /// </summary> /// <param name="imageBrush">原始的拼图图像笔刷。</param> /// <param name="source">用于生成图像的源位图。</param> /// <param name="blacks">黑色图形集合。</param> /// <param name="whites">白色图形集合。</param> /// <param name="ct">取消任务的通知。</param> /// <returns>具有指定角度凸起效果的图像。</returns> private Bitmap CreateImage(Brush imageBrush, Bitmap1 source, IList <Geometry> blacks, IList <Geometry> whites, CancellationToken ct) { ct.ThrowIfCancellationRequested(); Bitmap1 target = null; try { target = DeviceManager.CreateBitmap(imageSize); // 一次性绘制所有不相邻的拼图碎片。 ct.ThrowIfCancellationRequested(); this.CreateImage(imageBrush, source, target, blacks, ct); ct.ThrowIfCancellationRequested(); this.CreateImage(imageBrush, source, target, whites, ct); return(target); } catch { if (target != null) { target.Dispose(); } throw; } }
private void MainWindow_Load(object sender, EventArgs e) { if (_dxgiDevice != null) { CleanUp(); } _dxgiDevice = DxgiDevice.CreateDevice(); _factory = Direct2DFactory.CreateFactory(FactoryType.SingleThreaded, DebugLevel.None); using (Device device = _factory.CreateDevice(_dxgiDevice)) { _deviceContext = device.CreateDeviceContext(DeviceContextOptions.None); using (DxgiAdapter adapter = _dxgiDevice.GetAdapter()) using (DxgiFactory factory = adapter.GetFactory()) { _swapChain = factory.CreateSwapChainForHwnd(_dxgiDevice, Handle); _dxgiDevice.MaximumFrameLatency = 1; _swapChain.GetBuffer(0, out _surface); _bitmap = _deviceContext.CreateBitmapFromDxgiSurface(_surface); _deviceContext.SetTarget(_bitmap); } for (int index = 0; index < _brushes.Length; ++index) { _pens[index] = _deviceContext.CreateSolidColorBrush(GetRandomColor(_random, 1)); _brushes[index] = _deviceContext.CreateSolidColorBrush(GetRandomColor(_random, 0.25f)); } } _directWriteFactory = DirectWriteFactory.Create(DirectWriteFactoryType.Shared); }
public WindowedDirect2dRenderEnvironment(string formName, bool debug) { form = new RenderForm(formName); d3dDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport | (debug ? DeviceCreationFlags.Debug : DeviceCreationFlags.None)); dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device>(); dxgiFactory = new SharpDX.DXGI.Factory1(); swapChain = new SwapChain(dxgiFactory, dxgiDevice, new SwapChainDescription { BufferCount = 1, ModeDescription = new ModeDescription(Format.B8G8R8A8_UNorm), OutputHandle = form.Handle, IsWindowed = true, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }); dxgiSurface = swapChain.GetBackBuffer <Surface>(0); d2dFactory = new SharpDX.Direct2D1.Factory1(FactoryType.SingleThreaded, debug ? DebugLevel.Warning : DebugLevel.None); d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, DeviceContextOptions.None); bitmap = new Bitmap1(d2dContext, dxgiSurface, null); d2dContext.Target = bitmap; }
private void CreateDeviceContext() { _deviceContext = new DeviceContext(Direct2D1Platform.Direct2D1Device, DeviceContextOptions.None) { DotsPerInch = _savedDpi }; if (_swapChain == null) { CreateSwapChain(); } using (var dxgiBackBuffer = _swapChain.GetBackBuffer <Surface>(0)) using (var d2dBackBuffer = new Bitmap1( _deviceContext, dxgiBackBuffer, new BitmapProperties1( new SharpDX.Direct2D1.PixelFormat { AlphaMode = SharpDX.Direct2D1.AlphaMode.Premultiplied, Format = Format.B8G8R8A8_UNorm }, _savedSize.Width, _savedSize.Height, BitmapOptions.Target | BitmapOptions.CannotDraw))) { _deviceContext.Target = d2dBackBuffer; } }
public void CaptureScreenShotAsPng(Stream stream) { using var cpuBitmap = new Bitmap1(_d2D1DeviceContext, _d2D1DeviceContext.PixelSize, new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), _d2D1DeviceContext.DotsPerInch.Width, _d2D1DeviceContext.DotsPerInch.Height, BitmapOptions.CpuRead | BitmapOptions.CannotDraw)); cpuBitmap.CopyFromRenderTarget(_d2D1DeviceContext); var dataRectangle = cpuBitmap.Surface.Map(MapFlags.Read, out var dataStream); try { var surfaceDescription = cpuBitmap.Surface.Description; using (dataStream) { using var gdiBitmap = new Bitmap(surfaceDescription.Width, surfaceDescription.Height); for (var y = 0; y < surfaceDescription.Height; y++) { for (var x = 0; x < surfaceDescription.Width; x++) { dataStream.Seek((y * dataRectangle.Pitch) + (x * sizeof(int)), SeekOrigin.Begin); var pixel = dataStream.Read <int>(); gdiBitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(pixel)); } } gdiBitmap.Save(stream, ImageFormat.Png); } } finally { cpuBitmap.Surface.Unmap(); } }
public override void Update(D3D11Device device) { var rt = SetRenderTarget(device); if (_bitmap == null) { var pf = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied); var bp = new BitmapProperties1(pf, device.Dpi.Height, device.Dpi.Width, BitmapOptions.CannotDraw | BitmapOptions.Target) ; using (var surface = rt.Surface) { _bitmap = new Bitmap1(device.D2DDeviceContext, surface, bp); } } device.D2DDeviceContext.Target = _bitmap; device.D2DDeviceContext.BeginDraw(); { _scene.Draw(device.D2DDeviceContext, new SharpDX.RectangleF(0, 0, _rect.Width, _rect.Height)); } device.D2DDeviceContext.EndDraw(); device.D2DDeviceContext.Target = null; }
void CreateDeviceResources() { var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); var device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); var dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>(); var dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>(); var desc = new SwapChainDescription1(); desc.Width = 480; desc.Height = 640; desc.Format = Format.B8G8R8A8_UNorm; desc.Stereo = false; desc.SampleDescription = new SampleDescription(1, 0); desc.Usage = Usage.RenderTargetOutput; desc.BufferCount = 2; desc.Scaling = Scaling.AspectRatioStretch; desc.SwapEffect = SwapEffect.FlipSequential; desc.Flags = SwapChainFlags.AllowModeSwitch; this.swapChain = new SwapChain1(dxgiFactory2, device, new ComObject(mWindow), ref desc); var d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); this.d2dDeviceContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, DeviceContextOptions.None); var backBuffer = this.swapChain.GetBackBuffer <Surface>(0); var displayInfo = DisplayInformation.GetForCurrentView(); this.d2dTarget = new Bitmap1(this.d2dDeviceContext, backBuffer, new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), displayInfo.LogicalDpi, displayInfo.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw)); this.updateList = new List <IUpdatable>(); // 10.5改変 this.playerShotManager = new PlayerShotManager(this.d2dDeviceContext); this.updateList.Add(this.playerShotManager); this.fighterDisplay = new Fighter(this.d2dDeviceContext, playerShotManager); this.fighterDisplay.SetPosition(540, 240); this.displayList = new List <IDrawable>(); this.displayList.Add(this.fighterDisplay); this.displayList.Add(this.playerShotManager); this.targetManager = new RectTargetManager(this.d2dDeviceContext, this.playerShotManager); this.displayList.Add(this.targetManager); this.updateList.Add(this.targetManager); this.enemyShotManager = new EnemyShotManager(this.d2dDeviceContext, this.targetManager, this.fighterDisplay); this.displayList.Add(this.enemyShotManager); this.updateList.Add(this.enemyShotManager); }
public void Connect() { using (DXGI.Surface surface = Dxgi().SwapChain.GetBackBuffer <DXGI.Surface>(0)) using (var bitmap = new Bitmap1(Context, surface)) { Context.Target = bitmap; } }
/// <summary> /// Initializes a new instance of the <see cref="BitmapProxy"/> class. /// </summary> /// <param name="name">The name.</param> /// <param name="context">The context.</param> /// <param name="surface">The surface.</param> /// <param name="properties">The properties.</param> public BitmapProxy(string name, DeviceContext context, Surface surface, BitmapProperties1 properties) { Properties = properties; Context = context; bitmap = new Bitmap1(context, surface, properties); Size = new Size2((int)Bitmap.Size.Width, (int)Bitmap.Size.Height); Name = name; }
internal override void Initialize(DeviceContext context) { var d3D11Texture2D = (Direct3D11.Texture2D) this.texture2D; var surface = d3D11Texture2D.QueryInterface <Surface>(); bitmap1 = ToDispose(new Bitmap1(context, surface)); base.Initialize(context); }
/// <summary> /// Initializes a new instance of the <see cref="BitmapProxy"/> class. /// </summary> /// <param name="name">The name.</param> /// <param name="context">The context.</param> /// <param name="size">The size.</param> /// <param name="properties">The properties.</param> public BitmapProxy(string name, DeviceContext context, Size2 size, BitmapProperties1 properties) { Properties = properties; Context = context; bitmap = new Bitmap1(context, size, properties); Size = size; Name = name; }
public static void ConnectContextToDxgiSwapChain( D2D1MetaResource <IDxgi1_2ContainerWithSwapChain> d2D1MetaResource) { using (var surface = d2D1MetaResource.DxgiContainer.SwapChain1.GetBackBuffer <Surface>(0)) { using (var bitmap = new Bitmap1(d2D1MetaResource.Context, surface)) d2D1MetaResource.Context.Target = bitmap; } }
private void PlatformConstruct(GraphicsDevice2D graphicsDevice, Texture targetTexture) { _graphicsDevice = graphicsDevice; using (var dxgiSurface = targetTexture.DeviceResource.QueryInterface <Surface>()) { _bitmap = AddDisposable(new Bitmap1(graphicsDevice.DeviceContext, dxgiSurface)); } }
public SharpDX.Direct2D1.DeviceContext1 CreateDrawingContext( Bitmap1 renderTarget, DeviceContextOptions options = DeviceContextOptions.None) { var drawingContext = new SharpDX.Direct2D1.DeviceContext1(d2dDevice, options); drawingContext.Target = renderTarget; return(drawingContext); }
public static Bitmap1 LoadBitmap(string filename, DeviceContext context) { var props = new BitmapProperties1 { PixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied) }; return(Bitmap1.FromWicBitmap(context, LoadBitmap(filename), props)); }
private void _graphicsDeviceManager_DeviceChangeEnd(object sender, EventArgs e) { GraphicsDevice graphicsDevice = _game.GraphicsDevice; RenderTarget2D backBuffer = graphicsDevice.BackBuffer; _renderTarget2D = ToDispose(RenderTarget2D.New(graphicsDevice, backBuffer.Width, backBuffer.Height, backBuffer.Format)); _bitmap1 = ToDispose(new Bitmap1(DeviceContext, _renderTarget2D)); State = CanvasState.Refresh; }
private void LoadCircuitBoard(Image image) { circuitBoard = CircuitBoard.FromImage(image); update = true; bitmap?.Dispose(); bitmap = new Bitmap1(Dx.D2D.Context, new Size2(image.Width, image.Height), new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Ignore))); ResetTransformation(); }
public static Bitmap1 LoadBitmap(SharpDX.Direct3D11.DeviceContext device, string filename) { var props = new BitmapProperties1 { PixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied) }; return(Bitmap1.FromWicBitmap(device.QueryInterface <SharpDX.Direct2D1.DeviceContext>(), LoadBitmapSource(device, filename), props)); }
/// <summary> /// Update all resources /// </summary> /// <param name="backBuffer">BackBuffer</param> internal void UpdateResources(Texture2D11 backBuffer) { using (var surface2D = backBuffer.QueryInterface <Surface>()) { var dpi = NativeFactory.DesktopDpi; Target = new Bitmap1(NativeDeviceContext, surface2D, new BitmapProperties1(new PixelFormat( Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied), dpi.Height, dpi.Width, BitmapOptions.CannotDraw | BitmapOptions.Target)); NativeDeviceContext.Target = Target; } }
private void CreateDeviceContext() { using (var surface = SwapChain.GetBackBuffer <Surface>(0)) { DeviceContext = new DeviceContext2D1(Device2D, DeviceContextOptions.None); using (var bitmap = new Bitmap1(DeviceContext, surface)) { DeviceContext.Target = bitmap; } } }
public override void Dispose() { _scene.Dispose(); if (_bitmap != null) { _bitmap.Dispose(); _bitmap = null; } base.Dispose(); }
private void SetupResources() { this.backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); this.targetView = new RenderTargetView(this.device, this.backBuffer); this.surface = this.backBuffer.QueryInterface <Surface1>(); this.painter = new SharpDX.Direct2D1.DeviceContext(this.surface); this.bitmap = new Bitmap1(this.painter, this.surface); this.painter.Target = this.bitmap; device.ImmediateContext.OutputMerger.SetRenderTargets(targetView); }
public Image(Stream stream, DeviceContext context) { var factory = new ImagingFactory(); var decoder = new BitmapDecoder(factory, stream, DecodeOptions.CacheOnLoad); BitmapFrameDecode frame = decoder.GetFrame(0); FormatConverter converter = new FormatConverter(factory); converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA); var newBitmap = Bitmap1.FromWicBitmap(context, converter); }
private void CreateSwapChain() { using (var dxgiAdaptor = DxgiDevice.Adapter) using (var dxgiFactory = dxgiAdaptor.GetParent <Factory2>()) { _deviceContext?.Dispose(); _deviceContext = new DeviceContext(D2DDevice, DeviceContextOptions.None) { DotsPerInch = _savedDpi }; var swapChainDesc = new SwapChainDescription1 { Width = _savedSize.Width, Height = _savedSize.Height, Format = Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new SampleDescription { Count = 1, Quality = 0, }, Usage = Usage.RenderTargetOutput, BufferCount = 2, Scaling = Scaling.None, SwapEffect = SwapEffect.FlipSequential, Flags = 0, }; _swapChain?.Dispose(); _swapChain = CreateSwapChain(dxgiFactory, swapChainDesc); using (var dxgiBackBuffer = _swapChain.GetBackBuffer <Surface>(0)) using (var d2dBackBuffer = new Bitmap1( _deviceContext, dxgiBackBuffer, new BitmapProperties1( new PixelFormat { AlphaMode = AlphaMode.Ignore, Format = Format.B8G8R8A8_UNorm }, _savedDpi.Width, _savedDpi.Height, BitmapOptions.Target | BitmapOptions.CannotDraw))) { _deviceContext.Target = d2dBackBuffer; } } }
/// <summary> /// 准备渲染拼图碎片。 /// </summary> /// <param name="imageData">拼图使用的图片数据。</param> /// <param name="pieces">所有拼图碎片的集合。</param> /// <param name="rotatable">拼图碎片是否可以旋转。</param> /// <param name="ct">取消任务的通知。</param> public override void PrepareRender(byte[] imageData, JigsawPieceCollection pieces, bool rotatable, CancellationToken ct) { base.PrepareRender(imageData, pieces, rotatable, ct); ct.ThrowIfCancellationRequested(); imageSize = new Size2((int)this.Image.Size.Width, (int)this.Image.Size.Height); using (Brush imageBrush = new BitmapBrush(this.DeviceContext, this.Image)) { using (Bitmap1 source = DeviceManager.CreateBitmap(imageSize)) { ct.ThrowIfCancellationRequested(); // 设置特效的输入。 bevelBaseEffect.SetInput(0, source, true); bevelEffect.SetInput(0, source, true); // 分别按黑色与白色创建拼图碎片特效。 List <Geometry> blacks = new List <Geometry>(); List <Geometry> whites = new List <Geometry>(); foreach (JigsawPiece piece in pieces) { Geometry[] geoms = piece.OriginalPath.GetSourceGeometry(); // 划分黑色与白色,他们分别是不相邻的。 bool[] colors = piece.GetColors(); for (int i = 0; i < colors.Length; i++) { if (colors[i]) { blacks.Add(geoms[i]); } else { whites.Add(geoms[i]); } } } ct.ThrowIfCancellationRequested(); pointSpecularEffect.LightPosition = new Vector3(0, 0, 0); this.images[0] = this.CreateImage(imageBrush, source, blacks, whites, ct); if (rotatable) { // 生成不同旋转角度的图像,使用不同的光照情况。 pointSpecularEffect.LightPosition = new Vector3(0, imageSize.Height, 0); this.images[1] = this.CreateImage(imageBrush, source, blacks, whites, ct); pointSpecularEffect.LightPosition = new Vector3(imageSize.Width, imageSize.Height, 0); this.images[2] = this.CreateImage(imageBrush, source, blacks, whites, ct); pointSpecularEffect.LightPosition = new Vector3(imageSize.Width, 0, 0); this.images[3] = this.CreateImage(imageBrush, source, blacks, whites, ct); } } } }