繁体   English   中英

无法使用C#将类型'string'隐式转换为'byte []'

[英]Cannot implicitly convert type 'string' to 'byte[]' using C#

对于我的项目,我需要像哈希码这样的28F996F0.jpg获取图像源。 我正在尝试下面的代码来获取该值,但是有一个错误-无法将类型'string'隐式转换为'byte []'。

 var Image= ImgresponseJson.query.pages[ImgfirstKey].thumbnail.source;
 img.ImageData = string.Format("{0:X}.jpg", Image.GetHashCode());

我的Json对象类是

public class PoiImageAnswer
{
 public int Width { set; get; }
 public int Height { set; get; }
 public byte[] ImageData { set; get; }
}

我无法将图像网址转换为像这样的哈希码28F996F0.jpg

public class Hash
{
    public static string GetHash(string input)
    {
        HashAlgorithm hashAlgorithm = new SHA256CryptoServiceProvider();
        byte[] byteValue = Encoding.UTF8.GetBytes(input);
        byte[] byteHash = hashAlgorithm.ComputeHash(byteValue);
        return Convert.ToBase64String(byteHash);
    }
}

您在找什么吗?

您需要在PoiImageAnswer类中添加一个字符串属性,以包含图像URL。 例如

public string ImageUrl { get; set; }

然后:

img.ImageUrl = string.Format("{0:X}.jpg", Image.GetHashCode());

编辑:

这将允许您将其放入byte []中:

img.ImageData = new System.Text.UTF8Encoding().GetBytes(string.Format("{0:X}.jpg", Image.GetHashCode()));

只需修改last class属性:

public class PoiImageAnswer
{
 public int Width { set; get; }
 public int Height { set; get; }
 public string ImageDataFilename { set; get; }
}

那么您的代码将起作用:

string ImageURL = "http://kajsdkajdg.com/abc.jpg";
var ImageURLHash = string.Format("{0:X}.jpg", ImageURL.GetHashCode());

暂无
暂无

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

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