C# 在图像控件中绘制字节数组
是否可以将字节数组转换为图像并直接在图像控件中显示,而不将其保存在磁盘上 以下是我迄今为止开发的代码a:C# 在图像控件中绘制字节数组,c#,asp.net,C#,Asp.net,是否可以将字节数组转换为图像并直接在图像控件中显示,而不将其保存在磁盘上 以下是我迄今为止开发的代码a: protected void btnShow_Click(object sender, System.EventArgs e) { Byte[] blobEncryptedImage = Signature.Get(txSaleGUID.Text); Crypto crypto = new Crypto("mypass"); B
protected void btnShow_Click(object sender, System.EventArgs e)
{
Byte[] blobEncryptedImage = Signature.Get(txSaleGUID.Text);
Crypto crypto = new Crypto("mypass");
Byte[] decryptedImage = Encoding.ASCII.GetBytes(crypto.Decrypt(blobEncryptedImage));
MemoryStream ms = new MemoryStream(decryptedImage);
Image img = Image.FromStream(ms);
//Image1.ImageUrl = System.Drawing.Image.FromStream(ms);
}
public static Image byteArrayToImage(byte[] data)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
return new System.Drawing.Bitmap(Image.FromStream(ms));
}
}
更新1
这是我的加密类:
public class Crypto
{
private ICryptoTransform rijndaelDecryptor;
// Replace me with a 16-byte key, share between Java and C#
private static byte[] rawSecretKey = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
public Crypto(string passphrase)
{
byte[] passwordKey = encodeDigest(passphrase);
RijndaelManaged rijndael = new RijndaelManaged();
rijndaelDecryptor = rijndael.CreateDecryptor(passwordKey, rawSecretKey);
}
public string Decrypt(byte[] encryptedData)
{
byte[] newClearData = rijndaelDecryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.ASCII.GetString(newClearData);
}
public string DecryptFromBase64(string encryptedBase64)
{
return Decrypt(Convert.FromBase64String(encryptedBase64));
}
private byte[] encodeDigest(string text)
{
MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] data = Encoding.ASCII.GetBytes(text);
return x.ComputeHash(data);
}
}
有人能提供线索吗?根据George在评论中所说的,您需要一个页面,或者最好是一个简单的处理程序(.ashx),它将在响应流中返回图像。然后,您可以将您拥有的代码放入该处理程序中,但不必将字节数组转换为图像,只需将字节写入响应流即可 然后,在.aspx页面中,设置要调用该处理程序的图像的URL 示例:
GetImage.ashx是返回图像字节的处理程序。您可以使用以下命令直接将图像数据嵌入html:
<img src="data:image/gif;base64,RAAA...more data.....">
如果您对此感兴趣,请查看该链接:
但我认为使用处理程序更好,因为我不确定每个浏览器都接受这种类型的东西。为什么要使用
编码.ASCII.GetBytes
?删除它,它是二进制数据,而不是ASCII(文本)。您需要将图像字节写入输出流,该输出流可以由URL单独调用。e、 g src=“”谢谢@JonGrant…问题是我的crypto.Decrypt方法返回一个从ASCII转换的字符串…请更新1是否可以将字节动态传递给该处理程序?要显示的图像将是用户根据所选GUID…@it\u farway根据您的代码,您的意思是将加密图像传递给处理程序并返回解密图像吗?如果是这样,是的,这是可能的,但如果可能的话,我会避免。传递整个映像可能会导致性能问题。如果图像存储在数据库中,请传递一个键以帮助检索图像。如果映像与GUID关联,请将GUID作为参数传递给处理程序。