private void CreateD2DRenderTargets() { // Create a D2D render target which can draw into our offscreen D3D surface textureRenderTarget = d2DFactory.CreateGraphicsSurfaceRenderTarget( textureSurface, renderTargetProperties ); // Create a linear gradient brush for the 2D geometry GradientStopCollection gradientStops = textureRenderTarget.CreateGradientStopCollection(stopsGeometry, Gamma.StandardRgb, ExtendMode.Mirror); linearGradientBrush = textureRenderTarget.CreateLinearGradientBrush( new LinearGradientBrushProperties( new Point2F(100, 0), new Point2F(100, 200)), gradientStops ); // create a black brush blackBrush = textureRenderTarget.CreateSolidColorBrush(new ColorF(GetColorValues(System.Windows.Media.Colors.Black))); using (Stream stream = Application.ResourceAssembly.GetManifestResourceStream("Microsoft.WindowsAPICodePack.DirectX.Samples.tulip.jpg")) { d2dBitmap = BitmapUtilities.LoadBitmapFromStream( textureRenderTarget, imagingFactory, stream); } gridPatternBitmapBrush = CreateGridPatternBrush(textureRenderTarget); gridPatternBitmapBrush.Opacity = 0.5f; CreateBackBufferD2DRenderTarget(); }
public TextShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap, DWriteFactory dwriteFactory) : base(initialRenderTarget, random, d2DFactory, bitmap) { this.dwriteFactory = dwriteFactory; layoutRect = RandomRect(CanvasWidth, CanvasHeight); NiceGabriola = Random.NextDouble() < 0.25 && dwriteFactory.SystemFontFamilyCollection.Contains("Gabriola"); TextFormat = dwriteFactory.CreateTextFormat( RandomFontFamily(), RandomFontSize(), RandomFontWeight(), RandomFontStyle(), RandomFontStretch(), System.Globalization.CultureInfo.CurrentUICulture); if (CoinFlip) TextFormat.LineSpacing = RandomLineSpacing(TextFormat.FontSize); Text = RandomString(Random.Next(1000, 1000)); FillBrush = RandomBrush(); RenderingParams = RandomRenderingParams(); if (CoinFlip) { Options = DrawTextOptions.None; if (CoinFlip) Options |= DrawTextOptions.Clip; if (CoinFlip) Options |= DrawTextOptions.NoSnap; } }
internal BitmapShape(Paint2DForm parent, RectF rect, D2DBitmap bitmap, float transparency) : base(parent) { _rect = rect; _bitmap = bitmap; _transparency = transparency; }
public static void DrawBitmap(this SharpDX.Direct2D1.RenderTarget target, D2DBitmap bitmap, float destX, float destY, float destWidth, float destHeight, float srcX, float srcY, float srcWidth, float srcHeight, float opacity) { var destRect = new RawRectangleF(destX, destY, destX + destWidth, destY + destHeight); var srcRect = new RawRectangleF(srcX, srcY, srcX + srcWidth, srcY + srcHeight); target.DrawBitmap(bitmap.NativeBitmap, destRect, opacity, BitmapInterpolationMode.Linear, srcRect); }
private void GetImageFromPath(RenderTarget pRenderer, string pPath) { using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(pPath)) { System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat ); Size = new Size(bitmap.Width, bitmap.Height); //// Declare an array to hold the bytes of the bitmap. //IntPtr ptr = bmpData.Scan0; //byte[] bytes = new byte[bmpData.Stride * bitmap.Height]; // Unlock the bits. bitmap.UnlockBits(bmpData); } BitmapDecoder decoder = ImagingFactory.CreateDecoderFromFilename(pPath, DesiredAccess.Read, DecodeMetadataCacheOptions.OnDemand); BitmapFrameDecode frameDeocder = decoder.GetFrame(0); WICFormatConverter formatConverter = ImagingFactory.CreateFormatConverter(); BitmapSource src = frameDeocder.ToBitmapSource(); formatConverter.Initialize(frameDeocder.ToBitmapSource(), PixelFormats.Pf32bppPBGRA, BitmapDitherType.None, BitmapPaletteType.MedianCut); ImageResource = pRenderer.CreateBitmapFromWicBitmap(formatConverter.ToBitmapSource()); }
internal BitmapShape(Paint2DForm parent, RectF rect, D2DBitmap bitmap, float transparency) : base(parent) { _rect = rect; _bitmap = bitmap; _transparency = transparency; }
protected override void OnLoad(EventArgs e) { D2DBitmap[] bmps = new D2DBitmap[] { this.Device.CreateBitmapFromGDIBitmap(Properties.Resources.icons8_car_production_96), this.Device.CreateBitmapFromGDIBitmap(Properties.Resources.icons8_himeji_castle_96), this.Device.CreateBitmapFromGDIBitmap(Properties.Resources.icons8_home_page_96), this.Device.CreateBitmapFromGDIBitmap(Properties.Resources.icons8_music_96), this.Device.CreateBitmapFromGDIBitmap(Properties.Resources.icons8_uwu_emoji_96), }; // Try this! // // High performance drawing by convert bitmap to D2DBitmap // for (int i = 0; i < 1000; i++) { icons.Add(new IconInfo(bmps[rand.Next(bmps.Length)], this.ClientRectangle)); } // Try this! // // Low performance drawing using original GDI+ bitmap // //for (int i = 0; i < 1000; i++) //{ // icons.Add(new IconInfo(bmps[rand.Next(bmps.Length)], this.ClientRectangle)); //} }
public MeshShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap) : base(initialRenderTarget, random, d2DFactory, bitmap) { FillBrush = RandomBrush(); mesh = CoinFlip ? MeshFromRandomGeometry() : MeshFromRandomTriangles(); }
BitmapBrush CreateGridPatternBrush(RenderTarget pRenderTarget) { // Create a compatible render target. BitmapRenderTarget spCompatibleRenderTarget = pRenderTarget.CreateCompatibleRenderTarget(CompatibleRenderTargetOptions.None, (new SizeF(10.0f, 10.0f))); // Draw a pattern. SolidColorBrush spGridBrush = spCompatibleRenderTarget.CreateSolidColorBrush( new ColorF(0.93f, 0.94f, 0.96f, 1.0f)); spCompatibleRenderTarget.BeginDraw(); spCompatibleRenderTarget.FillRectangle(new RectF(0.0f, 0.0f, 10.0f, 1.0f), spGridBrush); spCompatibleRenderTarget.FillRectangle(new RectF(0.0f, 0.1f, 1.0f, 10.0f), spGridBrush); spCompatibleRenderTarget.EndDraw(); // Retrieve the bitmap from the render target. D2DBitmap spGridBitmap = spCompatibleRenderTarget.Bitmap; // Choose the tiling mode for the bitmap brush. BitmapBrushProperties brushProperties = new BitmapBrushProperties(ExtendMode.Wrap, ExtendMode.Wrap, BitmapInterpolationMode.Linear); // Create the bitmap brush. return(textureRenderTarget.CreateBitmapBrush(spGridBitmap, brushProperties)); }
protected DrawingShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap) { renderTarget = initialRenderTarget; Random = random; this.d2DFactory = d2DFactory; this.bitmap = bitmap; shapeID = ++shapesCreated; }
protected DrawingShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap) { renderTarget = initialRenderTarget; Random = random; this.d2DFactory = d2DFactory; this.bitmap = bitmap; shapeID = ++shapesCreated; }
public LineShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap) : base(initialRenderTarget, random, d2DFactory, bitmap) { point0 = RandomPoint(); point1 = RandomPoint(); PenBrush = RandomBrush(); StrokeWidth = RandomStrokeWidth(); if (CoinFlip) StrokeStyle = RandomStrokeStyle(); }
private void saveButton_Click(object sender, EventArgs e) { if (renderTarget == null) { // Should not happen MessageBox.Show("Unable to save file."); return; } SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.Filter = "Bitmap image (*.bmp)|*.bmp|Png image (*.png)|*.png|Jpeg image (*.jpg)|*.jpg|Gif image (*.gif)|*.gif"; if (DialogResult.OK == saveDlg.ShowDialog()) { SizeU size = new SizeU((uint)ClientSize.Width, (uint)ClientSize.Height); ImagingBitmap wicBitmap = wicFactory.CreateImagingBitmap( size.Width, size.Height, PixelFormats.Bgr32Bpp, BitmapCreateCacheOption.CacheOnLoad); D2DBitmap d2dBitmap = renderTarget.CreateBitmap(size, new BitmapProperties(new PixelFormat(Microsoft.WindowsAPICodePack.DirectX.Graphics.Format.B8G8R8A8UNorm, AlphaMode.Ignore), renderTarget.Dpi.X, renderTarget.Dpi.Y)); d2dBitmap.CopyFromRenderTarget(renderTarget); RenderTarget wicRenderTarget = d2dFactory.CreateWicBitmapRenderTarget(wicBitmap, renderProps); wicRenderTarget.BeginDraw(); wicRenderTarget.DrawBitmap(d2dBitmap); wicRenderTarget.EndDraw(); Guid fileType; switch (saveDlg.FilterIndex) { case 1: fileType = ContainerFormats.Bmp; break; case 2: fileType = ContainerFormats.Png; break; case 3: fileType = ContainerFormats.Jpeg; break; case 4: fileType = ContainerFormats.Gif; break; default: fileType = ContainerFormats.Bmp; // default to bitmap files break; } wicBitmap.SaveToFile(wicFactory, fileType, saveDlg.FileName); } }
internal static D2DBitmap LoadBitmapFromFile( RenderTarget renderTarget, ImagingFactory wicFactory, string fileName) { BitmapDecoder decoder = wicFactory.CreateDecoderFromFileName(fileName, DesiredAccess.Read, DecodeMetadataCacheOption.OnLoad); D2DBitmap ret = CreateBitmapFromDecoder(renderTarget, wicFactory, decoder); decoder.Dispose(); return(ret); }
internal static D2DBitmap LoadBitmapFromStream( RenderTarget renderTarget, ImagingFactory wicFactory, Stream ioStream) { BitmapDecoder decoder = wicFactory.CreateDecoderFromStream(ioStream, DecodeMetadataCacheOption.OnLoad); D2DBitmap ret = CreateBitmapFromDecoder(renderTarget, wicFactory, decoder); decoder.Dispose(); return(ret); }
public BitmapShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap) : base(initialRenderTarget, random, d2DFactory, bitmap) { DestRect = RandomRect(CanvasWidth, CanvasHeight); opacity = RandomOpacity(); DrawSection = Random.NextDouble() < 0.25; if (drawSection) SourceRect = RandomRect( Bitmap.PixelSize.Width, Bitmap.PixelSize.Height); }
public ImageInlineObject(RenderTarget renderTarget, ImagingFactory wicFactory, string resourceName) { _renderTarget = renderTarget; using (Stream stream = Application.ResourceAssembly.GetManifestResourceStream(resourceName)) { _bitmap = BitmapUtilities.LoadBitmapFromStream(renderTarget, wicFactory, stream); // Save the bitmap size, for faster access _bitmapSize = _bitmap.Size; } }
public ImageInlineObject(RenderTarget renderTarget, ImagingFactory wicFactory, string resourceName) { _renderTarget = renderTarget; using (Stream stream = Application.ResourceAssembly.GetManifestResourceStream(resourceName)) { _bitmap = BitmapUtilities.LoadBitmapFromStream(renderTarget, wicFactory, stream); // Save the bitmap size, for faster access _bitmapSize = _bitmap.Size; } }
public RoundRectangleShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap) : base(initialRenderTarget, random, d2DFactory, bitmap) { rect = RandomRoundedRect(); double which = Random.NextDouble(); if (which < 0.67) PenBrush = RandomBrush(); if (which > 0.33) FillBrush = RandomBrush(); if (CoinFlip) StrokeStyle = RandomStrokeStyle(); StrokeWidth = RandomStrokeWidth(); }
public GDIEllipsesShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap, int count) : base(initialRenderTarget, random, d2DFactory, bitmap) { for(int i = 0; i < count; i++) { ellipses.Add(RandomGdiEllipse()); } if (RenderTargetSupportsGDI(RenderTarget)) { gdiRenderTarget = RenderTarget.GetGdiInteropRenderTarget(); } }
public void UnloadGPU() { if (GLTextureID != 0) { int id = GLTextureID; Opengl32.glDeleteTextures(1, ref id); GLTextureID = 0; } if (D2DBitmap != null && !D2DBitmap.IsDisposed) { D2DBitmap?.Dispose(); } //D2DBitmap = null; }
protected override void OnCreateResources(RenderTarget target) { // We don't need to free any resources because the base class will // call OnFreeResources if necessary before calling this method. RedBrush = target.CreateSolidColorBrush(new ColorF(1, 0, 0)); //whiteBrush = RenderTarget.CreateSolidColorBrush(new ColorF(1, 1, 1)); _d2DBitmap = target.CreateBitmap(target.PixelSize, new BitmapProperties(new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.PixelFormat(Format.B8G8R8A8UNorm, AlphaMode.Premultiplied), 96, 96)); textFormat = writeFactory.CreateTextFormat("Arial", 12); base.OnCreateResources(target); // Call this last to start the animation }
public void Dispose() { if (IsDisposed) { return; } IsDisposed = true; GdiBitmap.Dispose(); UnloadGPU(); if (D2DBitmap != null && !D2DBitmap.IsDisposed) { D2DBitmap?.Dispose(); } D2DBitmap = null; }
private void bitmapButton_Click(object sender, EventArgs e) { if (bitmapDialog == null) { bitmapDialog = new OpenFileDialog(); bitmapDialog.DefaultExt = "*.jpg;*.png"; } if (bitmapDialog.ShowDialog() == DialogResult.OK) { string filename = bitmapDialog.FileName; currentBitmap = BitmapUtilities.LoadBitmapFromFile(renderTarget, wicFactory, filename); currentShapeType = Shape.Bitmap; SwitchDrawMode(sender); } }
void RenderD2DContentIntoSurface() { SizeF rtSize = renderTarget.Size; renderTarget.BeginDraw(); if (!isOpacityRTPopulated) { opacityRenderTarget.BeginDraw(); opacityRenderTarget.Transform = Matrix3x2F.Identity; opacityRenderTarget.Clear(new ColorF(GetColorValues(System.Windows.Media.Colors.Black), 0)); opacityRenderTarget.DrawText( text, textFormat, new RectF( 0, 0, rtSize.Width, rtSize.Height ), textBrush ); opacityRenderTarget.EndDraw(); isOpacityRTPopulated = true; } renderTarget.Clear(new ColorF(GetColorValues(System.Windows.Media.Colors.Black))); renderTarget.AntiAliasMode = AntiAliasMode.Aliased; D2DBitmap spBitmap = opacityRenderTarget.Bitmap; renderTarget.FillOpacityMask( spBitmap, textBrush, OpacityMaskContent.TextNatural, new RectF(0, 0, rtSize.Width, rtSize.Height), new RectF(0, 0, rtSize.Width, rtSize.Height) ); renderTarget.EndDraw(); }
private void addBitmapBrushBotton_Click(object sender, EventArgs e) { ExtendMode ex = extendedModeXComboBox.SelectedIndex > 0 ? (ExtendMode)extendedModeXComboBox.SelectedIndex : ExtendMode.Wrap; ExtendMode ey = extendedModeYComboBox.SelectedIndex > 0 ? (ExtendMode)extendedModeYComboBox.SelectedIndex : ExtendMode.Wrap; D2DBitmap brushBitmap = BitmapUtilities.LoadBitmapFromFile(renderTarget, parent.wicFactory, imageFilename); BitmapBrush brush = renderTarget.CreateBitmapBrush( brushBitmap, new BitmapBrushProperties( ex, ey, BitmapInterpolationMode.NearestNeighbor), new BrushProperties( opacity, Matrix3x2F.Identity)); parent.brushes.Add(brush); parent.currentBrushIndex = parent.brushes.Count - 1; FillBrushesListBox(); }
protected override void OnResize(EventArgs e) { lock (renderSyncObject) { if (RenderTarget != null) { // Resize the render targrt to the actual host size var size = new SizeU((uint)ClientSize.Width, (uint)ClientSize.Height); if (hwndRenderTarget != null) { hwndRenderTarget.Resize(size); //need to resize hwndRenderTarget to make its size same as the window's size } if (renderMode == RenderModes.BitmapRenderTargetOnPaint) { bitmapRenderTarget.Dispose(); bitmapRenderTarget = dcRenderTarget.CreateCompatibleRenderTarget(CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); bitmap = null; //the bitmap created in dc render target can't be used in hwnd render target foreach (var shape in drawingShapes) { shape.Bitmap = Bitmap; shape.RenderTarget = RenderTarget; } RefreshAll(); } else if (renderMode == RenderModes.BitmapRenderTargetRealTime) { Debug.Assert(hwndRenderTarget != null);//this should never be null considering the above bitmapRenderTarget.Dispose(); bitmapRenderTarget = hwndRenderTarget.CreateCompatibleRenderTarget(CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); bitmap = null; //the bitmap created in dc render target can't be used in hwnd render target foreach (var shape in drawingShapes) { shape.Bitmap = Bitmap; shape.RenderTarget = RenderTarget; } RefreshAll(); } } } base.OnResize(e); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); fullImage = Device.LoadBitmap(Properties.Resources.IMGP6873); gridImageWidth = fullImage.Width / gridCountX; gridImageHeight = fullImage.Height / gridCountY; screenImageWidth = (float)ClientRectangle.Width / gridCountX; screenImageHeight = (float)ClientRectangle.Height / gridCountY; sprites = new Sprite[gridCountX, gridCountY]; for (int y = 0; y < gridCountY; y++) { for (int x = 0; x < gridCountX; x++) { var bmp = Device.CreateBitmapGraphics(screenImageWidth, screenImageHeight); // copy a part of original image bmp.BeginRender(); bmp.DrawBitmap(fullImage, new D2DRect(0, 0, screenImageWidth, screenImageHeight), new D2DRect(x * gridImageWidth, y * gridImageHeight, gridImageWidth, gridImageHeight)); bmp.EndRender(); var s = new Sprite { bmp = bmp, originX = x * screenImageWidth, originY = y * screenImageHeight, width = screenImageWidth, height = screenImageHeight, }; sprites[x, y] = s; } } BeginClip(); }
public LayerShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap, int count) : base(initialRenderTarget, random, d2DFactory, bitmap) { Parameters = new LayerParameters(); parameters.ContentBounds = CoinFlip ? RandomRect(CanvasWidth, CanvasHeight) : new RectF(0, 0, CanvasWidth, CanvasHeight); if (CoinFlip) { GeometricMaskShape = new GeometryShape(initialRenderTarget, random, d2DFactory, Bitmap); parameters.GeometricMask = GeometricMaskShape.Geometry; } parameters.MaskAntialiasMode = CoinFlip ? AntialiasMode.Aliased : AntialiasMode.PerPrimitive; parameters.MaskTransform = RandomMatrix3x2(); parameters.Opacity = RandomOpacity(); if (CoinFlip) parameters.OpacityBrush = RandomOpacityBrush(); parameters.Options = CoinFlip ? LayerOptions.InitializeForClearType : LayerOptions.None; for(int i = 0; i < count; i++) { shapes.Add(RandomShape()); } }
public GeometryShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap) : base(initialRenderTarget, random, d2DFactory, bitmap) { coolStrokes = CoinFlip; double which = Random.NextDouble(); if (which < 0.67 || coolStrokes) PenBrush = RandomBrush(); if (!coolStrokes && which > 0.33) FillBrush = RandomBrush(); if (coolStrokes || CoinFlip) StrokeStyle = RandomStrokeStyle(); if (CoinFlip) worldTransform = RandomMatrix3x2(); StrokeWidth = RandomStrokeWidth(); geometry = RandomGeometry(); if (coolStrokes || Random.NextDouble() < 0.3) ModifyGeometry(); if (coolStrokes && CoinFlip) { ModifyGeometry(); } }
private static D2DBitmap CreateBitmapFromDecoder(RenderTarget renderTarget, ImagingFactory wicFactory, BitmapDecoder decoder) { // Create the initial frame. BitmapFrameDecode source = decoder.GetFrame(0); // Convert the image format to 32bppPBGRA -- which Direct2D expects. FormatConverter converter = wicFactory.CreateFormatConverter(); converter.Initialize( source.ToBitmapSource(), PixelFormats.Pbgra32Bpp, BitmapDitherType.None, BitmapPaletteType.MedianCut ); // Create a Direct2D bitmap from the WIC bitmap. D2DBitmap ret = renderTarget.CreateBitmapFromWicBitmap(converter.ToBitmapSource()); converter.Dispose(); source.Dispose(); return(ret); }
public abstract void Draw(D2DGraphics g, DateTime now, TimeSpan elapsed, Rectangle bounds, D2DBitmap image);
public static void DrawBitmap(this RenderContext context, D2DBitmap bitmap, float destX, float destY, float destWidth, float destHeight, float srcX, float srcY, float srcWidth, float srcHeight, float opacity) { context.RenderTarget.DeviceContext2D.DrawBitmap(bitmap, destX, destY, destWidth, destHeight, srcX, srcY, srcWidth, srcHeight, opacity); }
public IconInfo(D2DBitmap bitmap, Rectangle screenRect) { this.Bitmap = bitmap; this.Reset(screenRect); }
public static void DrawBitmap(this RenderContext context, D2DBitmap bitmap, float destX, float destY) { context.RenderTarget.DeviceContext2D.DrawBitmap(bitmap, destX, destY, bitmap.Width, bitmap.Height); }
public static void DrawBitmap(this RenderContext context, D2DBitmap bitmap, float destX, float destY, float destWidth, float destHeight, BitmapInterpolationMode interpolationMode) { context.RenderTarget.DeviceContext2D.DrawBitmap(bitmap, destX, destY, destWidth, destHeight, interpolationMode); }
public TextShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap, DWriteFactory dwriteFactory) : base(initialRenderTarget, random, d2DFactory, bitmap) { this.dwriteFactory = dwriteFactory; layoutRect = RandomRect(CanvasWidth, CanvasHeight); NiceGabriola = Random.NextDouble() < 0.25 && dwriteFactory.SystemFontFamilyCollection.Contains("Gabriola"); TextFormat = dwriteFactory.CreateTextFormat( RandomFontFamily(), RandomFontSize(), RandomFontWeight(), RandomFontStyle(), RandomFontStretch(), System.Globalization.CultureInfo.CurrentUICulture); if (CoinFlip) { TextFormat.LineSpacing = RandomLineSpacing(TextFormat.FontSize); } Text = RandomString(Random.Next(1000, 1000)); FillBrush = RandomBrush(); RenderingParams = RandomRenderingParams(); if (CoinFlip) { Options = DrawTextOptions.None; if (CoinFlip) { Options |= DrawTextOptions.Clip; } if (CoinFlip) { Options |= DrawTextOptions.NoSnap; } } }
/// <summary> /// Dispose of resources (IDisposable implementation) /// </summary> /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param> protected override void Dispose(bool disposing) { if (disposing && !disposed) { lock (renderSyncObject) { render = null; lock (sharedSyncObject) { if (sharedD2DFactory != null && d2DFactory == sharedD2DFactory) sharedRefCount--; if (d2DFactory != null && d2DFactory != sharedD2DFactory) d2DFactory.Dispose(); d2DFactory = null; if (dwriteFactory != null && dwriteFactory != sharedDwriteFactory) dwriteFactory.Dispose(); dwriteFactory = null; if (wicFactory != null && wicFactory != sharedWicFactory) wicFactory.Dispose(); wicFactory = null; if (sharedRefCount == 0) { if (sharedD2DFactory != null) sharedD2DFactory.Dispose(); sharedD2DFactory = null; if (sharedDwriteFactory != null) sharedDwriteFactory.Dispose(); sharedDwriteFactory = null; if (sharedWicFactory != null) sharedWicFactory.Dispose(); sharedWicFactory = null; } } foreach (DrawingShape shape in drawingShapes) { shape.Dispose(); } if (bitmap != null) bitmap.Dispose(); bitmap = null; if (dcRenderTarget != null) dcRenderTarget.Dispose(); dcRenderTarget = null; if (bitmapRenderTarget != null) bitmapRenderTarget.Dispose(); bitmapRenderTarget = null; if (hwndRenderTarget != null) hwndRenderTarget.Dispose(); hwndRenderTarget = null; disposed = true; } } base.Dispose(disposing); }
private void CreateD2DRenderTargets() { // Create a D2D render target which can draw into our offscreen D3D surface textureRenderTarget = d2DFactory.CreateGraphicsSurfaceRenderTarget( textureSurface, renderTargetProperties ); // Create a linear gradient brush for the 2D geometry GradientStopCollection gradientStops = textureRenderTarget.CreateGradientStopCollection(stopsGeometry, Gamma.StandardRgb, ExtendMode.Mirror); linearGradientBrush = textureRenderTarget.CreateLinearGradientBrush( new LinearGradientBrushProperties( new Point2F(100, 0), new Point2F(100, 200)), gradientStops ); // create a black brush blackBrush = textureRenderTarget.CreateSolidColorBrush(new ColorF(GetColorValues(System.Windows.Media.Colors.Black))); using (Stream stream = Application.ResourceAssembly.GetManifestResourceStream("Microsoft.WindowsAPICodePack.DirectX.Samples.tulip.jpg")) { d2dBitmap = BitmapUtilities.LoadBitmapFromStream( textureRenderTarget, imagingFactory, stream); } gridPatternBitmapBrush = CreateGridPatternBrush(textureRenderTarget); gridPatternBitmapBrush.Opacity = 0.5f; CreateBackBufferD2DRenderTarget(); }
public override void Draw(D2DGraphics g, DateTime now, TimeSpan elapsed, Rectangle bounds, D2DBitmap image) { Debug.Assert(Decorations != null); foreach (var d in Decorations) { d.UpdateAndDraw(g, now, elapsed, bounds, image, this); } }
public LineShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap) : base(initialRenderTarget, random, d2DFactory, bitmap) { point0 = RandomPoint(); point1 = RandomPoint(); PenBrush = RandomBrush(); StrokeWidth = RandomStrokeWidth(); if (CoinFlip) { StrokeStyle = RandomStrokeStyle(); } }
public void DrawBitmap(D2DBitmap pBitmap, Rectangle pRectangle) { _renderer.DrawBitmap(pBitmap, 1f, BitmapInterpolationMode.Linear, TransformRectangle(pRectangle)); }
private void SetRenderMode(RenderModes rm) { lock (renderSyncObject) { renderMode = rm; if (!IsInitialized && !isInitializing) return; //clean up objects that will be invalid after RenderTarget change if (dcRenderTarget != null) { dcRenderTarget.Dispose(); dcRenderTarget = null; } if (hwndRenderTarget != null) { hwndRenderTarget.Dispose(); hwndRenderTarget = null; } if (bitmapRenderTarget != null) { bitmapRenderTarget.Dispose(); bitmapRenderTarget = null; } peelings.Clear(); bitmap = null; //the bitmap created in dc render target can't be used in hwnd render target // Create the screen render target var size = new SizeU((uint)ClientSize.Width, (uint)ClientSize.Height); var props = new RenderTargetProperties { PixelFormat = new PixelFormat( Format.B8G8R8A8UNorm, AlphaMode.Ignore), Usage = RenderTargetUsages.GdiCompatible }; if (renderMode == RenderModes.DCRenderTarget || renderMode == RenderModes.BitmapRenderTargetOnPaint) { dcRenderTarget = d2DFactory.CreateDCRenderTarget(props); if (renderMode == RenderModes.BitmapRenderTargetOnPaint) { bitmapRenderTarget = dcRenderTarget.CreateCompatibleRenderTarget( CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); } render = null; } else { hwndRenderTarget = d2DFactory.CreateHwndRenderTarget( props, new HwndRenderTargetProperties(Handle, size, Microsoft.WindowsAPICodePack.DirectX.Direct2D1.PresentOptions.RetainContents)); if (renderMode == RenderModes.BitmapRenderTargetRealTime) { bitmapRenderTarget = hwndRenderTarget.CreateCompatibleRenderTarget( CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); } render = RenderSceneInBackground; } //move all shapes to new rendertarget and refresh foreach (var shape in drawingShapes) { shape.Bitmap = Bitmap; shape.RenderTarget = RenderTarget; } RefreshAll(); } }
public TextLayoutShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap, DWriteFactory dwriteFactory) : base(initialRenderTarget, random, d2DFactory, bitmap, dwriteFactory) { RandomizeTextLayout(); Point0 = RandomPoint(); }
protected override void OnResize(EventArgs e) { lock (renderSyncObject) { if (RenderTarget != null) { // Resize the render targrt to the actual host size var size = new SizeU((uint)ClientSize.Width, (uint)ClientSize.Height); if (hwndRenderTarget != null) hwndRenderTarget.Resize(size); //need to resize hwndRenderTarget to make its size same as the window's size if (renderMode == RenderModes.BitmapRenderTargetOnPaint) { bitmapRenderTarget.Dispose(); bitmapRenderTarget = dcRenderTarget.CreateCompatibleRenderTarget(CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); bitmap = null; //the bitmap created in dc render target can't be used in hwnd render target foreach (var shape in drawingShapes) { shape.Bitmap = Bitmap; shape.RenderTarget = RenderTarget; } RefreshAll(); } else if (renderMode == RenderModes.BitmapRenderTargetRealTime) { Debug.Assert(hwndRenderTarget != null);//this should never be null considering the above bitmapRenderTarget.Dispose(); bitmapRenderTarget = hwndRenderTarget.CreateCompatibleRenderTarget(CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); bitmap = null; //the bitmap created in dc render target can't be used in hwnd render target foreach (var shape in drawingShapes) { shape.Bitmap = Bitmap; shape.RenderTarget = RenderTarget; } RefreshAll(); } } } base.OnResize(e); }
public GDIEllipsesShape(RenderTarget initialRenderTarget, Random random, D2DFactory d2DFactory, D2DBitmap bitmap, int count) : base(initialRenderTarget, random, d2DFactory, bitmap) { for (int i = 0; i < count; i++) { ellipses.Add(RandomGdiEllipse()); } if (RenderTargetSupportsGDI(RenderTarget)) { gdiRenderTarget = RenderTarget.GdiInteropRenderTarget; } }
private void bitmapButton_Click(object sender, EventArgs e) { if (bitmapDialog == null) { bitmapDialog = new OpenFileDialog(); bitmapDialog.DefaultExt = "*.jpg;*.png"; } if (bitmapDialog.ShowDialog() == DialogResult.OK) { string filename = bitmapDialog.FileName; currentBitmap = BitmapUtilities.LoadBitmapFromFile(renderTarget, wicFactory, filename); currentShapeType = Shape.Bitmap; SwitchDrawMode(sender); } }