示例#1
0
        private static void Main()
        {
            var wicFactory = new ImagingFactory();
            var d2dFactory = new SharpDX.Direct2D1.Factory();

            string    filename = "output.jpg";
            const int width    = 512;
            const int height   = 512;

            var rectangleGeometry = new RoundedRectangleGeometry(d2dFactory, new RoundedRectangle()
            {
                RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, width - 128 * 2, height - 128 * 2)
            });

            var wicBitmap = new Bitmap(wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad);

            var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);

            var d2dRenderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties);

            var solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White);

            d2dRenderTarget.BeginDraw();
            d2dRenderTarget.Clear(Color.Black);
            d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null);
            d2dRenderTarget.EndDraw();

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            var stream = new WICStream(wicFactory, filename, NativeFileAccess.Write);
            // Initialize a Jpeg encoder with this stream
            var encoder = new JpegBitmapEncoder(wicFactory);

            encoder.Initialize(stream);

            // Create a Frame encoder
            var bitmapFrameEncode = new BitmapFrameEncode(encoder);

            bitmapFrameEncode.Initialize();
            bitmapFrameEncode.SetSize(width, height);
            var pixelFormatGuid = SharpDX.WIC.PixelFormat.FormatDontCare;

            bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid);
            bitmapFrameEncode.WriteSource(wicBitmap);

            bitmapFrameEncode.Commit();
            encoder.Commit();

            bitmapFrameEncode.Dispose();
            encoder.Dispose();
            stream.Dispose();

            System.Diagnostics.Process.Start(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, filename)));
        }
示例#2
0
        public static SharpDX.Direct2D1.Bitmap RedrawRestNoteBitmapCache(RenderTarget renderTargetSource, Color color, DurationTypes durationType)
        {
            ImagingFactory factory = new ImagingFactory();

            SharpDX.Direct2D1.Factory   factory2  = new SharpDX.Direct2D1.Factory();
            SharpDX.DirectWrite.Factory factory3  = new SharpDX.DirectWrite.Factory();
            SharpDX.WIC.Bitmap          wicBitmap = new SharpDX.WIC.Bitmap(factory, 0x20, 0x30, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnDemand);
            RenderTargetProperties      renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Unknown), 0f, 0f, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);
            WicRenderTarget             renderTarget           = new WicRenderTarget(factory2, wicBitmap, renderTargetProperties);

            try
            {
                PointF[] pathPoints;
                renderTarget.BeginDraw();
                renderTarget.Clear(new RawColor4?(Color.Transparent.ToRawColor4(1f)));
                RawVector2 pos = new RawVector2(18f, 44f);
                using (GraphicsPath path = new GraphicsPath())
                {
                    DurationTypes types = durationType;
                    if (types <= DurationTypes.Eighth)
                    {
                        switch (types)
                        {
                        case DurationTypes.The32nd:
                            pos = new RawVector2(pos.X, (pos.Y - (8f * (((float)McMeasure.StaveSpacing) / 2f))) + 3f);
                            path.AddLine(pos.X, pos.Y, pos.X + 1.6f, pos.Y);
                            path.AddLine(pos.X + 1.6f, pos.Y, (pos.X - 8f) + 2.4f, pos.Y + 41f);
                            path.AddLine((float)((pos.X - 8f) + 2.4f), (float)(pos.Y + 41f), (float)((pos.X - 8f) - 0.8f), (float)(pos.Y + 41f));
                            path.AddLine((pos.X - 8f) - 0.8f, pos.Y + 41f, pos.X, pos.Y);
                            DrawRestDotPart(renderTarget, pos, color);
                            DrawRestDotPart(renderTarget, new RawVector2(pos.X - 2.2f, pos.Y + 11f), color);
                            DrawRestDotPart(renderTarget, new RawVector2(pos.X - 4.4f, pos.Y + 22f), color);
                            break;

                        case DurationTypes.The16th:
                            pos = new RawVector2(pos.X, (pos.Y - (6f * (((float)McMeasure.StaveSpacing) / 2f))) + 3f);
                            path.AddLine(pos.X, pos.Y, pos.X + 1.6f, pos.Y);
                            path.AddLine(pos.X + 1.6f, pos.Y, (pos.X - 8f) + 2.4f, pos.Y + 30f);
                            path.AddLine((float)((pos.X - 8f) + 2.4f), (float)(pos.Y + 30f), (float)((pos.X - 8f) - 0.8f), (float)(pos.Y + 30f));
                            path.AddLine((pos.X - 8f) - 0.8f, pos.Y + 30f, pos.X, pos.Y);
                            DrawRestDotPart(renderTarget, pos, color);
                            DrawRestDotPart(renderTarget, new RawVector2(pos.X - 2.4f, pos.Y + 11f), color);
                            break;

                        case DurationTypes.Eighth:
                            goto Label_06F6;
                        }
                    }
                    else
                    {
                        switch (types)
                        {
                        case DurationTypes.Quarter:
                            pos = new RawVector2(pos.X - 11f, pos.Y - (7f * (((float)McMeasure.StaveSpacing) / 2f)));
                            path.AddLine(pos.X, pos.Y, pos.X + 7f, pos.Y + 9.5f);
                            path.AddBezier((float)(pos.X + 7f), (float)(pos.Y + 9.5f), (float)(pos.X + 3f), (float)(pos.Y + 16f), (float)(pos.X + 3f), (float)(pos.Y + 16f), (float)(pos.X + 8f), (float)(pos.Y + 24f));
                            path.AddBezier((float)(pos.X + 8f), (float)(pos.Y + 24f), (float)(pos.X - 0f), (float)(pos.Y + 21f), (float)(pos.X - 1f), (float)(pos.Y + 26f), (float)(pos.X + 4f), (float)(pos.Y + 34f));
                            path.AddBezier((float)(pos.X + 4f), (float)(pos.Y + 34f), (float)(pos.X - 8f), (float)(pos.Y + 28f), (float)(pos.X - 4f), (float)(pos.Y + 17f), (float)(pos.X + 4.5f), (float)(pos.Y + 21f));
                            path.AddLine((float)(pos.X + 4.5f), (float)(pos.Y + 21f), (float)(pos.X - 1f), (float)(pos.Y + 14f));
                            path.AddBezier((float)(pos.X - 1f), (float)(pos.Y + 14f), (float)(pos.X + 3.5f), (float)(pos.Y + 11f), (float)(pos.X + 4f), (float)(pos.Y + 7f), (float)(pos.X - 1f), (float)(pos.Y + 2f));
                            goto Label_0AD7;

                        case DurationTypes.Half:
                            pos = new RawVector2(pos.X - 8f, pos.Y - (4f * (((float)McMeasure.StaveSpacing) / 2f)));
                            path.AddLine(pos.X - 7f, pos.Y, pos.X + 7f, pos.Y);
                            path.AddLine(pos.X + 7f, pos.Y, pos.X + 7f, pos.Y - 5f);
                            path.AddLine((float)(pos.X + 7f), (float)(pos.Y - 5f), (float)(pos.X - 7f), (float)(pos.Y - 5f));
                            path.AddLine(pos.X - 7f, pos.Y - 5f, pos.X - 7f, pos.Y);
                            path.AddLine(pos.X - 7f, pos.Y, pos.X - 10f, pos.Y);
                            path.AddLine(pos.X - 10f, pos.Y, pos.X - 10f, pos.Y + 1f);
                            path.AddLine((float)(pos.X - 10f), (float)(pos.Y + 1f), (float)(pos.X + 10f), (float)(pos.Y + 1f));
                            path.AddLine(pos.X + 10f, pos.Y + 1f, pos.X + 10f, pos.Y);
                            goto Label_0AD7;
                        }
                        if (types == DurationTypes.Whole)
                        {
                            pos = new RawVector2(pos.X - 8f, pos.Y - (6f * (((float)McMeasure.StaveSpacing) / 2f)));
                            path.AddLine(pos.X - 7f, pos.Y, pos.X + 7f, pos.Y);
                            path.AddLine(pos.X + 7f, pos.Y, pos.X + 7f, pos.Y + 5f);
                            path.AddLine((float)(pos.X + 7f), (float)(pos.Y + 5f), (float)(pos.X - 7f), (float)(pos.Y + 5f));
                            path.AddLine(pos.X - 7f, pos.Y + 5f, pos.X - 7f, pos.Y);
                            path.AddLine(pos.X - 7f, pos.Y, pos.X - 10f, pos.Y);
                            path.AddLine(pos.X - 10f, pos.Y, pos.X - 10f, pos.Y - 1f);
                            path.AddLine((float)(pos.X - 10f), (float)(pos.Y - 1f), (float)(pos.X + 10f), (float)(pos.Y - 1f));
                            path.AddLine(pos.X + 10f, pos.Y - 1f, pos.X + 10f, pos.Y);
                        }
                    }
                    goto Label_0AD7;
                    Label_06F6:
                    pos = new RawVector2(pos.X - 2f, (pos.Y - (6f * (((float)McMeasure.StaveSpacing) / 2f))) + 3f);
                    path.AddLine(pos.X, pos.Y, pos.X + 1.6f, pos.Y);
                    path.AddLine(pos.X + 1.6f, pos.Y, (pos.X - 8f) + 2.4f, pos.Y + 19f);
                    path.AddLine((float)((pos.X - 8f) + 2.4f), (float)(pos.Y + 19f), (float)((pos.X - 8f) - 0.8f), (float)(pos.Y + 19f));
                    path.AddLine((pos.X - 8f) - 0.8f, pos.Y + 19f, pos.X, pos.Y);
                    DrawRestDotPart(renderTarget, pos, color);
                    Label_0AD7:
                    path.Flatten();
                    pathPoints = path.PathPoints;
                    path.Dispose();
                }
                PathGeometry geometry = new PathGeometry(factory2);
                if (pathPoints.Length > 1)
                {
                    GeometrySink sink = geometry.Open();
                    sink.SetSegmentFlags(PathSegment.ForceRoundLineJoin);
                    sink.BeginFigure(new RawVector2(pathPoints[0].X, pathPoints[0].Y), FigureBegin.Filled);
                    for (int i = 1; i < pathPoints.Length; i++)
                    {
                        sink.AddLine(new RawVector2(pathPoints[i].X, pathPoints[i].Y));
                    }
                    sink.EndFigure(FigureEnd.Closed);
                    sink.Close();
                    sink.Dispose();
                }
                SolidColorBrush brush = new SolidColorBrush(renderTarget, color.ToRawColor4(1f));
                renderTarget.FillGeometry(geometry, brush);
                brush.Dispose();
                geometry.Dispose();
                renderTarget.EndDraw();
            }
            catch (Exception)
            {
                factory.Dispose();
                factory2.Dispose();
                factory3.Dispose();
                wicBitmap.Dispose();
                renderTarget.Dispose();
                return(null);
            }
            SharpDX.Direct2D1.Bitmap bitmap2 = SharpDX.Direct2D1.Bitmap.FromWicBitmap(renderTargetSource, wicBitmap);
            factory.Dispose();
            factory2.Dispose();
            factory3.Dispose();
            wicBitmap.Dispose();
            renderTarget.Dispose();
            return(bitmap2);
        }
示例#3
0
        public static SharpDX.Direct2D1.Bitmap RedrawMiscNoteBitmapCache(RenderTarget renderTargetSource, Color color)
        {
            ImagingFactory factory = new ImagingFactory();

            SharpDX.Direct2D1.Factory   factory2  = new SharpDX.Direct2D1.Factory();
            SharpDX.DirectWrite.Factory factory3  = new SharpDX.DirectWrite.Factory();
            SharpDX.WIC.Bitmap          wicBitmap = new SharpDX.WIC.Bitmap(factory, 0x10, 0x10, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnDemand);
            RenderTargetProperties      renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Unknown), 0f, 0f, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);
            WicRenderTarget             renderTarget           = new WicRenderTarget(factory2, wicBitmap, renderTargetProperties);

            try
            {
                PointF[] pathPoints;
                renderTarget.BeginDraw();
                renderTarget.Clear(new RawColor4?(Color.Transparent.ToRawColor4(1f)));
                RawVector2 vector = new RawVector2(8f, 8f);
                using (GraphicsPath path = new GraphicsPath())
                {
                    float num  = vector.X - 6f;
                    float num2 = vector.Y - 3f;
                    path.AddLine((float)(num + 0f), (float)(num2 + 0f), (float)(num + 12f), (float)(num2 + 0f));
                    path.AddLine((float)(num + 12f), (float)(num2 + 0f), (float)(num + 12f), (float)(num2 + 6f));
                    path.AddLine((float)(num + 12f), (float)(num2 + 6f), (float)(num + 0f), (float)(num2 + 6f));
                    path.AddLine((float)(num + 0f), (float)(num2 + 6f), (float)(num + 0f), (float)(num2 + 0f));
                    path.Flatten();
                    pathPoints = path.PathPoints;
                    path.Dispose();
                }
                PathGeometry geometry = new PathGeometry(factory2);
                if (pathPoints.Length > 1)
                {
                    GeometrySink sink = geometry.Open();
                    sink.SetSegmentFlags(PathSegment.ForceRoundLineJoin);
                    sink.BeginFigure(new RawVector2(pathPoints[0].X, pathPoints[0].Y), FigureBegin.Filled);
                    for (int i = 1; i < pathPoints.Length; i++)
                    {
                        sink.AddLine(new RawVector2(pathPoints[i].X, pathPoints[i].Y));
                    }
                    sink.EndFigure(FigureEnd.Closed);
                    sink.Close();
                    sink.Dispose();
                }
                SolidColorBrush brush = new SolidColorBrush(renderTarget, color.ToRawColor4(1f));
                renderTarget.FillGeometry(geometry, brush);
                brush.Dispose();
                geometry.Dispose();
                renderTarget.EndDraw();
            }
            catch (Exception)
            {
                factory.Dispose();
                factory2.Dispose();
                factory3.Dispose();
                wicBitmap.Dispose();
                renderTarget.Dispose();
                return(null);
            }
            SharpDX.Direct2D1.Bitmap bitmap2 = SharpDX.Direct2D1.Bitmap.FromWicBitmap(renderTargetSource, wicBitmap);
            factory.Dispose();
            factory2.Dispose();
            factory3.Dispose();
            wicBitmap.Dispose();
            renderTarget.Dispose();
            return(bitmap2);
        }