// 性能最高,其数组和像素一一对应
public static void test1(Image img)
{
Bitmap bmp = new Bitmap(img);
BitmapData bitmapData = bmp.LockBits(new Rectangle(new Point(0, 0), img.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte[] BGRValues = new byte[bitmapData.Stride * bitmapData.Height];
IntPtr Ptr = bitmapData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(Ptr, BGRValues, 0, BGRValues.Length);
bmp.UnlockBits(bitmapData);
}
// 性能较低,数组内容较少,内容未知
public static void test2(Image img)
{
System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();
byte[] btImage1 = new byte[0];
btImage1 = (byte[])ic.ConvertTo(img, btImage1.GetType());
}
// 性能较低,数组内容为图片格式内容,格式未知
public static void test3(Image img)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms,ImageFormat.Bmp);
byte[] byteImage = new Byte[0];
byteImage = ms.ToArray();
}
从图像转换到byte[]数组的几种方法
最新推荐文章于 2019-09-29 10:45:00 发布
本文介绍三种将图片转换为字节数组的方法,并对比它们的性能特点。第一种方法通过锁定位图获取像素数据,性能最高;第二种方法使用ImageConverter,数组内容较少且未知;第三种方法通过内存流保存图片为特定格式。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Qwen-Image
图片生成
Qwen
Qwen-Image是阿里云通义千问团队于2025年8月发布的亿参数图像生成基础模型,其最大亮点是强大的复杂文本渲染和精确图像编辑能力,能够生成包含多行、段落级中英文文本的高保真图像
379

被折叠的 条评论
为什么被折叠?



