I'm trying to use SharpDX to create a simple maze-like 2D program using DirectX.
To that end I want to create bitmaps that I can render on-screen for the walls, hallways, outside of the maze, etc.
However, I can't seem to figure out how to either load an existing image file into the Bitmap class in the SharpDX library, or how to create a new such Bitmap class from scratch.
Since all the classes are said to be mapped directly to DirectX types, I guess this just means I need to learn more DirectX, but I was hoping there was a simple example somewhere that could show me what I need to do.
If I have to construct a new Bitmap from scratch and draw to it, I can do that, it's not difficult getting the pixels I need right, however I can't even seem to figure out that part.
Does anyone have any experience with the SharpDX library and can give me some pointers?
Here is Alexandre's approach in VB:
And this is Tom's example in VB, with a slight modification.
By providing the rendertarget and bitmap in the function call, I can use the function in a Lambda statement to place the bitmap in the resource cache that is provided in the D2dControl by dalance (available in NuGet). Also, since I have my bitmaps loaded as resources in my project, I can simply provide them in the function call:
Back when I was using SlimDX, I ran across the same issue (loading a bitmap using Direct2D) and found a similar solution that does away with the embedded loop and requires a bit less code; converting it to SharpDX was a simple matter. (I wish I could tell you where I found the original, but it's been years and apparently I didn't document the source. It could be straight out of the SlimDX samples for all I know.)
I left the namespaces intact so you know exactly where each type is defined. Also, some of the parameters (particularly those for PixelFormat) are flexible; play around with them and use whatever works for you.
As you can see, it locks the bitmap stream much as Alexandre's approach (which is used in the associated SharpDX sample project), but instead of manually copying each pixel, the constructor itself copies the stream behind the scenes. I haven't compared performance with the method Alexandre has proposed, so I can't tell you which method is faster, but this one is plenty fast enough for my purposes, and the code is clean.
(Sorry for the lack of syntax highlighting; the <code> tag for some reason breaks my snippet into sections.)
You should ask directly this question to the "Issue" tab on Sharpdx project, as I would be able to give you a quick response from there (you are lucky that I'm sometimes checking its usage from the net ;) ). If you were asking this officially, I could make an improvement to the library and keep a record of your request in the issue database.
Concerning your particular issue, it's not clear what kind of DirectX API you are using. I assume that you are using Direct2D?
If yes, there is no API to load directly a SharpDX.Direct2D1.Bitmap from a file (I will add this method to the API). I have just uploaded a bitmap sample that is performing this.
As you said, SharpDX is giving access to the raw-low-level DirectX API (unlike for example XNA that is giving a higher level API), so you definitely need to understand how program raw DirectX in order to use SharpDX.