简体   繁体   中英

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

For my project I need to get the Image sourse as hash code like this 28F996F0.jpg. I am trying the following code to get this value but having one error-Cannot implicitly convert type 'string' to 'byte[]'.

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

My Json object class is

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

I could not get how to convert the image url to hash code like this 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);
    }
}

Is it what your looking for ?

You need to add a string property to your PoiImageAnswer class to contain the image url. eg

public string ImageUrl { get; set; }

Then:

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

EDIT:

This would allow you to put it into the byte[]:

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

simply modify the last class property:

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

then your code will work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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