Loading

图片转base64字符串,base64字符串转图片

        private void button1_Click(object sender, EventArgs e)
        {
            string base64String = ImageToBase64("D:\\desktop\\000.png");
            // pictureBox2为图片容器
            byte[] bytes = Convert.FromBase64String(base64String); // 将base64文本转换为字节数组
            pictureBox1.Image = Image.FromStream(new MemoryStream(bytes)); // 将字节数组转为流再转为图片并显示

        }


        public string ImageToBase64(string imagePath)
        {
            using (Image image = Image.FromFile(imagePath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    image.Save(ms, image.RawFormat);
                    byte[] imageBytes = ms.ToArray();
                    return Convert.ToBase64String(imageBytes);
                }
            }
        }

 

posted @ 2024-08-14 16:22  码农加一  阅读(15)  评论(0编辑  收藏  举报