473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

BinaryWrite GIF from Binary String

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
Mar 11 '08 #1
5 3385
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);
}

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
"an**************@gmail.com" wrote:
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
Mar 11 '08 #2
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)
"an**************@gmail.com" wrote:
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
Mar 11 '08 #3
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/arc...05/27/276.aspx
Dim stream As New
System.IO.MemoryStream(Convert.FromBase64String(im ageData))
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
Mar 12 '08 #4
an**************@gmail.com wrote:
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
Mar 12 '08 #5
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!
Mar 12 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin Humphreys | last post by:
Hi, How can I output the Response.BinaryWrite content in an asp page that has html tags inside? I need to write the binary data to the client browser inside the html tags. Thanks In Advance,...
1
by: Kevin Humphreys | last post by:
Hi All, Please help me regarding the 'Response.BinaryWrite' I am making one web application where I need to store some of client logo's and others images. In this context I am able to store...
0
by: Bill | last post by:
I'm using the example shown at : http://support.microsoft.com:80/support/kb/articles/q276/4/88.asp It involves using ASP/VbScript and ADODB.Stream to read binary data from a *.DOC or *.RTF file...
5
by: katrinaVictim | last post by:
Question: I get the eror listed at the bottom of the post. What can I do to make the response of the x1.send a "binary" type? Or, in general, how can I just "make this work"? <%@...
2
by: Bammer22 | last post by:
I am trying to BinaryWrite an image from a binary string that I am storing in the web.config file. The image is just a 1x1 pixel gif. I am setting the content type to "image/gif", but the output is...
2
by: Nik | last post by:
I am trying to write out the equivalent of this asp statement in c#: Response.BinaryWrite(chrb(239) & chrb(187) & chrb(191)) 'BOM = EF BB BF string binString = "11111111"; byte myBin =...
1
by: abcd | last post by:
I am writing a page which uses response.binarywrite to the client, after it has completed sending the file I am interested in I want to display some mesange like "download completed" etc...I am...
5
by: twiggy182 | last post by:
Hi, I really need you help because I'm not very familliar with ASP and I could not find any solution to my problem. To put you in situation, I have a CGI to which I send a file name, and that...
1
by: mattridings | last post by:
Hi gang, Have a script that works fine. However, it's really cpu intensive and I'm looking for suggestions on a) whether or not that's normal and if so b)a better way of doing it. Script is...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.