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

时间:2017-10-25 01:55:13

标签: c# bitmap

好的,正如标题所示,我试图将单个Scanner input = new Scanner(System.in); int answer; public static void main(String[] args) { // TODO code application logic here int SIDES = 6; int a = 1 + (int) (Math.random() * SIDES); int b = 1 + (int) (Math.random() * SIDES); System.out.println("What is " + a + " - " + b + " = "); int difference = a - b; Scanner one = new Scanner (System.in); System.out.println("What is the answer?"); int answer = Integer.parseInt(one.next()); //int answer = one.next(); System.out.println("your answer is " + answer); if (answer==difference) { System.out.println("You are correct!"); System.out.println("You gain 5 points"); } else{ System.out.println("You are wrong."); System.out.println("The correct answer is " + difference); System.out.println("You lose 5 points"); } 张贴的图像(任何类型)转换为位图对象。我基本上需要这样做才能在保存文件后关闭对象。

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

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

1 个答案:

答案 0 :(得分:1)

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