0

How can I convert the following Binary value to Bitmap image in ASP.NET C#

89504e470d0a1a0a0000000d4948445200000103000000cf0806000000f18cb4b00000000473424954080808087c086488000000097048597300000b1200000b1201d2dd7efc00000016744558744372656174696f6e2054696d650031322f32372f3131cce39cd90000002674455874536f667477617265005245534f5552434553204e4f54205553454420464f5220454e47494e45f6c2e0720000200049444154789cec9d779c5d5775efbffbf473fb748d7aefdd922c775bd84e6c638a1d20b4600831844e48422f2f24218440c80b2fe5250412420fa1d9e082255bb66cc956b7ba2c8d6634bddeb9fdb4bddf1fe76a2cd932312f8065657e9fcffd7c

None of the similar question's answered helped me. Anybody please help!

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Karthik Malla
  • 5,570
  • 12
  • 46
  • 89

1 Answers1

4

Looks like a PNG image (it's a 100x100 transparent pixels only image). You could use the following function to convert this HEX string into a byte array and save it to a file:

class Program
{
    static void Main()
    {
        var data = "89504e470d0a1a0a0000000d4948445200000103000000cf0806000000f18cb4b00000000473424954080808087c086488000000097048597300000b1200000b1201d2dd7efc00000016744558744372656174696f6e2054696d650031322f32372f3131cce39cd90000002674455874536f667477617265005245534f5552434553204e4f54205553454420464f5220454e47494e45f6c2e0720000200049444154789cec9d779c5d5775efbffbf473fb748d7aefdd922c775bd84e6c638a1d20b4600831844e48422f2f24218440c80b2fe5250412420fa1d9e082255bb66cc956b7ba2c8d6634bddeb9fdb4bddf1fe76a2cd932312f8065657e9fcffd7c";
        var buffer = StringToByteArray(data);
        File.WriteAllBytes("test.png", buffer);
    }

    static byte[] StringToByteArray(string hex)
    {
        return Enumerable
            .Range(0, hex.Length)
            .Where(x => x % 2 == 0)
            .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
            .ToArray();
    }
}

If you want to display it dynamically in ASP.NET you could write a generic handler that will serve this image:

public class MyImage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/png";
        var data = "89504e470d0a1a0a0000000d4948445200000103000000cf0806000000f18cb4b00000000473424954080808087c086488000000097048597300000b1200000b1201d2dd7efc00000016744558744372656174696f6e2054696d650031322f32372f3131cce39cd90000002674455874536f667477617265005245534f5552434553204e4f54205553454420464f5220454e47494e45f6c2e0720000200049444154789cec9d779c5d5775efbffbf473fb748d7aefdd922c775bd84e6c638a1d20b4600831844e48422f2f24218440c80b2fe5250412420fa1d9e082255bb66cc956b7ba2c8d6634bddeb9fdb4bddf1fe76a2cd932312f8065657e9fcffd7c";
        var buffer = StringToByteArray(data);
        context.Response.OutputStream.Write(buffer, 0, buffer.Length);
    }

    private byte[] StringToByteArray(string hex)
    {
        return Enumerable
            .Range(0, hex.Length)
            .Where(x => x % 2 == 0)
            .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
            .ToArray();
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

and in some web form where you want to display it you could use an image control pointing to this handler:

<asp:Image ID="img" runat="server" ImageUrl="~/MyImage.ashx" />

or a simple static <img> tag if you prefer:

<img src="<%= ResolveUrl("~/MyImage.ashx") %>" alt="" />

Yet another possibility is to use the Data URI scheme (assuming your client browsers support it):

<img src="data:image/png;base64,<%= MyImageData() %>" alt="" />

where the MyImageData function could be defined in your code behind:

public partial class _Default : System.Web.UI.Page
{
    protected string MyImageData()
    {
        var data = "89504e470d0a1a0a0000000d4948445200000103000000cf0806000000f18cb4b00000000473424954080808087c086488000000097048597300000b1200000b1201d2dd7efc00000016744558744372656174696f6e2054696d650031322f32372f3131cce39cd90000002674455874536f667477617265005245534f5552434553204e4f54205553454420464f5220454e47494e45f6c2e0720000200049444154789cec9d779c5d5775efbffbf473fb748d7aefdd922c775bd84e6c638a1d20b4600831844e48422f2f24218440c80b2fe5250412420fa1d9e082255bb66cc956b7ba2c8d6634bddeb9fdb4bddf1fe76a2cd932312f8065657e9fcffd7c";
        var buffer = StringToByteArray(data);
        return Convert.ToBase64String(buffer);
    }

    private byte[] StringToByteArray(string hex)
    {
        return Enumerable
            .Range(0, hex.Length)
            .Where(x => x % 2 == 0)
            .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
            .ToArray();
    }
}
Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for your code. But I cannot see the output. How can I display that image using ASP:Image or HTML image input? – Karthik Malla Jan 11 '12 at 14:23
  • Alas! This is not displaying output at `` But the code runs without any errors. – Karthik Malla Jan 11 '12 at 14:28
  • @lock, what do you mean that it is not displaying output? You see a white image? If that's the case, this is normal. As I said the example data you have shown represents a 100x100 px transparent pixels PNG image. What output did you expect more precisely? Have you tried with an image that is not consisting of only transparent pixels? – Darin Dimitrov Jan 11 '12 at 14:31
  • Ya! Thanks. I executed that code at .aspx before so, I didn't get any output. Now, I tried to execute that code .ashx and got it. Thanks a lot Darin. – Karthik Malla Jan 11 '12 at 14:34
  • And how to find image is PNG/JPEG/BITMAP or other? so, that it can accept any file format. – Karthik Malla Jan 11 '12 at 14:36
  • @lock, you could attempt loading it into a [System.Drawing.Image](http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx) and then: http://stackoverflow.com/a/5209528/29407 – Darin Dimitrov Jan 11 '12 at 14:38
  • But Darin, I changed other picture and tried and whatever picture I upload I am getting output to white 100x100 – Karthik Malla Jan 11 '12 at 14:45
  • @lock, well maybe there is something on your server that transforms this picture. Though to say. Does your input string change? – Darin Dimitrov Jan 11 '12 at 14:46
  • I used ASP.NET fileupload function and uploaded an image to MySQL database (Binary field) as FileUpload1.FileBytes(); and a new record is inserted and this binary code is added there. What's wrong in my procedure? – Karthik Malla Jan 12 '12 at 14:42