简体   繁体   English

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

[英]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.

相关问题 选择Linq不能将类型byte []隐式转换为System.web.httpPostedFileBase - Select Linq cannot implicitly convert type byte[] to System.web.httpPostedFileBase 如何通过将字符串转换为System.Web.HttpPostedFileBase将图像保存在db中 - How to save Image in db by converting string to System.Web.HttpPostedFileBase 无法从程序集 System.Web 加载类型“System.Web.HttpPostedFileBase” - Could not load type 'System.Web.HttpPostedFileBase' from assembly System.Web 我如何将 Canvas 图像作为 HttpPostedFilebase object 发送? 有可能的? - How can i send Canvas Image as a HttpPostedFilebase object ? it is possible? 无法将类型“System.Drawing.Image”隐式转换为“System.Drawing.Bitmap” - Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'` 我应该将图像转换为HttpPostedFileBase吗? - Should I convert image into HttpPostedFileBase? 将EmguCV图像转换为系统绘图位图 - Convert EmguCV image to system drawing bitmap 将HttpPostedFileBase - pdf类型转换为png / jpg类型 - Convert HttpPostedFileBase - pdf type to png/jpg type 是否可以将网格转换为图像文件(例如位图)? - Is it possible to convert a grid to an image file such as a bitmap? 非System.Web替代HttpPostedFileBase? - Non System.Web substitute for HttpPostedFileBase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM