C#如何实现将图像保存到内存,再读取?image.saveas();怎么写啊?
还有读取的时候怎么弄?

解决方案 »

  1.   

    Image的Save方法有重载,可以保存到流,加载可以            Image img = Image.FromStream(stream);
      

  2.   

    释放流:ns.Close()//不然其他用户无法处理当前图片
      

  3.   


    要是用 MemoryStream 整个过程应该是怎样的?刚接触,能否实例分析一下啊?谢谢了
      

  4.   

    理解MemoryStream ,byte[],Image 
      

  5.   

    截取当前窗体工作区图像,保存至内存//截取当前窗体图像
    Bitmap bitFormImg = new Bitmap(this.Width, this.Height);
    this.DrawToBitmap(bitFormImg, new Rectangle(0, 0, this.Width, this.Height));
    int border = (this.Width - this.ClientSize.Width) / 2;//边框宽度
    int caption = (this.Height - this.ClientSize.Height) - border;//标题栏高度//截取签字区域图像(去边框、标题栏)
    Bitmap bitSignatureImg = bitFormImg.Clone(new Rectangle(border, caption, this.ClientSize.Width, this.ClientSize.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    //创建内存流
    MemoryStream Ms = new MemoryStream();
    //将图像保存至内存流
    bitSignatureImg.Save(Ms, ImageFormat.Bmp);
    bitFormImg.Dispose();
    bitSignatureImg.Dispose();byte[] img = new byte[Ms.Length];
    Ms.Position = 0;
    Ms.Read(img, 0, Convert.ToInt32(Ms.Length));
    Ms.Close();
    string result = Convert.ToBase64String(img);
    读取内存中图像/// <summary>
    /// 将By数组转为Image数据
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public Image BytesToImage(byte[] bytes)
    {
     MemoryStream ms = new MemoryStream(bytes);
     Image img = Image.FromStream(ms);
     //图片加文字水印
     //ImageWaterMark(img);
     return img;
    }
    显示在pictureBox中
    string image = 图片二进制数据字符串。;//第一个方法这里已经把图片二进制数据存储为字符串了。
    //string result = Convert.ToBase64String(img);byte[] by = Convert.FromBase64String(image);
    //绑定图片
    this.pictureBox1.Image = BytesToImage(by);