sun
sun

Reputation: 85

How to load image from binary data in Asp.Net?

I am a beginner in .net Technology. I am using VS2008, C# ,Asp.Net 3.5 Framework, SQL SERVER 2005. In a database table 'Cust_M_Tbl', there is a field of varchar(500) type viz Cust_Image. The value in the Cust_Image is

mspZVnmQlz1GgRRpQEqBFGTHeUELiUhxQQ2GQU9BF3DCUYEeaiJJAQQLKGCBDYcySMENDi9qgQWJv0xBEe8sWkEEDr19QQWMxVoBE20odAEGFDtZAQeBtUtBDn7NUkEd0ytIAQl/r4WBBooWTAEHiCSGAQiMyjEBCFG+KYELYSoowQzluisBBt1NTwEYSM4hgQ5LTTpBD0e5KUEGYa0ugQxqoCLBCWgZKcEKcJJZQQ2DM1nBBAszVoEHiQMSZGhtcwEGCgwPExQUExMUFAMSYmVqcHUECQsOEBMTEhITFAISZWVobnUDCA0PERMVFRQUFRUDEWBjaG5yAQYLDg8REREREQISamlqb3UFCQ0PEhMUFRUWFhYDEV9hZmxxdQMJDQ4PEBEREQIRa2xucXcGCg0PEhMVFhgZGQQRY2ZpbXIBBgsNDxASExQDEHJzdgMHDA0PEBMVFxgZBBFiY2ZpbnMDCQsNDxIVFwMPc3YDBgkNDQ4PExQWGAQRXl9hY2ZqcwQIDBATFxgEDgQHCgwPDQ8PEhMVAxFXWFlcXV9hbXYFCxIWGRoGDA0ODwsODxMDEFVXV1dVVltkcwYOFhodAAD/Aw1PUVFQTk5RV2YKEQAA/wQMR0VFR0ZJQz0xAAD/BQdCQkMY8B0ZtKlQFCF/MssEMp7YkXe5scQP8fmd96ZVrvO8oGFXhoDAjEe5o+U/XAnxKOTp9vDgoSTOH22Eg2rytkcs9uqvFV7GSeUaetGWD0jVWeSqCuD6Sb6l/KxsWXbH1iDoY8LJhgKhkvVBei3Xmp4gx74bl58QiXckdX0KgxJhDWSa/zDvZvGfSVKVLvXzhv8/A+3tV1M36hSdkpPukozfqJj4O9ELUHNNUj8SRvFr0do7bU6tXqEbVubYYiVnalpHbCb07QoVPsO402Lwu3d9vnk6+bnZ/zbgpmAm4zfCLQrlOseeQ4XOarfqeCA14qS2EWZxATfilss++PYY+xymdxgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGA==

I want to display this customer image in my web page. How can I do it? Should I decrypt it? or it is in any other format? Any help will be appreciated. Regards,

Upvotes: 2

Views: 8986

Answers (2)

Christophe Geers
Christophe Geers

Reputation: 8972

This looks like Base64 encoding. You can find an online decoder here:

http://en.wikipedia.org/wiki/Base64

It validates your input as a valid Base-64 char array.

You can decode a base64 string in C# in the following way:

string encodedString = "your image data encoded as base 64 char array";
byte[] data = Convert.FromBase64String(encodedString);

Take a look at the FromBase64String article on MSDN for more information.

Now you want to display the image on an ASP.NET web page (*.ASPX).

E.g.:

<img src="myimage.jpg" />

Instead of referencing an actual image file (eg: myimage.jpg), you want to reference an ASP.NET handler (*.ASHX) that serves the bytes of the image (the byte[] array named data in the previous code sample).

E.g.:

<img src="ImageHandler.ashx" />

The code for the image handler looks something like this:

public class ImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // Load the image (see previous code sample)
        byte[] data = ...;

        // Display the image
        context.Response.OutputStream.Write(data, 0, data.Length);
        context.Response.ContentType = "image/JPEG";
    }
}

Read more about implementing an IHttpHandler on MSDN.

You need to pass an identifier to the imagehandler.ashx page so that you know which image to retrieved.

E.g.:

<img src="ImageHandler.ashx?id=<%=id%>" />

Put this instead of your img-tag or your ASP.NET image control.

Upvotes: 1

A.B.Cade
A.B.Cade

Reputation: 16915

After decoding as @Christophe Geers suggested use

string encodedString = "your image data encoded as base 64 char array";
byte[] data = Convert.FromBase64String(encodedString);

Response.BinaryWrite(data);

maybe this can help more: http://odetocode.com/articles/172.aspx

Upvotes: 1

Related Questions