在C#中使用SharpDX,如何将SharpDx的Bitmap与GDI的Bitmap互相转换?
时间: 2024-04-30 10:19:50 浏览: 235
C# JPG、PNG、Bmp(Bitmap)格式互转Webp
在C#中使用SharpDX和GDI进行Bitmap互相转换的方法如下:
1. 将SharpDX的Bitmap转换为GDI的Bitmap:
```csharp
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
System.Drawing.Bitmap gdiBitmap = new System.Drawing.Bitmap(stream);
}
```
2. 将GDI的Bitmap转换为SharpDX的Bitmap:
```csharp
using (var stream = new MemoryStream())
{
gdiBitmap.Save(stream, ImageFormat.Bmp);
stream.Seek(0, SeekOrigin.Begin);
SharpDX.Direct3D11.Texture2D sharpDxBitmap = SharpDX.Direct3D11.Texture2D.FromStream(device, stream, (int)stream.Length);
}
```
其中,`bitmap`为SharpDX的Bitmap对象,`gdiBitmap`为GDI的Bitmap对象,`device`为SharpDX的Device对象。
阅读全文