SharpDx Save Bitmap to File

*爱你&永不变心* 提交于 2019-12-24 19:31:44

问题


I have a Sharpdx Direct2D1 Bitmap from the render target. I want to save it to a file as an image though. This is for test purposes. I am not sure if i need a WIC bitmap for that or how to convert these bitmaps. Or how to receive the WIC bitmap in the first place.

Also i can't find an easy explanation how to save this bitmap to a file in general.

Any help appriciated.

Edit:

I'm using this approach now: http://www.rolandk.de/wp/2013/06/inhalt-der-rendertarget-textur-in-ein-bitmap-kopieren/

It is in German, but what he says is, that you have to copy the resource of the render target to a staging resource to have access via the cpu. Then copy the contents over to a bitmap.

I'm trying this approach then finally using bitmap.Save(filename). But somehow the bitmap stays empty. Maybe i miss to encode the file correctly. But seems more like I don't get the data from the back buffer target at all.


回答1:


If you have your image in a WicBitmap it is very easy to move it into a GDI+ bitmap and save it from there:

    var pixelData = new byte[width*height*4];
    wicBitmap.CopyPixels(pixelData, width*4);
    var bmp = new System.Drawing.Bitmap(width, height);
    var bd = bmp.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.WriteOnly,
        System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
    Marshal.Copy(pixelData, 0, bd.Scan0, pixelData.Length);
    bmp.UnlockBits(bd);

There may or may not be better ways to solve this. But this is the one I have used successfully.




回答2:


The question is answered by Dan Bysröm, if you have the wicBitmap and then simply copy the data to the gdi bitmap.

I solved my particular problem with the link I posted in the edit part though. I did not use wic or anything, because that maybe was a totally wrong approach for my problem in the first place.

The question that arised in the edit part was solved by not using CopyResource(...) but CopySubResource(...) instead with default parameter to copy all. I am not sure why CopyResource(...) did not work though.



来源:https://stackoverflow.com/questions/47411596/sharpdx-save-bitmap-to-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!