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

C#byte數(shù)組與Image的相互轉(zhuǎn)換實例代碼

 更新時間:2017年04月23日 16:44:57   投稿:lqh  
這篇文章主要介紹了C#byte數(shù)組與Image的相互轉(zhuǎn)換實例代碼的相關(guān)資料,需要的朋友可以參考下

C#byte數(shù)組與Image的相互轉(zhuǎn)換實例代碼

功能需求:

1、把一張圖片(png bmp jpeg bmp gif)轉(zhuǎn)換為byte數(shù)組存放到數(shù)據(jù)庫。

2、把從數(shù)據(jù)庫讀取的byte數(shù)組轉(zhuǎn)換為Image對象,賦值給相應(yīng)的控件顯示。

3、從圖片byte數(shù)組得到對應(yīng)圖片的格式,生成一張圖片保存到磁盤上。

這里的Image是System.Drawing.Image。

  //Get an image from file
    Image image = Image.FromFile("D:\\test.jpg");
    Bitmap bitmap = new Bitmap("D:\\test.jpg");

以下三個函數(shù)分別實現(xiàn)了上述三個需求:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace NetUtilityLib
{
  public static class ImageHelper
  {
    /// <summary>
    /// Convert Image to Byte[]
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static byte[] ImageToBytes(Image image)
    {
      ImageFormat format = image.RawFormat;
      using (MemoryStream ms = new MemoryStream())
      {
        if (format.Equals(ImageFormat.Jpeg))
        {
          image.Save(ms, ImageFormat.Jpeg);
        }
        else if (format.Equals(ImageFormat.Png))
        {
          image.Save(ms, ImageFormat.Png);
        }
        else if (format.Equals(ImageFormat.Bmp))
        {
          image.Save(ms, ImageFormat.Bmp);
        }
        else if (format.Equals(ImageFormat.Gif))
        {
          image.Save(ms, ImageFormat.Gif);
        }
        else if (format.Equals(ImageFormat.Icon))
        {
          image.Save(ms, ImageFormat.Icon);
        }
        byte[] buffer = new byte[ms.Length];
        //Image.Save()會改變MemoryStream的Position,需要重新Seek到Begin
        ms.Seek(0, SeekOrigin.Begin);
        ms.Read(buffer, 0, buffer.Length);
        return buffer;
      }
    }

    /// <summary>
    /// Convert Byte[] to Image
    /// </summary>
    /// <param name="buffer"></param>
    /// <returns></returns>
    public static Image BytesToImage(byte[] buffer)
    {
      MemoryStream ms = new MemoryStream(buffer);
      Image image = System.Drawing.Image.FromStream(ms);
      return image;
    }

    /// <summary>
    /// Convert Byte[] to a picture and Store it in file
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="buffer"></param>
    /// <returns></returns>
    public static string CreateImageFromBytes(string fileName, byte[] buffer)
    {
      string file = fileName;
      Image image = BytesToImage(buffer);
      ImageFormat format = image.RawFormat;
      if (format.Equals(ImageFormat.Jpeg))
      {
        file += ".jpeg";
      }
      else if (format.Equals(ImageFormat.Png))
      {
        file += ".png";
      }
      else if (format.Equals(ImageFormat.Bmp))
      {
        file += ".bmp";
      }
      else if (format.Equals(ImageFormat.Gif))
      {
        file += ".gif";
      }
      else if (format.Equals(ImageFormat.Icon))
      {
        file += ".icon";
      }
      System.IO.FileInfo info = new System.IO.FileInfo(file);
      System.IO.Directory.CreateDirectory(info.Directory.FullName);
      File.WriteAllBytes(file, buffer);
      return file;
    }
  }
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • C#從foreach語句中枚舉元素看數(shù)組詳解

    C#從foreach語句中枚舉元素看數(shù)組詳解

    這篇文章主要給大家介紹了關(guān)于C#從foreach語句中枚舉元素看數(shù)組的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • C#中派生類調(diào)用基類構(gòu)造函數(shù)用法分析

    C#中派生類調(diào)用基類構(gòu)造函數(shù)用法分析

    這篇文章主要介紹了C#中派生類調(diào)用基類構(gòu)造函數(shù)用法,實例分析了派生類調(diào)用基類構(gòu)造函數(shù)的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#中多態(tài)性的實現(xiàn)

    C#中多態(tài)性的實現(xiàn)

    這篇文章主要介紹了C#中多態(tài)性的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Unity給物體添加多個Tag的實現(xiàn)

    Unity給物體添加多個Tag的實現(xiàn)

    這篇文章主要介紹了Unity給物體添加多個Tag的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • c#中task與thread區(qū)別及其使用的方法示例

    c#中task與thread區(qū)別及其使用的方法示例

    本文主要介紹了c#中task與thread區(qū)別及其使用的方法示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • c#中分割字符串的幾種方法

    c#中分割字符串的幾種方法

    c#中分割字符串的幾種方法...
    2007-04-04
  • C#連接數(shù)據(jù)庫的方法

    C#連接數(shù)據(jù)庫的方法

    ASP.NET連接數(shù)據(jù)庫的技術(shù)叫ADO.NET,它是用來向數(shù)據(jù)庫提交sql語句的一堆類。這里連接的是Sql Server 2008數(shù)據(jù)庫,其他數(shù)據(jù)庫用法差不多,就是調(diào)用的類名不一樣
    2015-11-11
  • 分享WCF聊天程序--WCFChat實現(xiàn)代碼

    分享WCF聊天程序--WCFChat實現(xiàn)代碼

    無意中在一個國外的站點下到了一個利用WCF實現(xiàn)聊天的程序,作者是:Nikola Paljetak。研究了一下,自己做了測試和部分修改,感覺還不錯,分享給大家
    2015-11-11
  • 詳解C# 中Session的用法

    詳解C# 中Session的用法

    這篇文章主要介紹了C# 中Session的用法,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#獲取ListView鼠標下的Item實例

    C#獲取ListView鼠標下的Item實例

    下面小編就為大家?guī)硪黄狢#獲取ListView鼠標下的Item實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論