简体   繁体   English

从图像转换为Base64返回错误

[英]Converting from Image to Base64 returns Error

I am trying to convert an image into a base64 String so i can save in a column in my database. 我正在尝试将图像转换为base64字符串,以便可以保存在数据库的列中。 Now i see this method 现在我看到这种方法

public string ImageToBase64(Image image,System.Drawing.Imaging.ImageFormat format)
{
    using (MemoryStream ms = new MemoryStream())
    {
        // Convert Image to byte[]
        image.Save(ms, format);
        byte[] imageBytes = ms.ToArray();

        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(imageBytes);
        return base64String;
    }
}

And it gives me an Error in ASP.NET saying it does not contain the definition for Save , asks if i am missing an Assembly reference. 它给我一个ASP.NET错误,说它不包含Save的定义,询问我是否缺少Assembly引用。 And when i call it in the main method like this 当我在这样的主要方法中调用它时

string base64ImageString = ImageToBase64(img, System.Drawing.Imaging.ImageFormat.Jpeg);

of which the main code looks like this now : 现在,其中的主要代码如下所示:

        sigObj.SetImageFileFormat(0);
        sigObj.SetImageXSize(500);
        sigObj.SetImageYSize(150);
        sigObj.SetImagePenWidth(8);
        sigObj.SetJustifyX(5);
        sigObj.SetJustifyY(5);
        sigObj.SetJustifyMode(5);
        System.Drawing.Image img = sigObj.GetSigImage();
        base64ImageString = ImageToBase64(img, System.Drawing.Imaging.ImageFormat.Jpeg);

It gives me another funny error there which looks like this 它给了我另一个有趣的错误,看起来像这样

Cannot convert from System.Drawing.Image to System.Web.UI.Web.Controls.Image

Please what exactly am I doing wrong. 请我到底在做什么错。 I am doing this on ASP.NET webform 我正在ASP.NET Webform上执行此操作

The declaration of your method is getting clashed with System.Drawing.Image and System.Web.UI.Controls. 方法的声明与System.Drawing.Image和System.Web.UI.Controls发生冲突。 Use it like 像这样使用

public string ImageToBase64(System.Drawing.Image image,System.Drawing.Imaging.ImageFormat format)

The last error tells you exactly what the issue is. 最后一个错误告诉您确切的问题是什么。

In the code file you have a reference to System.Web.UI.Web.Controls.Image , so in the method signature your reference to Image image is actually referring to that package. 在代码文件中,您有对System.Web.UI.Web.Controls.Image的引用,因此在方法签名中,对Image image的引用实际上是在指该包。

You can test this by hovering over the reference and you should see the full path as above. 您可以通过将鼠标悬停在参考上来进行测试,并且应该看到上面的完整路径。

The reference that you actually need (and that contains the Save() method that you're trying to use is System.Drawing.Image . 您实际需要的引用(包含您要使用的Save()方法Save()System.Drawing.Image

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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