-1

I need to display an image using the download url . I am using the below logic to convert it into a base 64 string.

HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(downloadUrl); 
HttpWebResponse fileResp = (HttpWebResponse)(fileReq.GetResponse()); 
fileResp.GetResponseStream().CopyTo(ms); 

byte[] byteArray = ms.ToArray(); 

if (byteArray != null) 
{ 
return Convert.ToBase64String(byteArray); 
}

Trying to use this to display the image. I get the Base 64 string correctly in "response.data"

<img src="data:image/tiff;charset=utf-8;base64,' + response.data + '" />

Still I am seeing only a broken image. Anything that I am missing ?

wickjon
  • 900
  • 5
  • 14
  • 40
  • is Tiff an image format that is supported in browser like that? – Mike Miller Sep 23 '14 at 15:45
  • possible duplicate of [Is there an HTML5 method for displaying a tiff image data already loaded into memory](http://stackoverflow.com/questions/16310801/is-there-an-html5-method-for-displaying-a-tiff-image-data-already-loaded-into-me) – MrTux Sep 23 '14 at 15:45
  • Do you know for sure it is a `tiff` file? I've only worked with png in the past. Also, what does response.data actually look like (e.g. first 20 chars). and is the tag and its src attribute being rendered accordingly? – ne1410s Sep 23 '14 at 15:46
  • Yes it is an tiff image for sure. It starts with "SUkqAAgAAAAQAP4ABAABAAAAAAAAAAABBAABAAAAaQkAAAEBBAABAAAAWg0/....." Yes they are displayed as broken ones. @ne1410s – wickjon Sep 23 '14 at 15:49
  • But if you were to inspect the dynamic element (in a modern browser e.g. latest version of Chrome), does the src attribute actually equate to what you are expecting? I'm sure it probably does, I was just unfamiliar with your `` string syntax – ne1410s Sep 23 '14 at 15:55
  • Yes it does.. but it just shows a broken image @ne1410s – wickjon Sep 23 '14 at 15:56
  • 1
    Are you running on a TIFF-supporting browser? http://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support – ne1410s Sep 23 '14 at 15:57
  • If your image data is correct, it would appear your browser does not support the format. To test if it is correct, you may have luck with: http://www.askapache.com/online-tools/base64-image-converter/ – ne1410s Sep 23 '14 at 16:00

1 Answers1

0

I have now converted the tiff to jpeg which works. Sadly tiff is not supported in major browsers

 if (byteArray != null) 
{ 
byte[] jpegBytes; 
using (MemoryStream inStream = new MemoryStream(byteArray)) 
using (MemoryStream outStream = new MemoryStream()) 
{ 
System.Drawing.Bitmap.FromStream(inStream).Save(outStream,     System.Drawing.Imaging.ImageFormat.Jpeg); 

jpegBytes = outStream.ToArray(); 
return Convert.ToBase64String(jpegBytes); 
} 
}
wickjon
  • 900
  • 5
  • 14
  • 40