有人能否建议我如何将图像转换为字节数组,反之亦然?
我正在开发一个 WPF 应用程序,并使用流读取器。
将图像转换为字节数组的示例代码
public byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms,imageIn.RawFormat);
return ms.ToArray();
}
}
imageIn.RawFormat
代替System.Drawing.Imaging.ImageFormat.Gif
。请注意确保翻译后的意思与原文一致,同时简化语言以提高易读性。 - S.Serpooshan(new Bitmap(imageIn)).Save(ms, imageIn.RawFormat);
。 - Markus Safar要将一个图像对象转换为byte[]
,你可以按照以下步骤进行:
public static byte[] converterDemo(Image x)
{
ImageConverter _imageConverter = new ImageConverter();
byte[] xByte = (byte[])_imageConverter.ConvertTo(x, typeof(byte[]));
return xByte;
}
.ConvertTo(new Bitmap(x), typeof(byte[]));
一样将“Image”传递给它。 - Markus SafarImageConverter
是 System.Drawing
命名空间的一部分。 - AR5HAM获取图像路径的字节数组的另一种方法是:
byte[] imgdata = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(path));
这是我目前在使用的内容。我尝试过的其他一些技术并不理想,因为它们改变了像素的位深度(24位与32位),或者忽略了图像的分辨率(dpi)。
// ImageConverter object used to convert byte arrays containing JPEG or PNG file images into
// Bitmap objects. This is static and only gets instantiated once.
private static readonly ImageConverter _imageConverter = new ImageConverter();
将图像转换为字节数组:
/// <summary>
/// Method to "convert" an Image object into a byte array, formatted in PNG file format, which
/// provides lossless compression. This can be used together with the GetImageFromByteArray()
/// method to provide a kind of serialization / deserialization.
/// </summary>
/// <param name="theImage">Image object, must be convertable to PNG format</param>
/// <returns>byte array image of a PNG file containing the image</returns>
public static byte[] CopyImageToByteArray(Image theImage)
{
using (MemoryStream memoryStream = new MemoryStream())
{
theImage.Save(memoryStream, ImageFormat.Png);
return memoryStream.ToArray();
}
}
字节数组转换为图片:
/// <summary>
/// Method that uses the ImageConverter object in .Net Framework to convert a byte array,
/// presumably containing a JPEG or PNG file image, into a Bitmap object, which can also be
/// used as an Image object.
/// </summary>
/// <param name="byteArray">byte array containing JPEG or PNG file image or similar</param>
/// <returns>Bitmap object if it works, else exception is thrown</returns>
public static Bitmap GetImageFromByteArray(byte[] byteArray)
{
Bitmap bm = (Bitmap)_imageConverter.ConvertFrom(byteArray);
if (bm != null && (bm.HorizontalResolution != (int)bm.HorizontalResolution ||
bm.VerticalResolution != (int)bm.VerticalResolution))
{
// Correct a strange glitch that has been observed in the test program when converting
// from a PNG file image created by CopyImageToByteArray() - the dpi value "drifts"
// slightly away from the nominal integer value
bm.SetResolution((int)(bm.HorizontalResolution + 0.5f),
(int)(bm.VerticalResolution + 0.5f));
}
return bm;
}
编辑:要从jpg或png文件获取图像,您应该使用File.ReadAllBytes()将文件读入字节数组中:
Bitmap newBitmap = GetImageFromByteArray(File.ReadAllBytes(fileName));
这样可以避免与Bitmap相关的问题,因为Bitmap希望其源流保持打开状态,而一些建议的解决方法会导致源文件被锁定。
ImageConverter _imageConverter = new ImageConverter(); lock(SourceImage) { return (byte[])_imageConverter.ConvertTo(SourceImage, typeof(byte[])); }
但是,有时会出现两种不同大小的数组。通常在大约100次迭代后会发生这种情况。但是,当我使用 new Bitmap(SourceFileName);
获取位图,然后运行该代码时,它可以正常工作。 - Moon试试这个:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
您可以使用File.ReadAllBytes()
方法将任何文件读入字节数组中。要将字节数组写入文件中,只需使用File.WriteAllBytes()
方法即可。
希望这能帮到您。
您可以在这里找到更多信息和示例代码。
如果您没有引用“imageBytes”来传输流中的字节,该方法将不会返回任何内容。请确保引用“imageBytes = m.ToArray();”。
public static byte[] SerializeImage() {
MemoryStream m;
string PicPath = pathToImage";
byte[] imageBytes;
using (Image image = Image.FromFile(PicPath)) {
using ( m = new MemoryStream()) {
image.Save(m, image.RawFormat);
imageBytes = new byte[m.Length];
//Very Important
imageBytes = m.ToArray();
}//end using
}//end using
return imageBytes;
}//SerializeImage
var bitmap = new BitmapImage(uri);
//Pixel array
byte[] pixels = new byte[width * height * 4]; //account for stride if necessary and whether the image is 32 bit, 16 bit etc.
bitmap.CopyPixels(..size, pixels, fullStride, 0);
代码:
using System.IO;
byte[] img = File.ReadAllBytes(openFileDialog1.FileName);
这是将任何类型的图像(例如PNG、JPG、JPEG)转换为字节数组的代码
public static byte[] imageConversion(string imageName){
//Initialize a file stream to read the image file
FileStream fs = new FileStream(imageName, FileMode.Open, FileAccess.Read);
//Initialize a byte array with size of stream
byte[] imgByteArr = new byte[fs.Length];
//Read data from the file stream and put into the byte array
fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
//Close a file stream
fs.Close();
return imageByteArr
}