Convert Array of Int to image

Discussion in 'ASP .Net' started by Clas Hortien, Sep 22, 2004.

  1. Clas Hortien

    Clas Hortien Guest

    Hi,

    i have a array of uint[] and want to display this array as a
    System.Web.UI.WebControls.Image.


    //=========================================
    uint [] ar = new uint[3072];
    string fName;

    System.Web.UI.WebControls.Image i = new System.Web.UI.WebControls.Image();
    vm.Display._GenerateThumbnail(ar);
    fName = "c:\\temp\\f_" + vm.Name + ".bmp";

    SaveArrayToFile(ar,fName);
    i.ImageUrl = fName;
    i.ID = "I_1";
    Panel1.Controls.Add(i);

    private void SaveArrayToFile(uint [] ar, string FileName)
    {
    FileStream fs = new FileStream(FileName, FileMode.Create);
    BinaryWriter w = new BinaryWriter(fs);
    for (int i = 0; i < ar.Length; i++)
    {
    w.Write(ar);
    }
    w.Close();
    fs.Close();
    }

    //=========================================

    The problem is, that the pictures aren't shown, as the picures are garbage.
    What is wrong ?

    Best regards

    Clas

    --
    The light at the end of the tunnel,
    was switch off due to high costs.
     
    Clas Hortien, Sep 22, 2004
    #1
    1. Advertising

  2. Hi Clas,

    Let's first step through your code. You create an Image class instance. Then
    you call a method which you haven't told us anything about. It seems to be
    generating a thumbnail from the (empty) image you just created, although I
    can't tell for sure, again, because you've told us nothing about it. But it
    gets worse. Now you call a method called "SaveArrayToFile" which writes the
    array to a file specified in the previous line. Note that you have abandoned
    the Image class which you created empty. You never do anything with it, nor
    do you associate it in any way with a file. I have no idea why you would
    think that this creates an image of any kind. An image file is not created
    by writing an array of unsigned integers to a file. What you DO get is a
    binary file containing an array of unsigned integers.

    Okay, let's talk about how to do it right. Yes, you DO need to create an
    Image, but more specifically, a Bitmap. You need to make sure that no value
    in your array is greater than 255, or less than 0. That is the maximum and
    minimum values of a pixel byte. And your array elements must match the size
    of a byte, or you will have to convert them on the fly, which is expensive.
    And your array must have "width * height" values in it (width and height of
    the Bitmap).

    Now you have 2 choices. You can use Managed code to loop through the pixels,
    using the Bitmap.GetPixel() method, and set the values of each R,G, and B
    byte, or you can use an unsafe code block (more complicated, but much
    faster) to loop through the pixels of the Bitmap with a pointer, and set
    them that way.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    I get paid good money to
    solve puzzles for a living


    "Clas Hortien" <> wrote in message
    news:cis56o$6q6$-ag.de...
    > Hi,
    >
    > i have a array of uint[] and want to display this array as a
    > System.Web.UI.WebControls.Image.
    >
    >
    > //=========================================
    > uint [] ar = new uint[3072];
    > string fName;
    >
    > System.Web.UI.WebControls.Image i = new System.Web.UI.WebControls.Image();
    > vm.Display._GenerateThumbnail(ar);
    > fName = "c:\\temp\\f_" + vm.Name + ".bmp";
    >
    > SaveArrayToFile(ar,fName);
    > i.ImageUrl = fName;
    > i.ID = "I_1";
    > Panel1.Controls.Add(i);
    >
    > private void SaveArrayToFile(uint [] ar, string FileName)
    > {
    > FileStream fs = new FileStream(FileName, FileMode.Create);
    > BinaryWriter w = new BinaryWriter(fs);
    > for (int i = 0; i < ar.Length; i++)
    > {
    > w.Write(ar);
    > }
    > w.Close();
    > fs.Close();
    > }
    >
    > //=========================================
    >
    > The problem is, that the pictures aren't shown, as the picures are

    garbage.
    > What is wrong ?
    >
    > Best regards
    >
    > Clas
    >
    > --
    > The light at the end of the tunnel,
    > was switch off due to high costs.
     
    Kevin Spencer, Sep 22, 2004
    #2
    1. Advertising

  3. Clas Hortien <> wrote:
    > i have a array of uint[] and want to display this array as a
    > System.Web.UI.WebControls.Image.


    Well, how does the array of uints relate to the actual image? Where did
    you get them from?

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Sep 22, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S300
    Replies:
    4
    Views:
    22,001
    Roedy Green
    Aug 19, 2003
  2. Schnoffos
    Replies:
    2
    Views:
    1,221
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  4. arun
    Replies:
    8
    Views:
    457
    Dave Thompson
    Jul 31, 2006
  5. Alain Spineux
    Replies:
    6
    Views:
    634
    Eric Sosman
    May 17, 2011
Loading...

Share This Page