<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:label ID="Label1" runat="server" text="Label"></asp:label> <asp:Image ID="Image1" runat="server" /> </div> </form> </body> </html>
protected void Page_Load(object sender, EventArgs e) { /*******************将图片生成二进制流并转换成string************************/ string imgurl = @"D:\MyDownload\3N7781615TX5.png"; byte[] bytPic = GetPictureData(imgurl); string filetext = Convert.ToBase64String(bytPic); Label1.Text = filetext; /***********************图片转换成二进制后再显示图片**********************************/ byte[] toimg = Convert.FromBase64String(filetext); //图片路径 string strPath = DateTime.Now.ToString("yyyyMMddhhmmss")+".jpg"; string strPhotoPath = Server.MapPath(strPath); //保存图片文件 BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate)); bw.Write(toimg); bw.Close(); // 显示图片 this.Image1.ImageUrl = strPath; } public static byte[] GetPictureData(string imagepath) { ////根据图片文件的路径使用文件流打开,并保存为byte[] FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法 byte[] byData = new byte[fs.Length]; fs.Read(byData, 0, byData.Length); fs.Close(); return byData; }