毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> net技术 >> 正文

C#如何将图片转换为字符串再转回来

更新时间:2014-11-14:  来源:毕业论文

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);
            }
        }
    }
}

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。