将二进制值转换为位图图像

时间:2012-01-11 13:58:29

标签: asp.net bitmap

如何在ASP.NET C#

中将以下二进制值转换为位图图像
  

89504e470d0a1a0a0000000d4948445200000103000000cf0806000000f18cb4b00000000473424954080808087c086488000000097048597300000b1200000b1201d2dd7efc00000016744558744372656174696f6e2054696d650031322f32372f3131cce39cd90000002674455874536f667477617265005245534f5552434553204e4f54205553454420464f5220454e47494e45f6c2e0720000200049444154789cec9d779c5d5775efbffbf473fb748d7aefdd922c775bd84e6c638a1d20b4600831844e48422f2f24218440c80b2fe5250412420fa1d9e082255bb66cc956b7ba2c8d6634bddeb9fdb4bddf1fe76a2cd932312f8065657e9fcffd7c

类似问题的答案都没有帮助我。有人请帮忙!

1 个答案:

答案 0 :(得分:4)

看起来像PNG图像(它只是100x100透明像素图像)。您可以使用following function将此HEX字符串转换为字节数组并将其保存到文件中:

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();
    }
}

如果要在ASP.NET中动态显示它,可以编写一个通用处理程序来提供此图像:

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; }
    }
}

并且在某个要显示它的网页表单中,您可以使用指向此处理程序的图像控件:

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

或简单的静态<img>标记,如果您愿意:

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

另一种可能性是使用Data URI scheme(假设您的客户端浏览器支持它):

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

可以在您的代码中定义MyImageData函数:

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();
    }
}
相关问题