将字节转换为图像ASP.NET c#,并在Image1.Url中使用它 [英] Convert Bytes to Image ASP.NET c# and use it in Image1.Url

查看:83
本文介绍了将字节转换为图像ASP.NET c#,并在Image1.Url中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WEB-APP,它是一个Web-Cam App,它可以将图像并以字节为单位存储到数据库中,现在我也不想保存所拍摄的图像并将其保存在任何文件中现在是一种文件夹,它是显示捕获的图像的唯一方法,供我保存并再次查看,以确保单击了捕获图像后会触发输入流。

I have a WEB-APP that is a Web-Cam App that takes images and stores into a database as bytes, Now with that being said I also don't want to save the images those are taken and save it in any kind of folder right now the only way to show the image that is captured for me to save it and view it again to do that I have a input stream that's fired when the capture image is clicked.

using (StreamReader reader = new StreamReader(Request.InputStream))
{
    hexString = Server.UrlEncode(reader.ReadLine());
    string imageName = DateTime.Now.ToString("dd-MM-yy hh-mm-ss");
    string imagePath = string.Format("~/Pictures/{0}.png", imageName);
    File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));
    Session["Byte"] = hexString;
    //   Session["CapturedImage"] = ResolveUrl(imagePath);
    Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(ConvertHexToBytes(hexString));

}

我有一种将十六进制字符串转换为字节的方法:

I have a method that converts that hex string to bytes:

private static byte[] ConvertHexToBytes(string hex)
{
    // MemoryStream stream = new MemoryStream();
    byte[] bytes = new byte[hex.Length / 2];
    for (int i = 0; i < hex.Length; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    }
    return bytes;
}

我想使用字节显示该图像,但我不想将图像保存在任何文件夹中。如何获取这些字节并将其放入图像中?

I want to Display that image using the bytes, I don't want to save the image in any folder. How do I take those bytes and put them into a image?

我有一个污损的图像标签Image1.imageUrl =?我尝试了base64版本,但是无法正常工作。

I have a slandered image tag Image1.imageUrl =? I tried the base64 version but it doesn't work.

推荐答案


如何获取这些字节并将它们放入图像中?

How do I take those bytes and put them into a image?

注意:我根据您的问题做出的假设是,您的问题与将十六进制转换为字节。

想象一下,您在 aspx 页面中有此内容:

Imagine you have this in your aspx page:

<asp:Image ID="Image1" runat="server" />

GetImageBytes 下面的代码中返回 byte [] 。然后,要提供图像(不将其保存到文件中),我们要做的就是:

In the code below GetImageBytes returns a byte[]. Then, to serve the image (without saving it to a file), all we need to do is this:

using (MemoryStream ms = new MemoryStream(GetImageBytes()))
{
    // Image1 is instance of System.Web.UI.WebControls
    this.Image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
}

private byte[] GetImageBytes()
{
    return System.IO.File.ReadAllBytes(Server.MapPath("~/Content/someImage.jpg"));
}

通过在 Content 文件夹进行测试。现在,您知道它可以工作了,您需要确保它可以与您的 ConvertHexToBytes 方法一起工作。如果没有,则显然 ConvertHexToBytes 方法出了问题。

Try that out by placing an image in your Content folder to test it out. Now that you know it works, you need to make sure it can work with your ConvertHexToBytes method. If it does not, then clearly something is wrong with ConvertHexToBytes method.

这篇关于将字节转换为图像ASP.NET c#,并在Image1.Url中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆