繁体   English   中英

是否可以将单个System.Web.HttpPostedFileBase图像(任何类型)转换为位图对象?

[英]Is it possible to convert a single System.Web.HttpPostedFileBase image (any type) to a bitmap object?

好的,如标题所示,我试图将单个System.Web.HttpPostedFileBase发布的图像(任何类型)转换为位图对象。 保存文件后,我基本上需要执行此操作以关闭对象。

到目前为止,这是我的代码:

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);
}

不幸的是,当我运行此程序时,出现异常,说明以下内容:

{"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)}

要将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM