如何将图像文件读取为byte []? [英] How to read an image file to a byte[]?
本文介绍了如何将图像文件读取为byte []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我保存图像的方式.
This is how I save images.
[HttpPost]
public ActionResult Create(HttpPostedFileBase file)
{
if (file != null)
{
var extension = Path.GetExtension(file.FileName);
var fileName = Guid.NewGuid().ToString() + extension;
var path = Path.Combine(Server.MapPath("~/Content/Photos"), fileName);
file.SaveAs(path);
//...
}
}
我不想显示该位置的图像.我想先阅读它以进行进一步处理.
I don't want to display the image from that location. I want rather to read it first for further processing.
在这种情况下,如何读取图像文件?
How do I read the image file in that case?
推荐答案
更新:将图像读取到一个字节[]
Update: Reading the image to a byte[]
// Load file meta data with FileInfo
FileInfo fileInfo = new FileInfo(path);
// The byte[] to save the data in
byte[] data = new byte[fileInfo.Length];
// Load a filestream and put its content into the byte[]
using (FileStream fs = fileInfo.OpenRead())
{
fs.Read(data, 0, data.Length);
}
// Delete the temporary file
fileInfo.Delete();
// Post byte[] to database
为了历史,这是我在问题明确之前的答案.
您是说要将其作为 BitMap 实例加载?
Do you mean loading it as a BitMap instance?
BitMap image = new BitMap(path);
// Do some processing
for(int x = 0; x < image.Width; x++)
{
for(int y = 0; y < image.Height; y++)
{
Color pixelColor = image.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
image.SetPixel(x, y, newColor);
}
}
// Save it again with a different name
image.Save(newPath);
这篇关于如何将图像文件读取为byte []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文