2

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,

2
  • What have you tried? By the way you shouldn't post customers image source it can hold personal information. Commented Dec 12, 2011 at 7:44
  • @Reniuz....its an edited one just used for example Commented Dec 12, 2011 at 7:48

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

10 Comments

though it is a first step it does not really explain how to display the image
@Christophe Geers....I got this error- Error 2 The name 'Encoding' does not exist in the current context. How can I load this data to my img control?
Are you sure that you really need to hold the byte[] in a string ? why ?
@A.B.Cade...I am not so particular of that...i just need to display the image As Cristophe suggested I used the following code- string encodedString = c.dr.GetValue(0).ToString(); byte[] data = Convert.FromBase64String(encodedString); string decodedString = Encoding.UTF8.GetString(data);
@Christophe Geers....I got the decodedString.Thanks Cristophe....But how can I connect it with my image control
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.