Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
AnswerRe: New to C# - struggling with links + functions + ... etc etc ! Pin
Luc Pattyn2-May-11 9:43
sitebuilderLuc Pattyn2-May-11 9:43 
GeneralRe: New to C# - struggling with links + functions + ... etc etc ! [modified] Pin
BobJanova3-May-11 1:24
BobJanova3-May-11 1:24 
AnswerRe: New to C# - struggling with links + functions + ... etc etc ! [modified] Pin
Steven.Pinto20003-May-11 0:11
Steven.Pinto20003-May-11 0:11 
RantRe: New to C# - struggling with links + functions + ... etc etc ! Pin
Peter_in_27803-May-11 0:42
professionalPeter_in_27803-May-11 0:42 
GeneralRe: New to C# - struggling with links + functions + ... etc etc ! [modified] Pin
Steven.Pinto20004-May-11 23:58
Steven.Pinto20004-May-11 23:58 
QuestionSending and Recieving multiple SMS Pin
Christian_V_V2-May-11 7:11
Christian_V_V2-May-11 7:11 
Question[SOLVED] How to save a D3DImage as a PNG file [modified] Pin
Super Lloyd2-May-11 5:25
Super Lloyd2-May-11 5:25 
Answer[a possible solution] Re: How to save a D3DImage as a PNG file Pin
Super Lloyd2-May-11 7:37
Super Lloyd2-May-11 7:37 
I'm using SharpDX[^] so I have a fully managed DirectX environment.

I figured it out!
Just for the info, here is how!
class Program
{
    unsafe static void Main(string[] args)
    {
        using (var d3d2d = new D3D11_2D1())
        {
            d3d2d.Reset(250, 250);

            var sc = new Scene_11() { Context = d3d2d.Drawing };
            var scw = new SceneDwrite() { Context = d3d2d.Text };

            var rargs = new RenderArgs();
            d3d2d.BeginRender(rargs);
            sc.Render(rargs);
            scw.Render(rargs);
            d3d2d.EndRender(rargs);

            Save1(d3d2d);
            //Save2(d3d2d);
        }
    }

    unsafe private static void Save1(D3D11_2D1 d3d2d)
    {
        var wb = GetBitmap(d3d2d.Drawing.BackBuffer);
        using (var stream = new FileStream("scene.png", FileMode.Create))
        {
            var encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(wb));
            encoder.Save(stream);
        }
    }

    unsafe private static void Save2(D3D11_2D1 d3d2d)
    {
        // not working! :(
        using (var teximg = GetCopy(d3d2d.Drawing.BackBuffer))
            Resource.ToFile(teximg.Device.ImmediateContext, teximg, ImageFileFormat.Png, "scene.png");
    }

    unsafe private static WriteableBitmap GetBitmap(Texture2D tex)
    {
        int w = tex.Description.Width;
        int h = tex.Description.Height;

        var wb = new WriteableBitmap(w, h, 96.0, 96.0, PixelFormats.Bgra32, null);
        wb.Lock();
        uint* wbb = (uint*)wb.BackBuffer;

        var db = GetDataBox(tex);
        db.Data.Position = 0;
        for (int y = 0; y < h; y++)
        {
            db.Data.Position = y * db.RowPitch;
            for (int x = 0; x < w; x++)
            {
                var c = db.Data.Read<uint>();
                wbb[y * w + x] = c;
            }
        }
        wb.AddDirtyRect(new Int32Rect(0, 0, w, h));
        wb.Unlock();
        return wb;
    }

    private static DataBox GetDataBox(Texture2D tex)
    {
        var db = GetDataBox(tex, 1);
        return GetDataBox(tex, tex.Description.Height * db.RowPitch);
    }

    private static DataBox GetDataBox(Texture2D tex, int size)
    {
        using (var teximg = GetCopy(tex))
            return tex.Device.ImmediateContext.MapSubresource(
                teximg
                , 0
                , size
                , MapMode.Read
                , MapFlags.None);
    }

    private static Texture2D GetCopy(Texture2D tex)
    {
        var textimgdesc = tex.Description;
        textimgdesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);
        textimgdesc.Usage = ResourceUsage.Staging;
        textimgdesc.BindFlags = BindFlags.None;
        textimgdesc.CpuAccessFlags = CpuAccessFlags.Read | CpuAccessFlags.Write;
        var teximg = new Texture2D(tex.Device, textimgdesc);
        tex.Device.ImmediateContext.CopyResource(tex, teximg);
        return teximg;
    }
}

A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

QuestionCA2006 , performance warnings Pin
Subin Mavunkal2-May-11 0:53
Subin Mavunkal2-May-11 0:53 
AnswerRe: CA2006 , performance warnings Pin
I Believe In GOD2-May-11 2:36
I Believe In GOD2-May-11 2:36 
Questionc urgent Pin
hajar802-May-11 0:28
hajar802-May-11 0:28 
AnswerMy vote of 1 PinPopular
Keith Barrow2-May-11 0:45
professionalKeith Barrow2-May-11 0:45 
GeneralRe: My vote of 1 Pin
I Believe In GOD2-May-11 1:24
I Believe In GOD2-May-11 1:24 
AnswerRe: c urgent Pin
PIEBALDconsult2-May-11 2:49
mvePIEBALDconsult2-May-11 2:49 
GeneralRe: c urgent Pin
Keith Barrow2-May-11 3:48
professionalKeith Barrow2-May-11 3:48 
JokeRe: c urgent Pin
Thomas Krojer2-May-11 4:40
Thomas Krojer2-May-11 4:40 
GeneralRe: c urgent Pin
walterhevedeich2-May-11 16:32
professionalwalterhevedeich2-May-11 16:32 
Questionvideo codes c# Pin
naalgo1-May-11 22:50
naalgo1-May-11 22:50 
AnswerRe: video codes c# Pin
Richard MacCutchan1-May-11 23:16
mveRichard MacCutchan1-May-11 23:16 
AnswerRe: video codes c# Pin
Eddy Vluggen1-May-11 23:24
professionalEddy Vluggen1-May-11 23:24 
GeneralRe: video codes c# Pin
Keith Barrow2-May-11 0:46
professionalKeith Barrow2-May-11 0:46 
GeneralRe: video codes c# Pin
Thomas Krojer2-May-11 4:42
Thomas Krojer2-May-11 4:42 
GeneralRe: video codes c# Pin
Eddy Vluggen2-May-11 5:02
professionalEddy Vluggen2-May-11 5:02 
AnswerGimme codez Pin
Luc Pattyn1-May-11 23:36
sitebuilderLuc Pattyn1-May-11 23:36 
AnswerRe: video codes c# Pin
I Believe In GOD1-May-11 23:40
I Believe In GOD1-May-11 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.