图片转换为Byte[]

private byte[] FileContent()
{
HttpFileCollection files = HttpContext.Current.Request.Files;
byte[] result;
if (files.Count > 1)
{
base.Alert(“只能选择一个文件!”);
result = null;
}
else
{
if (files.Count == 0)
{
base.Alert(“请先选择上传的文件!”);
result = null;
}
else
{
HttpPostedFile httpPostedFile = files[0];
string extension = Path.GetExtension(httpPostedFile.FileName);
try
{
byte[] array = new byte[httpPostedFile.ContentLength];
if (extension.ToLower() == “.jpeg” || extension.ToLower() == “.bmp” || extension.ToLower() == “.png” || extension.ToLower() == “.jpg” || extension.ToLower() == “.gif”)
{
httpPostedFile.InputStream.Read(array, 0, httpPostedFile.ContentLength);
result = array;
}
else
{
base.Alert(“上传的不是图片文件!”);
result = null;
}
}
catch (Exception ex)
{
base.Alert(“上传文件出错!” + (ConfigurationSetting.Debug ? (“错误信息如下:\\r\\n[” + ex.Message + “]”) : “”));
result = null;
}
}
}
return result;
}

你可能感兴趣的:(图片转换为Byte[])