using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using System.Drawing.Imaging;
using Device = SharpDX.Direct3D11.Device;

namespace WindowsFormsApp4
{
public static class TextureLoader
{
public static Texture2D CreateTex2DFromFile(Device device, System.Drawing.Bitmap bitmap)
{
Texture2DDescription desc;
desc.Width = bitmap.Size.Width;
desc.Height = bitmap.Size.Height;
desc.ArraySize = 1;
desc.BindFlags = BindFlags.ShaderResource;
desc.Usage = ResourceUsage.Default;
desc.CpuAccessFlags = CpuAccessFlags.None;
desc.Format = Format.B8G8R8A8_UNorm;
desc.MipLevels = 1;
desc.OptionFlags = ResourceOptionFlags.None;
desc.SampleDescription.Count = 1;
desc.SampleDescription.Quality = 0;

BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);

var rect = new DataRectangle(data.Scan0, bitmap.Size.Width * 4);

Texture2D tex = new Texture2D(device, desc, rect);

// unlock the bitmap data
bitmap.UnlockBits(data);

return tex;
}
}
}