BinaryWrite GIF from Binary String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrew.douglas11@gmail.com

    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.Enc oding, 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.Enc oding.ASCII.Get Bytes(imageData )

    Response.Expire s = 0
    Response.Buffer = True
    Response.Clear( )
    Response.Conten tType = "image/gif"
    Response.Binary Write(imageByte s)
    Response.End()

    Can anyone offer any insight as to why this won't work? Using the
    ASCII encoding, imageBytes.Leng th is the same size as
    imageData.Lengt h, 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
  • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

    #2
    RE: BinaryWrite GIF from Binary String

    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(objec t 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.Conten tType = "image/gif";
    Response.Binary Write(b);
    }

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short Urls & more: http://ittyurl.net


    "andrew.douglas 11@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.Enc oding, 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.Enc oding.ASCII.Get Bytes(imageData )
    >
    Response.Expire s = 0
    Response.Buffer = True
    Response.Clear( )
    Response.Conten tType = "image/gif"
    Response.Binary Write(imageByte s)
    Response.End()
    >
    Can anyone offer any insight as to why this won't work? Using the
    ASCII encoding, imageBytes.Leng th is the same size as
    imageData.Lengt h, 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
    >

    Comment

    • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

      #3
      RE: BinaryWrite GIF from Binary String

      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)


      "andrew.douglas 11@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.Enc oding, 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.Enc oding.ASCII.Get Bytes(imageData )
      >
      Response.Expire s = 0
      Response.Buffer = True
      Response.Clear( )
      Response.Conten tType = "image/gif"
      Response.Binary Write(imageByte s)
      Response.End()
      >
      Can anyone offer any insight as to why this won't work? Using the
      ASCII encoding, imageBytes.Leng th is the same size as
      imageData.Lengt h, 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
      >

      Comment

      • andrew.douglas11@gmail.com

        #4
        Re: BinaryWrite GIF from Binary String

        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.Memor yStream(Convert .FromBase64Stri ng(imageData))
        Dim noteImage As System.Drawing. Bitmap =
        DirectCast(Syst em.Drawing.Imag e.FromStream(st ream),
        System.Drawing. Bitmap)
        noteImage.Save( fs, System.Drawing. Imaging.ImageFo rmat.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,
        BigEndianUnicod e, 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

        Comment

        • Andrew Morton

          #5
          Re: BinaryWrite GIF from Binary String

          andrew.douglas1 1@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,
          BigEndianUnicod e, 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.WriteF ile().

          Andrew


          Comment

          • Andrew Douglas

            #6
            Re: BinaryWrite GIF from Binary String

            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!

            Comment

            Working...