欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)的圖片、string相互轉(zhuǎn)換類分享

 更新時間:2015年03月27日 11:45:54   投稿:junjie  
這篇文章主要介紹了C#實現(xiàn)的圖片、string相互轉(zhuǎn)換類分享,本文直接給出類代碼,包含相互轉(zhuǎn)換的方法,需要的朋友可以參考下

C#中,Image為源自 Bitmap 和 Metafile 的類提供功能的抽象基類,也就是說更通用,當我們用Image.FromFile("xxx")時創(chuàng)建出來的是Image的某個派生類實體,所以我用Image作為參數(shù),而不是Bitmap之類的。

圖片在于string轉(zhuǎn)換的時候中間借助于MemorySteam和Byte數(shù)組,下面是我寫的FormatChange類,里面兩個互相轉(zhuǎn)換的過程。當然這里面也就包含了圖片與Byte[]數(shù)組的相互轉(zhuǎn)換嘍。

class FormatChange
  {
    public static string ChangeImageToString(Image image)
    {
      try
      {
        MemoryStream ms = new MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        byte[] arr = new byte[ms.Length];
        ms.Position = 0;
        ms.Read(arr, 0, (int)ms.Length);
        ms.Close();
        string pic = Convert.ToBase64String(arr);

        return pic;
      }
      catch (Exception)
      {
        return "Fail to change bitmap to string!";
      }
    }

    public static Image ChangeStringToImage(string pic)
    {
      try
      {
        byte[] imageBytes = Convert.FromBase64String(pic);
        //讀入MemoryStream對象
        MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
        memoryStream.Write(imageBytes, 0, imageBytes.Length);
        //轉(zhuǎn)成圖片
        Image image = Image.FromStream(memoryStream);

        return image;
      }
      catch (Exception)
      {
        Image image = null;
        return image;
      }
    }
  }

相關(guān)文章

  • C# WinForm中禁止改變窗口大小的方法

    C# WinForm中禁止改變窗口大小的方法

    這篇文章主要介紹了C# WinForm中禁止改變窗口大小的方法,需要把FormBorderStyle和MaximizeBox的值固定即可,需要的朋友可以參考下
    2014-08-08
  • C#實現(xiàn)異步連接Sql Server數(shù)據(jù)庫的方法

    C#實現(xiàn)異步連接Sql Server數(shù)據(jù)庫的方法

    這篇文章主要介紹了C#實現(xiàn)異步連接Sql Server數(shù)據(jù)庫的方法,涉及C#中await方法的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • 最新評論