C# 将图像绑定到图像控件
我想转换byte[]数组中的图像,然后再次将byte[]数组转换为图像,并用图像控件绑定该图像。请检查以下代码:C# 将图像绑定到图像控件,c#,image,C#,Image,我想转换byte[]数组中的图像,然后再次将byte[]数组转换为图像,并用图像控件绑定该图像。请检查以下代码: private void ShowImage() { var img = new System.Drawing.Bitmap(@"C:\Users\User\Desktop\Section-13.png"); byte[] image = imageToByteArray(img); Imag
private void ShowImage()
{
var img = new System.Drawing.Bitmap(@"C:\Users\User\Desktop\Section-13.png");
byte[] image = imageToByteArray(img);
Image image1 = byteArrayToImage(image);
Image2.ImageUrl = image1.ToString();
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
图像控制:
<asp:Image ID="Image2" runat="server" />
正如您在上面的代码中所看到的,我通过将图像转换为字符串来传递Image2.ImageUrl中的图像。我知道我做错了。请建议我是否在正确的轨道上
我目不转睛地想在GridView中绑定图像,但上面的代码只是为了我所知,我的目标只是简单地获取图像。提前谢谢 最简单的解决方案是将此
Section-13.png
复制到web应用程序的文件夹结构中,然后将ImageUrl
属性指向它:
Image2.ImageUrl = "~/images/Section-13.png";
另一方面,如果您希望从web应用程序之外的某个位置引用图像,则有两种可能性:
public class MyImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/png";
context.Response.WriteFile(@"C:\Users\User\Desktop\Section-13.png");
}
public bool IsReusable
{
get { return true; }
}
}
然后:
Image2.ImageUrl = "~/MyImageHandler.ashx";
“alt=”C:\Users\User\Desktop\Section-13.png“/>
创建http处理程序并将处理程序传递给img src标记
<img src="data:image/png;base64,<%= Convert.ToBase64String(System.IO.File.ReadAllBytes(@"")) %>" alt="C:\Users\User\Desktop\Section-13.png" />
公共类ImageHandler:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
字节[]缓冲区=imageToByteArray();
context.Response.OutputStream.Write(buffer,0,buffer.Length);
context.Response.ContentType=“image/JPEG”;
}
}
试着使
<img src="/imagehandler.ashx" />
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
byte[] buffer = imageToByteArray();
context.Response.OutputStream.Write(buffer , 0, buffer .Length);
context.Response.ContentType = "image/JPEG";
}
}
图像到字节数组
Image2.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(imageToByteArray(image1))
我还将使用此图像处理程序来处理此图像
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
我通过Context.Session传递id,并调用img.ashx-->我的处理程序
我做了什么
Context.Session["emp"] = "anything u could pass your id or something";
Image1.ImageUrl = "~/img.ashx";
感谢您的回复,但这不是我的目标,根据我的要求,我正在获取字节[]中的图像数组和我必须将数组转换回图像,然后与图像控件绑定。很抱歉,这是不可能的。您可以在Winforms或WPF应用程序中执行此操作,但不能在web应用程序中执行。实现此操作的唯一方法是使用数据URI方案,但请记住,并非所有浏览器都支持此方案。我已更新了我的答案,以说明exa嵌入到
标记中的数据URI方案的示例。为什么要将整个图像加载到内存中?如果图像非常大,并且您有很多请求,该怎么办?使用这种方法可能会很快耗尽内存。有关实现此处理程序的正确方法,请参阅我的答案。@DarinDimitrov:同意我的方法会影响性能。但是如果在这种情况下,图像在filsytem上不可用否,我的意思是,在ProcessRequest中,您不应该使用imageToByteArray();
。再次检查我的通用处理程序,以获得正确的实现方法。请注意,在我的示例中,图像不在网站的文件夹结构中(可能性1)。请注意使用了WriteFile
方法,该方法可以将图像以块的形式直接流式传输到响应,而无需像示例中那样将其完全加载到缓冲区中。image1.ToString()是将图像转换为字符串数据,而不是存储url,因此我认为您应该首先将图像存储到服务器路径,然后是Image2.ImageUrl=imagepath。您的意思是说,我首先将图像存储在本地的任何临时位置,然后将该url传递给图像控件。对吗?是的,我认为这是最简单的方法,尽管其他人可能会这么说可能会有帮助,但我觉得太复杂了。
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
Context.Session["emp"] = "anything u could pass your id or something";
Image1.ImageUrl = "~/img.ashx";
public class img : IHttpHandler,System.Web.SessionState.IReadOnlySessionState
{
Connection connect = new Connection();
public void ProcessRequest(HttpContext context)
{
if (context.Session["emp"].ToString() != null)
{
try
{
string text;
text =retrive your data from here by using context.Session["emp"]
DataTable dt1 = connect.exexc(text, connect.connectfunction());
if (dt1.Rows.Count > 0)
{
string img = dt1.Rows[0].ItemArray[0].ToString();--> i retrived the byte array here
byte[] imageBytes = Convert.FromBase64String(img);
context.Response.ContentType = "image/JPEG";
context.Response.BinaryWrite(imageBytes);
}
}
catch
{
}
}
}
public bool IsReusable
{
get
{
return false;
}
}