Using C#, is there a better way to convert a Windows Bitmap
to a byte[]
than saving to a temporary file and reading the result using a FileStream
?
使用c#,是否有更好的方法将Windows位图转换为字节[],而不是将其保存到临时文件中并使用文件流读取结果?
10 个解决方案
#1
323
There are a couple ways.
有几种方法。
ImageConverter
ImageConverter
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
This one is convenient because it doesn't require a lot of code.
这个很方便,因为它不需要很多代码。
Memory Stream
内存流
public static byte[] ImageToByte2(Image img)
{
using (var stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream.ToArray();
}
}
This one is equivalent to what you are doing, except the file is saved to memory instead of to disk. Although more code you have the option of ImageFormat and it can be easily modified between saving to memory or disk.
这个文件相当于您正在做的事情,除了文件被保存到内存中而不是磁盘。虽然有更多的代码可以选择ImageFormat,但是可以很容易地在保存到内存或磁盘之间进行修改。
Source: http://www.vcskicks.com/image-to-byte.php
来源:http://www.vcskicks.com/image-to-byte.php
#2
79
A MemoryStream can be helpful for this. You could put it in an extension method:
MemoryStream对这方面很有帮助。你可以用扩展法:
public static class ImageExtensions
{
public static byte[] ToByteArray(this Image image, ImageFormat format)
{
using(MemoryStream ms = new MemoryStream())
{
image.Save(ms, format);
return ms.ToArray();
}
}
}
You could just use it like:
你可以这样使用:
var image = new Bitmap(10, 10);
// Draw your image
byte[] arr = image.ToByteArray(ImageFormat.Bmp);
I partially disagree with prestomanifto's answer in regards to the ImageConverter. Do not use ImageConverter. There's nothing technically wrong with it, but simply the fact that it uses boxing/unboxing from object tells me it's code from the old dark places of the .NET framework and its not ideal to use with image processing (it's overkill for converting to a byte[] at least), especially when you consider the following.
我不同意前口万尼夫托关于图像处理程序的回答。不要使用ImageConverter。在技术上没有什么问题,只是事实,它使用拳击/拆箱从对象告诉我它的代码从旧的黑暗之处,. net框架及其与图像处理的不理想的使用(它是多余转换成至少一个字节[]),特别是当你考虑以下。
I took a look at the ImageConverter
code used by the .Net framework, and internally it uses code almost identical to the one I provided above. It creates a new MemoryStream
, saves the Bitmap
in whatever format it was in when you provided it, and returns the array. Skip the extra overhead of creating an ImageConverter
class by using MemoryStream
我查看了。net框架使用的ImageConverter代码,在内部它使用的代码与我上面提供的代码几乎相同。它创建一个新的MemoryStream,以您提供时的任何格式保存位图,并返回数组。通过使用MemoryStream,可以跳过创建ImageConverter类的额外开销
#3
37
You can also just Marshal.Copy the bitmap data. No intermediary memorystream etc. and a fast memory copy. This should work on both 24-bit and 32-bit bitmaps.
你也可以整理一下。复制位图数据。没有中间内存流等,也没有快速的内存拷贝。这应该同时适用于24位和32位位的位图。
public static byte[] BitmapToByteArray(Bitmap bitmap)
{
BitmapData bmpdata = null;
try
{
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
int numbytes = bmpdata.Stride * bitmap.Height;
byte[] bytedata = new byte[numbytes];
IntPtr ptr = bmpdata.Scan0;
Marshal.Copy(ptr, bytedata, 0, numbytes);
return bytedata;
}
finally
{
if (bmpdata != null)
bitmap.UnlockBits(bmpdata);
}
}
.
。
#4
12
Save the Image to a MemoryStream and then grab the byte array.
将图像保存到MemoryStream中,然后获取字节数组。
http://msdn.microsoft.com/en-us/library/ms142148.aspx
http://msdn.microsoft.com/en-us/library/ms142148.aspx
Byte[] data;
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, ImageFormat.Bmp);
data = memoryStream.ToArray();
}
#5
9
Use a MemoryStream
instead of a FileStream
, like this:
使用MemoryStream而不是FileStream,如下所示:
MemoryStream ms = new MemoryStream();
bmp.Save (ms, ImageFormat.Jpeg);
byte[] bmpBytes = ms.ToArray();
#6
7
Try the following:
试试以下:
MemoryStream stream = new MemoryStream();
Bitmap bitmap = new Bitmap();
bitmap.Save(stream, ImageFormat.Jpeg);
byte[] byteArray = stream.GetBuffer();
Make sure you are using:
确保你正在使用:
System.Drawing & using System.Drawing.Imaging;
#7
5
I believe you may simply do:
我相信你可以这么做:
ImageConverter converter = new ImageConverter();
var bytes = (byte[])converter.ConvertTo(img, typeof(byte[]));
#8
5
MemoryStream ms = new MemoryStream();
yourBitmap.Save(ms, ImageFormat.Bmp);
byte[] bitmapData = ms.ToArray();
#9
4
More simple:
更简单:
return (byte[])System.ComponentModel.TypeDescriptor.GetConverter(pImagen).ConvertTo(pImagen, typeof(byte[]))
#10
-2
Very simple use this just in one line:
很简单,只用一句话:
byte[] imgdata = File.ReadAllBytes(@"C:\download.png");
#1
323
There are a couple ways.
有几种方法。
ImageConverter
ImageConverter
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
This one is convenient because it doesn't require a lot of code.
这个很方便,因为它不需要很多代码。
Memory Stream
内存流
public static byte[] ImageToByte2(Image img)
{
using (var stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream.ToArray();
}
}
This one is equivalent to what you are doing, except the file is saved to memory instead of to disk. Although more code you have the option of ImageFormat and it can be easily modified between saving to memory or disk.
这个文件相当于您正在做的事情,除了文件被保存到内存中而不是磁盘。虽然有更多的代码可以选择ImageFormat,但是可以很容易地在保存到内存或磁盘之间进行修改。
Source: http://www.vcskicks.com/image-to-byte.php
来源:http://www.vcskicks.com/image-to-byte.php
#2
79
A MemoryStream can be helpful for this. You could put it in an extension method:
MemoryStream对这方面很有帮助。你可以用扩展法:
public static class ImageExtensions
{
public static byte[] ToByteArray(this Image image, ImageFormat format)
{
using(MemoryStream ms = new MemoryStream())
{
image.Save(ms, format);
return ms.ToArray();
}
}
}
You could just use it like:
你可以这样使用:
var image = new Bitmap(10, 10);
// Draw your image
byte[] arr = image.ToByteArray(ImageFormat.Bmp);
I partially disagree with prestomanifto's answer in regards to the ImageConverter. Do not use ImageConverter. There's nothing technically wrong with it, but simply the fact that it uses boxing/unboxing from object tells me it's code from the old dark places of the .NET framework and its not ideal to use with image processing (it's overkill for converting to a byte[] at least), especially when you consider the following.
我不同意前口万尼夫托关于图像处理程序的回答。不要使用ImageConverter。在技术上没有什么问题,只是事实,它使用拳击/拆箱从对象告诉我它的代码从旧的黑暗之处,. net框架及其与图像处理的不理想的使用(它是多余转换成至少一个字节[]),特别是当你考虑以下。
I took a look at the ImageConverter
code used by the .Net framework, and internally it uses code almost identical to the one I provided above. It creates a new MemoryStream
, saves the Bitmap
in whatever format it was in when you provided it, and returns the array. Skip the extra overhead of creating an ImageConverter
class by using MemoryStream
我查看了。net框架使用的ImageConverter代码,在内部它使用的代码与我上面提供的代码几乎相同。它创建一个新的MemoryStream,以您提供时的任何格式保存位图,并返回数组。通过使用MemoryStream,可以跳过创建ImageConverter类的额外开销
#3
37
You can also just Marshal.Copy the bitmap data. No intermediary memorystream etc. and a fast memory copy. This should work on both 24-bit and 32-bit bitmaps.
你也可以整理一下。复制位图数据。没有中间内存流等,也没有快速的内存拷贝。这应该同时适用于24位和32位位的位图。
public static byte[] BitmapToByteArray(Bitmap bitmap)
{
BitmapData bmpdata = null;
try
{
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
int numbytes = bmpdata.Stride * bitmap.Height;
byte[] bytedata = new byte[numbytes];
IntPtr ptr = bmpdata.Scan0;
Marshal.Copy(ptr, bytedata, 0, numbytes);
return bytedata;
}
finally
{
if (bmpdata != null)
bitmap.UnlockBits(bmpdata);
}
}
.
。
#4
12
Save the Image to a MemoryStream and then grab the byte array.
将图像保存到MemoryStream中,然后获取字节数组。
http://msdn.microsoft.com/en-us/library/ms142148.aspx
http://msdn.microsoft.com/en-us/library/ms142148.aspx
Byte[] data;
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, ImageFormat.Bmp);
data = memoryStream.ToArray();
}
#5
9
Use a MemoryStream
instead of a FileStream
, like this:
使用MemoryStream而不是FileStream,如下所示:
MemoryStream ms = new MemoryStream();
bmp.Save (ms, ImageFormat.Jpeg);
byte[] bmpBytes = ms.ToArray();
#6
7
Try the following:
试试以下:
MemoryStream stream = new MemoryStream();
Bitmap bitmap = new Bitmap();
bitmap.Save(stream, ImageFormat.Jpeg);
byte[] byteArray = stream.GetBuffer();
Make sure you are using:
确保你正在使用:
System.Drawing & using System.Drawing.Imaging;
#7
5
I believe you may simply do:
我相信你可以这么做:
ImageConverter converter = new ImageConverter();
var bytes = (byte[])converter.ConvertTo(img, typeof(byte[]));
#8
5
MemoryStream ms = new MemoryStream();
yourBitmap.Save(ms, ImageFormat.Bmp);
byte[] bitmapData = ms.ToArray();
#9
4
More simple:
更简单:
return (byte[])System.ComponentModel.TypeDescriptor.GetConverter(pImagen).ConvertTo(pImagen, typeof(byte[]))
#10
-2
Very simple use this just in one line:
很简单,只用一句话:
byte[] imgdata = File.ReadAllBytes(@"C:\download.png");