Esempio n. 1
0
        public void Render()
        {
            if (!_isDirty)
            {
                return;
            }
            if (!IsEnabled)
            {
                return;
            }

            _wicRenderTarget.BeginDraw();
            _wicRenderTarget.Clear(_clearColor);
            _wicRenderTarget.DrawText(_text, _textFormat, _rect, _sceneColorBrush);
            _wicRenderTarget.EndDraw();

            var        bitmapLock = _wicBitmap.Lock(SharpDX.WIC.BitmapLockFlags.Read);
            DataStream stream;

            _immediateContext.MapSubresource(_renderTexture, 0, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out stream);
            stream.WriteRange(bitmapLock.Data.DataPointer, _bitmapSize);
            _immediateContext.UnmapSubresource(_renderTexture, 0);
            stream.Dispose();
            bitmapLock.Dispose();

            _isDirty = false;
        }
Esempio n. 2
0
        public void OnRender(float framesPerSecond)
        {
            if (_isEnabled == false)
            {
                return;
            }

            if (fps != framesPerSecond)
            {
                fps        = framesPerSecond;
                textString = string.Format("FPS: {0}\n{1}", fps.ToString("0.00", culture), _text);

                _wicRenderTarget.BeginDraw();
                _wicRenderTarget.Clear(clearColor);
                _wicRenderTarget.DrawText(textString, _textFormat, rect, _sceneColorBrush);
                _wicRenderTarget.EndDraw();

                var        bitmapLock = _wicBitmap.Lock(SharpDX.WIC.BitmapLockFlags.Read);
                DataStream stream;
                _immediateContext.MapSubresource(_renderTexture, 0, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out stream);
                stream.WriteRange(bitmapLock.Data.DataPointer, _width * _height * 4);
                _immediateContext.UnmapSubresource(_renderTexture, 0);
                stream.Dispose();
                bitmapLock.Dispose();
            }
        }
Esempio n. 3
0
        private unsafe byte *GetBitmapData(string s, out int stride)
        {
            FreeBitmapData();

            var size = GetMeasureString(s);

            var w = (int)(Math.Ceiling(size.Width += 2));
            var h = (int)(Math.Ceiling(size.Height += 2));

            CreateBitmap(w, h);

            _renderTarget.BeginDraw();

            _renderTarget.Clear(TransparentColor);
            _renderTarget.AntialiasMode = AntialiasMode.Aliased;
            textFormat.TextAlignment    = TextAlignment.Center;
            _renderTarget.DrawText(s, textFormat, new RectangleF(0, 0, w, h), _brush);

            _renderTarget.EndDraw();

            //SaveToFile(@"C:\Xamarin\Cocos2D-XNAGraphics\" + s + ".png");

            // Calculate stride of source
            stride = _bitmap.Size.Width * 4;
            // Create data array to hold source pixel data
            //byte[] data = new byte[stride * _bitmap.Size.Height];
            byte[] data = new byte[stride * _bitmap.Size.Height];

            using (var datas = new DataStream(stride * _bitmap.Size.Height, true, true))
            {
                _bitmap.CopyPixels(stride, datas);

                data = datas.ReadRange <byte>(data.Length);
            }

            ReleaseResources();

            pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);
            IntPtr pointer = pinnedArray.AddrOfPinnedObject();

            return((byte *)pointer);;
        }
Esempio n. 4
0
        //
        // http://stackoverflow.com/questions/9151615/how-does-one-use-a-memory-stream-instead-of-files-when-rendering-direct2d-images
        //
        // Identical to above SO question, except that we are rendering to MemoryStream because it was added to the API
        //
        private MemoryStream RenderStaticTextToBitmap()
        {
            var width       = 400;
            var height      = 100;
            var pixelFormat = WicPixelFormat.Format32bppBGR;

            var wicFactory = new ImagingFactory();
            var dddFactory = new SharpDX.Direct2D1.Factory();
            var dwFactory  = new SharpDX.DirectWrite.Factory();

            var wicBitmap = new Bitmap(
                wicFactory,
                width,
                height,
                pixelFormat,
                BitmapCreateCacheOption.CacheOnLoad);

            var renderTargetProperties = new RenderTargetProperties(
                RenderTargetType.Default,
                new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown),
                0,
                0,
                RenderTargetUsage.None,
                FeatureLevel.Level_DEFAULT);
            var renderTarget = new WicRenderTarget(
                dddFactory,
                wicBitmap,
                renderTargetProperties)
            {
                TextAntialiasMode = TextAntialiasMode.Cleartype
            };

            renderTarget.BeginDraw();

            var textFormat = new TextFormat(dwFactory, "Consolas", 48)
            {
                TextAlignment      = SharpDX.DirectWrite.TextAlignment.Center,
                ParagraphAlignment = ParagraphAlignment.Center
            };
            var textBrush = new SharpDX.Direct2D1.SolidColorBrush(
                renderTarget,
                SharpDX.Colors.Blue);

            renderTarget.Clear(Colors.White);
            renderTarget.DrawText(
                "Hi, mom!",
                textFormat,
                new RectangleF(0, 0, width, height),
                textBrush);

            renderTarget.EndDraw();

            var ms = new MemoryStream();

            var stream = new WICStream(
                wicFactory,
                ms);

            var encoder = new PngBitmapEncoder(wicFactory);

            encoder.Initialize(stream);

            var frameEncoder = new BitmapFrameEncode(encoder);

            frameEncoder.Initialize();
            frameEncoder.SetSize(width, height);
            frameEncoder.PixelFormat = WicPixelFormat.FormatDontCare;
            frameEncoder.WriteSource(wicBitmap);
            frameEncoder.Commit();

            encoder.Commit();

            frameEncoder.Dispose();
            encoder.Dispose();
            stream.Dispose();

            ms.Position = 0;
            return(ms);
        }
        //  private static SharpDX.Direct2D1.DeviceContext d2dContext;
        public async static Task <MemoryStream> RenderStaticTextToBitmap(StorageFile imageFile)
        {
            var bitmap = new BitmapImage();

            using (var strm = await imageFile.OpenAsync(FileAccessMode.Read))
            {
                bitmap.SetSource(strm);
            }
            var width       = bitmap.PixelWidth;
            var height      = bitmap.PixelHeight;
            var pixelFormat = WicPixelFormat.Format32bppBGR;

            var             wicFactory = new ImagingFactory2();
            var             dddFactory = new SharpDX.Direct2D1.Factory();
            var             dwFactory  = new SharpDX.DirectWrite.Factory();
            WicRenderTarget renderTarget;
            Bitmap          wicBitmap;

            using (var bitmapSource = LoadBitmap(wicFactory, imageFile.Path))
            {
                wicBitmap = new Bitmap(wicFactory, bitmapSource, BitmapCreateCacheOption.CacheOnLoad);

                int pixelWidth  = (int)(wicBitmap.Size.Width * DisplayProperties.LogicalDpi / 96.0);
                int pixelHeight = (int)(wicBitmap.Size.Height * DisplayProperties.LogicalDpi / 96.0);

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

                renderTarget = new WicRenderTarget(
                    dddFactory,
                    wicBitmap,
                    renderTargetProperties)
                {
                    TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Cleartype
                };
            }

            renderTarget.BeginDraw();

            var textFormat = new TextFormat(dwFactory, "Segoe UI Light", 25)
            {
                TextAlignment      = SharpDX.DirectWrite.TextAlignment.Leading,
                ParagraphAlignment = ParagraphAlignment.Far
            };
            var textBrush = new SharpDX.Direct2D1.SolidColorBrush(
                renderTarget,
                SharpDX.Color.DarkBlue);

            StringBuilder sb     = new StringBuilder();
            var           dstamp = StampPersistData.Instance.DataStamp;

            sb.Append(dstamp.KMReading).Append("\n");
            sb.Append(dstamp.DateOfFirstReg).Append("\n");

            sb.Append(dstamp.Gps).Append("\n");
            sb.Append(dstamp.VehRegNo).Append("\n");
            sb.Append(dstamp.Make).Append("\n");

            sb.Append(dstamp.CusName).Append("\n");
            sb.Append(dstamp.InspectorName).Append("\n");
            sb.Append(dstamp.CaseNo);



            renderTarget.DrawText(
                sb.ToString(),
                textFormat,
                new SharpDX.RectangleF(1, 1, width + 50, height + 25),
                textBrush);

            //new RectangleF(width - 150, 0, width, height + 25),

            renderTarget.EndDraw();

            var ms = new MemoryStream();

            var stream = new WICStream(
                wicFactory,
                ms);

            BitmapEncoder encoder = null;

            if (imageFile.FileType == ".png")
            {
                encoder = new PngBitmapEncoder(wicFactory);
            }
            else if (imageFile.FileType == ".jpg")
            {
                encoder = new JpegBitmapEncoder(wicFactory);
            }

            encoder.Initialize(stream);

            var frameEncoder = new BitmapFrameEncode(encoder);

            frameEncoder.Initialize();
            frameEncoder.SetSize(width, height);
            // frameEncoder.PixelFormat = WicPixelFormat.FormatDontCare;
            frameEncoder.WriteSource(wicBitmap);
            frameEncoder.Commit();

            encoder.Commit();

            frameEncoder.Dispose();
            encoder.Dispose();
            stream.Dispose();

            ms.Position = 0;
            return(ms);
        }