最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

圖片在于string轉(zhuǎn)換的時候中間借助于MemorySteam和Byte數(shù)組,下面是我寫的FormatChange類,里面兩個互相轉(zhuǎn)換的過程。當(dāng)然這里面也就包含了圖片與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)文章

最新評論

商丘市| 云林县| 德阳市| 临澧县| 临高县| 文水县| 涿鹿县| 阿巴嘎旗| 中宁县| 乌鲁木齐县| 韩城市| 静乐县| 巩留县| 古丈县| 鹰潭市| 霸州市| 南郑县| 烟台市| 乳源| 五大连池市| 泰和县| 应用必备| 宁强县| 海宁市| 葵青区| 汝州市| 永修县| 盱眙县| 富顺县| 崇礼县| 南召县| 上饶县| 昌江| 泽库县| 铜陵市| 石景山区| 自贡市| 兴海县| 临猗县| 旌德县| 宣恩县|