这是我保存图片的方式。
[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);
//...
}
}
我不想显示该位置的图像。我想先阅读它以便进一步处理。
在这种情况下如何阅读图像文件?
答案 0 :(得分:19)
更新:将图像读取为字节[]
// 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实例吗?
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);