1

Is there anyway to convert a byte array to an image and display it directly in an image control without saving it on the disk?

Here is the code a I have developed so far:

    protected void btnShow_Click(object sender, System.EventArgs e)
    {
        Byte[] blobEncryptedImage = Signature.Get(txSaleGUID.Text);
        Crypto crypto = new Crypto("mypass");
        Byte[] decryptedImage = Encoding.ASCII.GetBytes(crypto.Decrypt(blobEncryptedImage));

        MemoryStream ms = new MemoryStream(decryptedImage);
        Image img = Image.FromStream(ms);
        //Image1.ImageUrl = System.Drawing.Image.FromStream(ms);
    }

    public static Image byteArrayToImage(byte[] data)
    {
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
        {
            return new System.Drawing.Bitmap(Image.FromStream(ms));
        }
    }

Update 1

this is my crypto class:

    public class Crypto
{
    private ICryptoTransform rijndaelDecryptor;
    // Replace me with a 16-byte key, share between Java and C#
    private static byte[] rawSecretKey = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    public Crypto(string passphrase)
    {
        byte[] passwordKey = encodeDigest(passphrase);
        RijndaelManaged rijndael = new RijndaelManaged();
        rijndaelDecryptor = rijndael.CreateDecryptor(passwordKey, rawSecretKey);
    }

    public string Decrypt(byte[] encryptedData)
    {
        byte[] newClearData = rijndaelDecryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
        return Encoding.ASCII.GetString(newClearData);
    }

    public string DecryptFromBase64(string encryptedBase64)
    {
        return Decrypt(Convert.FromBase64String(encryptedBase64));
    }

    private byte[] encodeDigest(string text)
    {
        MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] data = Encoding.ASCII.GetBytes(text);
        return x.ComputeHash(data);
    }
}

Could anyone give a clue?

Devester
  • 1,183
  • 4
  • 14
  • 41
  • Why are you using `Encoding.ASCII.GetBytes`? Remove that, it's binary data, not ASCII (text). – Jon Grant May 15 '13 at 15:06
  • 2
    You need to write your image bytes out to an output stream which can be separately called by by a URL. e.g src="http://www.example.com/GetImage?id=1" – George Johnston May 15 '13 at 15:07
  • Thanks @JonGrant...the problem is that my crypto.Decrypt method returns a string converted from ASCII...please the update 1 – Devester May 15 '13 at 15:11

2 Answers2

2

Using what George said in in his comment, you need to have a page or preferably a simple handler (.ashx) that will return the image in the response stream. You could then put that code you have in that handler, but instead of converting the byte array to an image, just write the bytes to the response stream.

Then, in your .aspx page, set the URL for the image to call that handler.

Example: <img src="~/GetImage.ashx?ImageID=1" />

GetImage.ashx is the handler that will return the image bytes.

DCNYAM
  • 11,966
  • 8
  • 53
  • 70
  • Would be possible to pass dynamically the bytes to that handler? The image to be displayed will be the user according the GUID selected... – Devester May 15 '13 at 15:26
  • @it_farway Based on your code, do you mean pass the encrypted image to the handler and return the decrypted image? If so, yes, this is possible, but I would avoid it if possible. Passing the entire image could lead to performance problems. If the image is stored in a database, pass a key to help you retrieve the image. If the image is associated with a GUID, pass the GUID as the parameter to the handler. – DCNYAM May 15 '13 at 17:29
0

You can directly embed your image data into the html using:

<img src="data:image/gif;base64,RAAA...more data.....">

see that link if you're interested in doing that: Html - embed image directly in html (old school style)

But I think using a handler is better since I'm not sure every browser accepts that kind of things.

Community
  • 1
  • 1
ppetrov
  • 3,077
  • 2
  • 15
  • 27