1.图片与二进制互相转换
protected void Page_Load(object sender, EventArgs e) { //图片转二进制流 string filepath = @"D:\netProjectTest\图片二进制互转\图片二进制互转\img\1.jpg"; string filepath1 = @"D:\netProjectTest\图片二进制互转\图片二进制互转\ss\1.jpg"; Bitmap bm = new Bitmap(System.Drawing.Image.FromFile(filepath)); MemoryStream ms = new MemoryStream(); bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); ms.Flush(); byte[] by = ms.ToArray(); StringBuilder sb = new StringBuilder(); foreach (var item in by) { sb.Append(item.ToString()); } Response.Write(sb); //二进制流 转换为图片 Bitmap bt1 = new Bitmap(new MemoryStream(by, 0, by.Length, true)); bt1.Save(filepath1, System.Drawing.Imaging.ImageFormat.Jpeg); }2.图片与base64互相转换
//图片转base64编码 string filepath = @"D:\netProjectTest\图片二进制互转\图片二进制互转\img\1.jpg"; string filepath1 = @"D:\netProjectTest\图片二进制互转\图片二进制互转\bb\1.jpg"; Bitmap bt = new Bitmap(filepath); String strbaser64 = String.Empty; MemoryStream ms = new MemoryStream(); bt.Save(ms, bt.RawFormat); byte[] bytes = ms.ToArray(); ms.Close(); strbaser64 = Convert.ToBase64String(bytes); string s = strbaser64; //base64转图片 byte[] base64Arr=Convert.FromBase64String(s); MemoryStream ms1= new MemoryStream(base64Arr); Bitmap bm1 = new Bitmap(ms1); ms1.Close(); bm1.Save(filepath1);