BinaryWrite GIF from Binary String

A

andrew.douglas11

Hello,

I'm looking to display an image in the browser using a binary string
containing all the bytes that make up a GIF image. I've tried all
the standard encodings in System.Text.Encoding, but the images aren't
showing up. I'm using the approach where I'm setting ImageURL =
"GetImage.aspx?id=1". Here's my code from GetImage.aspx:

Dim imageData As String = the binary data representing a .gif file
Dim imageBytes() As Byte =
System.Text.Encoding.ASCII.GetBytes(imageData)

Response.Expires = 0
Response.Buffer = True
Response.Clear()
Response.ContentType = "image/gif"
Response.BinaryWrite(imageBytes)
Response.End()

Can anyone offer any insight as to why this won't work? Using the
ASCII encoding, imageBytes.Length is the same size as
imageData.Length, but all the other standard encodings differ. Also,
if I use ASCII encoding and write the byte array to a file, I noticed
that the file size is very close to, but doesn't match the original
exactly. If I open the 2 different files using Notepad, I see special
characters like the Euro in the working original .gif file, but all
the special characters in the other file show up as a question mark
character.

Any help is greatly appreciated!

Andy
 
P

Peter Bromberg [C# MVP]

I'm not exactly sure what your goal is here, string is not necessarily
"binary data".

Here is some code that does work, if it helps:


protected void Page_Load(object sender, EventArgs e)
{
WebClient wc = new WebClient();
string url = "http://www.google.com/intl/en_ALL/images/logo.gif";
byte[] b = wc.DownloadData(url);
Response.Buffer = true;
Response.Clear();
Response.ContentType = "image/gif";
Response.BinaryWrite(b);
}
 
B

bruce barker

you need to know which encoding was used to convert the binary data to a
string. then you use the matching decode. if ascii encoding was used, this is
only 7bit, so data was lost converting to a string (high bit), so there is no
way to get it back on the decode.

-- bruce (sqlwork.com)
 
A

andrew.douglas11

Thanks for the replies - how do I find how the encoding? It's
whatever encoding that .Net uses in GDI+ to save a .GIF file. Here's
how the image is created:

'From: http://www.chrisfrazier.net/blog/archive/2005/05/27/276.aspx
Dim stream As New
System.IO.MemoryStream(Convert.FromBase64String(imageData))
Dim noteImage As System.Drawing.Bitmap =
DirectCast(System.Drawing.Image.FromStream(stream),
System.Drawing.Bitmap)
noteImage.Save(fs, System.Drawing.Imaging.ImageFormat.Gif)
stream.Close()

After this code runs, the file is stored to a String field in a Siebel
database (I realize that a different type might be preferable).
However, the .gif file is created correctly in the local file system,
and when I pull the string OUT of the database, it's the exact same
length in bytes as the file.

I've tried all the standard encodings (Unicode, UTF7, UTF8, UTF32,
BigEndianUnicode, and Default), and none of them seem to work. ASCII
gets me the closest to the correct byte size though. Is this a case
where I need to resort to referring to a numbered CodePage?

Thanks!

Andy
 
A

Andrew Morton

Thanks for the replies - how do I find how the encoding? It's
whatever encoding that .Net uses in GDI+ to save a .GIF file. Here's
how the image is created:
<snip>

..Net (and everything else) doesn't use any encoding to save a .gif file -
it's just a load of bytes with no relation to text.
I've tried all the standard encodings (Unicode, UTF7, UTF8, UTF32,
BigEndianUnicode, and Default), and none of them seem to work. ASCII
gets me the closest to the correct byte size though. Is this a case
where I need to resort to referring to a numbered CodePage?

The chrisfrazier web site is being too slow for me to see a reason for you
wanting to treat binary data as text, but basically you want the system to
use an 8-bit character set. UTF and ASCII are not; Windows-1252 and so on
are.

If the gif file exists on disk, you can use Response.WriteFile().

Andrew
 
A

Andrew Douglas

Got it working - thanks everyone for the replies! Bruce was correct
about losing data. The image data was coming from a Base64 String,
and I was losing data going to String. With
System.Convert.ToBase64String(data), all the bytes are as they were
when I saved the .gif.

Thanks all!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top