-1

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

harry.luson
  • 247
  • 7
  • 19
  • the error is telling you exactly what the problem is.. you are trying to set `ImageData which is of type byte` to a `string` also they proper syntax is `public int Width { get; set; }` – MethodMan Feb 29 '16 at 16:28
  • `Encoding.GetBytes` creates a byte array from a string... but I'm not 100% sure, that's what you really want to do. – Haukinger Feb 29 '16 at 16:28

3 Answers3

1
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 ?

ToXinE
  • 308
  • 2
  • 13
  • I want to it directly in my code. Like I extract the image source in this line var Image= ImgresponseJson.query.pages[ImgfirstKey].thumbnail.source;. so basically this is the url .then in the next Line I want to do it directly. Is it possible – harry.luson Feb 29 '16 at 16:36
0

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

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()));
Kevin
  • 1,462
  • 9
  • 9
  • Thank you it is working. as ImageUrl is in string format. But if I want to keep it in byte format like my code byte[] ImageData { set; get; } is it possible anyway. – harry.luson Feb 29 '16 at 16:41
  • it shows error 'System.Text.Encoding.UTF8' is a 'property' but is used like a 'type' – harry.luson Feb 29 '16 at 16:50
  • Sorry, forgot to put in the (). – Kevin Feb 29 '16 at 16:53
  • @Kelvin I tried this one. this shows the output "ImageData": "QzgyRTRENjguanBn". BUt can I get 28F996F0.jpg this type of number from this code. – harry.luson Feb 29 '16 at 17:03
  • How are you trying to get the string back out? You'll have to reverse the process e.g. var testString = new System.Text.UTF8Encoding.GetString(img.ImageData) – Kevin Feb 29 '16 at 17:18
0

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());
Falco Alexander
  • 3,092
  • 2
  • 20
  • 39