[英]Is it possible to convert a single System.Web.HttpPostedFileBase image (any type) to a bitmap object?
OK, so as the title suggests, I am attempting to convert a single System.Web.HttpPostedFileBase
posted image (any type) to a bitmap object. 好的,如标题所示,我试图将单个
System.Web.HttpPostedFileBase
发布的图像(任何类型)转换为位图对象。 I basically need to do this in order to close the object after I save the file. 保存文件后,我基本上需要执行此操作以关闭对象。
Here is my code so far: 到目前为止,这是我的代码:
var PortraitImage = Request.Files["PortraitImageSelector"];
string FileName = Guid.NewGuid().ToString() + Path.GetExtension(PortraitImage.FileName);
string PortraitImageFullLocation = Path.Combine(Server.MapPath(_VirtualPath), FileName);
string PortraitImageURL = string.Format("{0}/{1}", _VirtualPath, FileName);
using (var bmp = new Bitmap(PortraitImageFullLocation))
{
bmp.Save(PortraitImageFullLocation);
}
Unfortunately, when I run this I get an exception stating the following: 不幸的是,当我运行此程序时,出现异常,说明以下内容:
{"Parameter is not valid."}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147024809
HelpLink: null
InnerException: null
Message: "Parameter is not valid."
ParamName: null
Source: "System.Drawing"
StackTrace: " at System.Drawing.Bitmap..ctor(String filename)\r\n at johneschultz.Areas.Admin.Controllers.EmployeeController.Edit(EmployeeViewModel EmployeeData) in Z:\\_Profile Storage\\Projects\\johneschultz\\johneschultz\\Areas\\Admin\\Controllers\\EmployeeController.cs:line 552"
TargetSite: {Void .ctor(System.String)}
To save the HttpPostedFileBase
as a BitMap
, simply use the InputStream
because BitMap
has a constructor which takes a Stream
: 要将
HttpPostedFileBase
保存为BitMap
,只需使用InputStream
因为BitMap
有一个采用Stream
的构造函数:
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(this.Request.Files[0].InputStream))
{
bmp.Save("whatever.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.