从数组创建 8 位位图
Create 8 bit bitmap from array
我正在尝试从像素数组创建位图。
var height = 2;
var width = 2;
var output = new byte[4] { 0, 0, 0, 0 };
var gcHandle = GCHandle.Alloc(output, GCHandleType.Pinned);
var stride = width * sizeof(byte);
var pointer = gcHandle.AddrOfPinnedObject();
using (var bitmap = new Bitmap(width, height, stride, PixelFormat.Format8bppIndexed, pointer))
{
}
但是我得到 System.ArgumentException: 'Parameter is not valid.'
,没有内部异常或更多详细信息。
我不想用SetPixel
因为我的真实数组很大
这是为 .Net Standard 2.0
使用 System.Drawing.Common 4.5.0
库
你也许可以这样做:
unsafe public static void Main()
{
var height = 2;
var width = 2;
var output = new byte[4] { 1, 2, 3, 4};
using (var bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed))
{
var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
Marshal.Copy(data.Scan0, output, 0, 4);
bitmap.UnlockBits(data);
bitmap.Save(@"D:\blah.bmp");
}
}
Bitmap.LockBits Method
将位图锁定到系统内存中。
Copies data from a managed array to an unmanaged memory pointer, or
from an unmanaged memory pointer to a managed array.
如前所述,只需在 32bpp 内工作,让您的生活更轻松
public static void Main()
{
var height = 2;
var width = 2;
var c = Color.White.ToArgb();
var output = new int[4] { c, c, c, c };
using (var bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed))
{
var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
Marshal.Copy(output, 0, data.Scan0, 4);
bitmap.UnlockBits(data);
bitmap.Save(@"D:\trdy.bmp");
}
}
我正在尝试从像素数组创建位图。
var height = 2;
var width = 2;
var output = new byte[4] { 0, 0, 0, 0 };
var gcHandle = GCHandle.Alloc(output, GCHandleType.Pinned);
var stride = width * sizeof(byte);
var pointer = gcHandle.AddrOfPinnedObject();
using (var bitmap = new Bitmap(width, height, stride, PixelFormat.Format8bppIndexed, pointer))
{
}
但是我得到 System.ArgumentException: 'Parameter is not valid.'
,没有内部异常或更多详细信息。
我不想用SetPixel
因为我的真实数组很大
这是为 .Net Standard 2.0
System.Drawing.Common 4.5.0
库
你也许可以这样做:
unsafe public static void Main()
{
var height = 2;
var width = 2;
var output = new byte[4] { 1, 2, 3, 4};
using (var bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed))
{
var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
Marshal.Copy(data.Scan0, output, 0, 4);
bitmap.UnlockBits(data);
bitmap.Save(@"D:\blah.bmp");
}
}
Bitmap.LockBits Method
将位图锁定到系统内存中。
Copies data from a managed array to an unmanaged memory pointer, or from an unmanaged memory pointer to a managed array.
如前所述,只需在 32bpp 内工作,让您的生活更轻松
public static void Main()
{
var height = 2;
var width = 2;
var c = Color.White.ToArgb();
var output = new int[4] { c, c, c, c };
using (var bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed))
{
var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
Marshal.Copy(output, 0, data.Scan0, 4);
bitmap.UnlockBits(data);
bitmap.Save(@"D:\trdy.bmp");
}
}