解决了问题先不要着急上线,先在博客上记录下来

字节转bitmap ,bitmap转 64编码   前端src 把那串string对上就可以了

 

   public static string ImgToBase64String(Bitmap bmp)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static string Base64EncodeBytes( byte[] inBytes)
        {
            return Convert.ToBase64String(inBytes);
        }
        public static Bitmap BytesToBitmap(byte[] Bytes)
        {
            MemoryStream stream = null;
            try
            {
                //ZipHelper.BZipDeCompress(Bytes);
                stream = new MemoryStream(BZipDeCompress(Bytes));
                return new Bitmap((Image)new Bitmap(stream));
                //return new Bitmap(stream);
            }
            catch (Exception)
            {
                stream = new MemoryStream(Bytes);
                return new Bitmap((Image)new Bitmap(stream));
                //return new Bitmap(stream);
            } finally
            {
                stream.Close();
                stream.Dispose();
            }
        }
      public static byte[] BZipDeCompress(byte[] data, bool isClearData = false)
            {
            byte[] bytes = null;
            using (MemoryStream o= new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(data))
                {
                    using (Stream s = new BZip2InputStream(ms))
                    {
                        s.Flush();
                        int size = 0;
                        byte[] buffer = new byte[s.Length];
                        while ((size = s.Read(buffer, 0, buffer.Length))> 0)
                        {
                            o.Write(buffer, 0, size);
                        } 
                    }
                }
                bytes = o.ToArray();
            }
            if (isClearData)
                Array.Clear(data, 0, data.Length);
            return bytes;
        }
 <img src="data:image/png;base64,{{value.BWindowsImage}}"/>

调用

 data[i].BWindowsFSTImage = ImgToBase64String(BytesToBitmap(data[i].WindowsFSTImage));

试了直接从字节转64字符串不行.

posted on 2022-03-30 15:41  小石头的一天  阅读(162)  评论(0编辑  收藏  举报