代码改变世界

C# Base64加解密图片

2010-03-16 17:09  calm_水手  阅读(720)  评论(1编辑  收藏  举报
代码
 1  // 读取图片信息并用pictureBox显示
 2                 byte[] imageBytes=Convert.FromBase64String(str_picture);
 3                 MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
 4                 memoryStream.Write(imageBytes, 0, imageBytes.Length);
 5                 Image image = Image.FromStream(memoryStream);
 6                 memoryStream.Close();
 7 
 8                 // 将图片放置在 PictureBox 中
 9                 this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
10                 this.pictureBox1.Image = image;
11 
12 //将选中图片进行Base64加密
13                 OpenFileDialog openfile = new OpenFileDialog();
14                 openfile.Title = "请为商品选择相应的图片";
15                 openfile.Filter = "商品图片                      (*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
16                 if (DialogResult.OK == openfile.ShowDialog())
17                 {
18                    try
19                   {
20                     Bitmap bmp = new Bitmap(openfile.FileName);
21                     pictureBox1.Image = bmp;
22                     pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
23                     MemoryStream ms = new MemoryStream();
24                     bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
25                     byte[] arr = new byte[ms.Length];
26                     ms.Position = 0;
27                     ms.Read(arr, 0, (int)ms.Length);
28                     ms.Close();
29                     str_picture = Convert.ToBase64String(arr);
30                    }
31                   catch { }
32                    }