System.Web.UI.WebControls.Image 不包含保存的定义?保存图像

System.Web.UI.WebControls.Image does not contain a definition for Save? Saving image

我有一个 C# 网络应用程序,并且有一个从字节数组生成的图像。

byte[] imageBytes = Convert.FromBase64String(b64);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);
            ImageStudent.ImageUrl = "data:image/jpeg;base64," + b64;

这会在网络应用程序中完美地显示图像。但是我想将此图像保存到项目中的文件夹中。我试过这段代码:

String FilePath;
            FilePath = Server.MapPath("~/Images/test2.jpg");
            ImageStudent.Save(FilePath, ImageFormat.Jpeg);

但是我收到错误:

System.Web.UI.WebControls.Image does not contain a definition for Save

我使用的所有 .dll 都需要在网络应用程序上用于其他功能,还有其他方法可以保存此图像吗?

System.Drawing.Image 是您要查找的命名空间。

System.Drawing.Bitmap b = new System.Drawing.Bitmap(Server.MapPath("~/Images/test2.jpg"));
b.Save(saveFilePath , System.Drawing.Imaging.ImageFormat.Jpeg);

您只需要获取字节数组并将其保存在您想要的任何位置。一个基本示例是这样的:

File.WriteAllBytes(@"C:\CopyofImage.jpg", imageByte);

除非您自己扩展它们,否则您无法为现有库(如保存在图像上 class)编写新方法。