Sorry, the Forum is closed :(

Unfortunately, the forum is now closed.

Please read the following post for further details.

We apologize for the inconvenience.
The SharpDX team.
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Generate Image as byte[] in WinRT

Generate Image as byte[] in WinRT 1 year 11 months ago #1210


  • Posts:8
  • Mostley's Avatar
  • Mostley
  • Fresh Boarder
  • OFFLINE
I'm trying to generate (draw/render) a simple image and display it in WinRT/XAML. For that I want the byte[] of an image drawn with SharpDX. My Approach so far seems fine but the resulting buffer is empty.

I tried CopyPixels as well but it also only produced zeros.

Can somebody point me in the right direction?

- - -

My current Approach:
        private static byte[] Approach7(TexRenderer trnder, int width, int height)
        {
            var wicFactory = new ImagingFactory();
            var dddFactory = new SharpDX.Direct2D1.Factory();
            var dwFactory = new SharpDX.DirectWrite.Factory();

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


            var renderTargetProperties = new RenderTargetProperties(
                RenderTargetType.Default,
                new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Unknown),
                dddFactory.DesktopDpi.Width,
                dddFactory.DesktopDpi.Height,
                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 = TextAlignment.Center,
                    ParagraphAlignment = ParagraphAlignment.Center
                };
            var textBrush = new SolidColorBrush(
                renderTarget,
                Color.Blue);

            renderTarget.Clear(Color.White);
            renderTarget.DrawText(
                "Hello World!",
                textFormat,
                new RectangleF(0, 0, width, height),
                textBrush);
            var bitmapRenderTarget = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.None);
            trnder.Render(bitmapRenderTarget, 0, 0);

            renderTarget.EndDraw();

            var bitmaplock = wicBitmap.Lock(null, BitmapLockFlags.Read);
            var dStream = new DataStream(bitmaplock.Data.DataPointer, bitmaplock.Stride * bitmaplock.Size.Height, true, true);
            var buffer = new byte[bitmaplock.Stride * bitmaplock.Size.Height];
            dStream.Write(buffer, 0, buffer.Length);

            return buffer;
        }
Last Edit: 1 year 11 months ago by Mostley. Reason: added code block
The administrator has disabled public write access.

Re: Generate Image as byte[] in WinRT 1 year 9 months ago #1536


  • Posts:8
  • Mostley's Avatar
  • Mostley
  • Fresh Boarder
  • OFFLINE
bump
The administrator has disabled public write access.

Re: Generate Image as byte[] in WinRT 1 year 9 months ago #1541


  • Posts:4
  • class101's Avatar
  • class101
  • Fresh Boarder
  • OFFLINE
Bitmap to byte array ?
var img = new Bitmap(5, 5);
byte[] byteArray;
using (var stream = new MemoryStream())
{
    img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
    byteArray = stream.ToArray();
}
return byteArray;

In you code looks like your are writing nothing to nothing, usually your Read() from a stream to store in a buffer, or you Write() to a stream a buffer already filled
Last Edit: 1 year 9 months ago by class101.
The administrator has disabled public write access.

Re: Generate Image as byte[] in WinRT 1 year 9 months ago #1546


  • Posts:8
  • Mostley's Avatar
  • Mostley
  • Fresh Boarder
  • OFFLINE
well sure but how can I get a sharddx RenderTarget in WinRT for that bitmap?

I want to create a new empty buffer draw into it via sharpdx and then get a byte[] back.
The administrator has disabled public write access.

Re: Generate Image as byte[] in WinRT 1 year 9 months ago #1552


  • Posts:84
  • stivo's Avatar
  • stivo
  • Expert Boarder
  • OFFLINE
I think it goes like this
  1.  
  2. int stride = width * 4;
  3. using (var buffer = new SharpDX.DataStream(height * stride, true, true))
  4. {
  5. // Copy the content of the WIC to the buffer
  6. buffer.WriteRange<byte>(data);
  7. return new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
  8. {
  9. Width = width,
  10. Height = height,
  11. ArraySize = 1,
  12. BindFlags = SharpDX.Direct3D11.BindFlags.RenderTarget,
  13. Usage = SharpDX.Direct3D11.ResourceUsage.Immutable,
  14. CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
  15. Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
  16. MipLevels = 1,
  17. OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
  18. SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
  19. }, new SharpDX.DataRectangle(buffer.DataPointer, stride));
  20. }

data has to be abyte array that contains your texture
Last Edit: 1 year 9 months ago by stivo.
The administrator has disabled public write access.

Re: Generate Image as byte[] in WinRT 1 year 9 months ago #1553


  • Posts:843 Thank you received: 1
  • xoofx's Avatar
  • xoofx
  • Administrator
  • OFFLINE
You are wrongly using the DataStream. This should work:
    var bitmaplock = wicBitmap.Lock(null, BitmapLockFlags.Read);
    var dStream = new DataStream(bitmaplock.Data.DataPointer, bitmaplock.Stride * bitmaplock.Size.Height, true, true);
    var buffer = dStream.ReadRange<byte>(bitmaplock.Stride * bitmaplock.Size.Height);

Or without using DataStream but with Utilities.Read
    var bitmaplock = wicBitmap.Lock(null, BitmapLockFlags.Read);
    var buffer = new byte[bitmaplock.Stride * bitmaplock.Size.Height];
    Utilities.Read(bitmaplock.Data.DataPointer, buffer, 0, buffer.Length);
The administrator has disabled public write access.

Re: Generate Image as byte[] in WinRT 1 year 9 months ago #1568


  • Posts:84
  • stivo's Avatar
  • stivo
  • Expert Boarder
  • OFFLINE
thanks for the clarification, it did work for me though :)
The administrator has disabled public write access.

Re: Generate Image as byte[] in WinRT 1 year 9 months ago #1581


  • Posts:8
  • Mostley's Avatar
  • Mostley
  • Fresh Boarder
  • OFFLINE
thanks that helped a lot actually
The administrator has disabled public write access.
  • Page:
  • 1
Time to create page: 0.204 seconds