我想转换byte []数组中的图像,然后再将byte []数组转换为图像并将该图像与图像控件绑定。请检查以下代码:
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中的图像转换为字符串。我知道我做错了。请告知我是否走在正确的轨道上。
我goggle并发现在GridView中绑定图像,但上面的代码仅仅是为了我的知识我的目标只是简单地获取图像。提前致谢。
答案 0 :(得分:0)
最简单的解决方案是简单地将此Section-13.png
复制到Web应用程序的文件夹结构中,然后将ImageUrl
属性指向它:
Image2.ImageUrl = "~/images/Section-13.png";
另一方面,如果您想从Web应用程序之外的某个位置引用图像,那么您有两种可能性:
编写一个通用处理程序,它将图像流式传输到响应,然后将ImageUrl属性指向此通用处理程序:
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";
使用Data URI scheme
嵌入图像。在这种情况下,您可以直接使用<img>
HTML标记:
<img src="data:image/png;base64,<%= Convert.ToBase64String(System.IO.File.ReadAllBytes(@"")) %>" alt="C:\Users\User\Desktop\Section-13.png" />
答案 1 :(得分:0)
创建一个http处理程序并将处理程序传递给img src标记
<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";
}
}
答案 2 :(得分:0)
尝试制作 Data URL Scheme
data:[<MIME-type>][;charset=<encoding>][;base64],<data>
在图像控制中
<asp:image id="reddot" runat="server" ImageUrl="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" />
通常我们用于图像大小调整和动态图像的方法
http://i-skool.co.uk/net/resize-and-optimise-image-class-in-c/
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();
}
答案 3 :(得分:0)
我也会选择此图像处理程序来处理此图像。我使用此示例代码在我的webform中使用.ashx处理程序
检索图像 Context.Session["emp"] = "anything u could pass your id or something";
Image1.ImageUrl = "~/img.ashx";
我通过Context.Session传递了id并调用了img.ashx - &gt;我的经纪人
内部处理程序我做了
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;
}
}