加载/创建映像到SharpDX在.NET程序 [英] Loading/Creating an image into SharpDX in a .NET program

查看:180
本文介绍了加载/创建映像到SharpDX在.NET程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用SharpDX创建使用DirectX的一个简单的迷宫式的2D节目。

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.

不过,我似乎无法弄清楚如何可以加载现有图像文件输出到在的 SharpDX 库,或如何从头开始创建一个新的等位图类。

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.

既然是说,所有的类都必须直接映射到DirectX类型,我想这只是意味着我需要学习更多的DirectX,但我希望有一个简单的例子的地方,能告诉我什么,我需要做的。

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.

没有任何人有与 SharpDX 库的经验,可以给我一些指点?

Does anyone have any experience with the SharpDX library and can give me some pointers?

推荐答案

您应该直接问这个问题上Sharpdx项目的问题选项卡,因为我就可以给你从那里快速响应(你是幸运的我正在有时从网检查它的使用;))。如果你被正式问这个,我可以做的改进,图书馆和保持问题数据库的请求的记录。

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.

关于你的具体问题,目前还不清楚什么样的DirectX API所使用。我假设你正在使用Direct2D的?

Concerning your particular issue, it's not clear what kind of DirectX API you are using. I assume that you are using Direct2D?

如果是的,有没有API来直接加载从文件中SharpDX.Direct2D1.Bitmap(我将这个方法添加到API)。我刚刚上传了一个<一个href="http://$c$c.google.com/p/sharpdx/source/browse/trunk/Samples/Direct2D1/BitmapApp/Program.cs">bitmap执行此示例。

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.

/// <summary>
/// Loads a Direct2D Bitmap from a file using System.Drawing.Image.FromFile(...)
/// </summary>
/// <param name="renderTarget">The render target.</param>
/// <param name="file">The file.</param>
/// <returns>A D2D1 Bitmap</returns>
public static Bitmap LoadFromFile(RenderTarget renderTarget, string file)
{
    // Loads from file using System.Drawing.Image
    using (var bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(file))
    {
        var sourceArea = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
        var bitmapProperties = new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));
        var size = new System.Drawing.Size(bitmap.Width, bitmap.Height);

        // Transform pixels from BGRA to RGBA
        int stride = bitmap.Width * sizeof(int);
        using (var tempStream = new DataStream(bitmap.Height * stride, true, true))
        {
            // Lock System.Drawing.Bitmap
            var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            // Convert all pixels 
            for (int y = 0; y < bitmap.Height; y++)
            {
                int offset = bitmapData.Stride*y;
                for (int x = 0; x < bitmap.Width; x++)
                {
                    // Not optimized 
                    byte B = Marshal.ReadByte(bitmapData.Scan0, offset++);
                    byte G = Marshal.ReadByte(bitmapData.Scan0, offset++);
                    byte R = Marshal.ReadByte(bitmapData.Scan0, offset++);
                    byte A = Marshal.ReadByte(bitmapData.Scan0, offset++);
                    int rgba = R | (G << 8) | (B << 16) | (A << 24);
                    tempStream.Write(rgba);
                }

            }
            bitmap.UnlockBits(bitmapData);
            tempStream.Position = 0;

            return new Bitmap(renderTarget, size, tempStream, stride, bitmapProperties);
        }
    }
}

正如你所说,SharpDX是给访问原始低级的DirectX API(不像例如XNA被赋予了更高级别的API),所以你一定要了解如何计划生的DirectX为了使用SharpDX。

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.

这篇关于加载/创建映像到SharpDX在.NET程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆