c#中檔案與二進位制流檔案的轉換
c# SQL資料庫image圖片檔案的保存於讀取
SqlConnection con = new SqlConnection("Persist Security Info=False;User id=sa;pwd=123;database=WDERPDATA_001;server=(local)");
con.Open();
SqlDataAdapter dat = new SqlDataAdapter("select*from TLFGL_LFRY", con);
DataSet set = new DataSet();
dat.Fill(set);
DataTable tb = set.Tables[0];
DataRow rw = tb.Rows[0];
DataColumn cl = tb.Columns[0];
//MessageBox.Show( cl.ColumnName.ToString());
byte[] MyData = new byte[0];
// MyData=tb.Rows[1]["AU_LFRY_ZJZ"];
System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
Image _Image = Image.FromStream(new MemoryStream((byte[])tb.Rows[0]["AU_LFRY_ZJZ"]) );
pictureBox1.Image = _Image;
Bitmap bmmpp = new Bitmap(pictureBox1.Image);
MemoryStream MS = new MemoryStream();
//Bitmap bmp = new Bitmap(VisitorCheckIn.Class1.bmpname);
bmmpp.Save(MS, ImageFormat.Bmp);
Byte[] ImageData = new Byte[MS.Length];
MS.Position = 0;
MS.Read(ImageData, 0, Convert.ToInt32(MS.Length));
string sql00 = "UPDATE TLFGL_LFRY SET
SqlCommand dbCmd = new SqlCommand(sql00, con);
dbCmd.CommandType = CommandType.Text;
SqlParameter parameter = new SqlParameter("@Photo", SqlDbType.Image, int.MaxValue);
parameter.Value = ImageData;
dbCmd.Parameters.Add(parameter);
dbCmd.ExecuteNonQuery();
將檔案轉換成二進位制方法:
/// <summary>
/// 將檔案轉換成二進位制
/// </summary>
/// <param name="Path">檔案路徑</param>
/// <returns></returns>
public static byte[] ConvertToBinary(string Path)
{
FileStream stream = new FileInfo(Path).OpenRead();
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
return buffer;
}
將二進位制轉換成檔案方法:
using System.IO;
string filePath = "D://a.jpg"; //檔案路徑
byte[] br = ConvertToBinary(filePath);
FileStream fstream = File.Create(filePath, br.Length);
try
{
fstream.Write(br, 0, br.Length); //二進位制轉換成檔案
}
catch(Exception ex)
{
//丟擲異常資訊
}
finally
{
fstream.Close();
}
附加:二進位制與圖片互轉
Image aa =new Bitmap(@"E:/photo/tm.jpg");
System.IO.MemoryStream stream =new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter
=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formatter.Serialize(stream, aa); //將影象序列化成二進位制流
stream.Position =0;
Image bb = (Image)formatter.Deserialize(stream); //將二進位制流序列成Image
相關推薦
c#中檔案與二進位制流檔案的轉換
c# SQL資料庫image圖片檔案的保存於讀取 SqlConnection con = new SqlConnection("Persist Security Info=False;User id=sa;pwd=123;database=WDERPD
C/C++中ASCII與Unicode字串相互轉換
轉載地址:https://blog.csdn.net/wbq2018/article/details/8806431 1、ASCII to Unicode 函式: wcstombs(VC6)、wcstombs_s 例項: //crt_wcstombs_s.c //This examp
【C++】C++中int與string的相互轉換
一、int轉string 1.c++11標準增加了全域性函式std::to_string: string to_string (int val); string to_string (long val); string to_string (long long val); string to_str
C++中漢字與unicode碼的轉換
已知一個漢字的unicode碼(例如-20319),可以通過以下方法得到該unicode碼對應的漢字: 1,宣告一個3位元組長的字元陣列,例如char a[3]; 2,對該陣列賦值為char a[3]={unsigned(-2
C/C++中字串與數字之間的轉換
#include "iostream" #include "string" #include "sstream" #include "cstdio" using namespace std; string num2str(double a) { stringstream ss;ss << a
C++中數字與字串之間的轉換(包括C++11新標準和寬窄字元轉換)
1、字串數字之間的轉換 (1)string --> char * string str("OK"); char * p = str.c_str(); (2)char * -->string char *p = "OK"; string str(p); (3)char *
java 中 十進位制與二進位制的互相轉換
/** * @Description: 十進位制轉換成二進位制 () * @param decimalSource * @return String */ public static String decimalToBinary(i
C++中數字與字串之間的轉換
字串數字之間的轉換 (1)string --> char * string str("OK"); char * p = str.c_str(); (2)char * -->string char *p = "OK";
C++中int與string的相互轉換
一、int轉string 1.c++11標準增加了全域性函式std::to_string: string to_string (int val); string to_string (long val); string to_string (long long val);
C 物件 檔案與二進位制串(byte陣列)之間的轉換
1.關於本文在使用C#下的TCP(類TcpClient)、UDP(類UdpClient)協議傳輸資訊時,都需要將資訊轉換為byte型別的陣列進行傳送。本文實現了兩種object與byte陣列的轉換和一種檔案與byte陣列轉換的方式。基礎型別的資料,可以用BitConv
將圖片(二進位制檔案)儲存於資料庫,論檔案位元組流與二進位制字串相互轉換
開發中遇到儲存圖片檔案於資料庫這樣的需求。我們知道檔案本身就是一份二進位制資料,不同型別的檔案只是編碼形式不同,對應的解讀形式不同,無論txt檔案、jpg檔案亦或是mp4檔案,本質上都是0和1組成的。而在C++中我們可以通過位元組流來讀寫檔案,也就是說我們可以把檔案讀入cha
淺談C語言中文字檔案與二進位制檔案
C語言中,按檔案中的資料組織形式來分,資料檔案可分為ASCII碼檔案(即文字檔案)和二進位制檔案。 文字檔案在磁碟中存放時每個字元對應一個位元組,用於存放對應的ASCII碼。 二進位制檔案把資料按其在記憶體中的儲存形式存放在磁碟上,一個位元組並不一定對應一個字元。 對於A
C#中結構體與位元組流互相轉換
C++的訊息結構體如下 struct cs_message{ u32_t cmd_type; char username[16]; u32_t dstID; u32_t
VS2008中自定義C++工程模板與修改新建檔案預設編碼的辦法
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
十二、C#入門基礎12(泛型,檔案與資料流)
一、泛型 什麼是泛型? 泛型是用於處理演算法,資料結構的一種程式設計方法。泛型的目標是採用廣 泛適用和可互動性的形式來表示演算法和資料結構,以似它們能夠直接用 於軟體構造。泛型類,結構,介面,和方法可以根據它們儲存和操作的 資料型別來進行引數化。 泛型能在編譯時提供強大的型別檢查,減少資
十二、C#入門基礎(泛型,檔案與資料流)
一、泛型 什麼是泛型? 泛型是用於處理演算法,資料結構的一種程式設計方法。泛型的目標是採用廣 泛適用和可互動性的形式來表示演算法和資料結構,以似它們能夠直接用 於軟體構造。泛型類,結構,介面,和方法可以根據它們儲存和操作的 資料型別來進行引數化。 泛型能在編譯時
檔案轉為二進位制流儲存在資料庫中的思路
資料庫在儲存資料時,有兩種格式,一種是以位元組儲存的,二進位制資料。 另一種是以字元形式儲存的。 資料庫要儲存二進位制資料,其資料庫的列的屬性必須是二進位制型別的,如MySQL中,型別為BINARY和VARBNARY的列以二進位制形式儲存資料。 因為二進位制一般
C# 檔案和位元組流的轉換
1、讀取檔案,並轉換為位元組流 FileStream fs = new FileStream(filename,FileMode.Open, FileAccess.Read); byte[] infbytes = new byte[(int)fs.Length]; fs.Re
C#中ArrayList 與 string、string[]數組 的轉換
list string ray AR nbsp 數組 arr spl oar 1、ArrarList 轉換為 string[] : ArrayList list = new ArrayList(); list.Add("aaa"); list
c++ 中 char 與 string 之間的相互轉換
第一部分: 將 char * 或者 char [] 轉換為 string 可以直接賦值,轉換。 第二部分: 將 &n