Stream st = new MemoryStream();
Image img = c.GetType().GetProperty("Image").GetValue(c, null) as Image;
img.Save(st, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] by = new byte[st.Length];
st.Read(by, 0, by.Length);
string text = Convert.ToBase64String(by);
//保存字符串
//..........................................
by = null;
img.Dispose();
st.Dispose();
C# code
string text = 读取字符串;
byte[] by = Convert.FromBase64String(text);
Stream ms = new MemoryStream();
ms.Write(by, 0, by.Length);
Image bi = Image.FromStream(ms);
pic.Image = bi;
bi.Dispose();
ms.Dispose();
by = null;
然后在Image bi = Image.FromStream(ms);这里的时候报错了,说是无效的参数,求指点!
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace SVG4Net
{
public class ImageConvert
{
/// <summary>
/// 将图片转换为Base64格式字符串
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
public static string ToBase64String(Image img)
{
return ToBase64String(img, ImageFormat.Png);
}
public static string ToBase64String(Image img,ImageFormat format)
{
if (img != null)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, format);
byte[] buffer = ms.ToArray();
return Convert.ToBase64String(buffer);
}
}
return string.Empty;
}
public static string ToBase64HtmlString(Image img)
{
return ToBase64HtmlString(img, ImageFormat.Png);
}
public static string ToBase64HtmlString(Image img, ImageFormat format)
{
string type = "jpeg";
if (format.Guid == ImageFormat.Png.Guid)
{
type = "png";
}
else if (format.Guid == ImageFormat.Gif.Guid)
{
type = "gif";
}
return string.Format("data:image/{0};base64,", type) + ToBase64String(img, format);
}
public static Image FromBase64String(string base64Str)
{
Bitmap bitmap = null;
Image img = null;
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = Convert.FromBase64String(base64Str);
ms.Write(buffer, 0, buffer.Length);
try
{
img = Image.FromStream(ms);
if (img != null)
{
bitmap = new Bitmap(img.Width,img.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(img,new Rectangle(0,0,bitmap.Width,bitmap.Height));
}
}
}
catch { }
}
return bitmap;
}
public static Image FromBase64HtmlString(string str)
{
string[] strs = str.Split(',');
if (strs.Length > 0)
{
return FromBase64String(strs[strs.Length - 1]);
}
else
{
return FromBase64String(str);
}
}
}
}