C#でGUIDとImageタイプのデータメソッドを取得し、画像の保存、復元を行う
ここ数日ちょうどC#を学んで、データベースの中でGUIDを挿入することに出会って、IMAGEタイプのデータの問題、BAIDUは下を向いて、解決方法を探し当てました.
2.img
その他にも、画像ファイルをBase 64形式に変換する必要がある場合があります.
例:
1.GUID
:
string guid=System.Guid.NewGuid().ToString();// System.Guid.Empty.ToString();
//
SqlParameter fd = new SqlParameter("@ ", SqlDbType.UniqueIdentifier, 16);
fd.Value = new Guid(guid);
cmd.Parameters.Add(fd);
:
using (System.Data.IDataReader reader = cmd.ExecuteReader())
if (reader.Read()){
string id = reader[" "];
}
2.img
MemoryStream ms = new MemoryStream();
// !!
pictureBox1.Image.Save("a.bmp");
FileStream fileStream = new FileStream("a.bmp", FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
byte[] img = binaryReader.ReadBytes((int)fileStream.Length);
binaryReader.Close();
fileStream.Close();
img
その他にも、画像ファイルをBase 64形式に変換する必要がある場合があります.
例:
// pictureBox , ,
pictureBox1.Image.Save("a.gif");
FileStream fileStream1 = new FileStream("a.gif", FileMode.Open, FileAccess.Read);
byte[] inputBytes1 = new byte[fileStream1.Length];
fileStream1.Read(inputBytes1, 0, System.Convert.ToInt32(fileStream1.Length));
//
TestWork.File file1 = new TestWork.File();
// Guid
file1.FileMainID = guid;
//
file1.Data = inputBytes1;
//
file1.Length = inputBytes1.Length;
//
InsertNormalFileData(file1);
/// <summary>
///
/// </summary>
/// <param name="file"> </param>
/// <param name="databaseName"> </param>
/// <returns></returns>
private static bool InsertFileData(TestWork.File file, string databaseName)
{
//
SqlConnection con = null;
try
{
//
con = CreateDataBase(databaseName);
//
con.Open();
// converter
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
// SqlCommand
SqlCommand cmd = con.CreateCommand();
// ,
cmd.CommandText = "XXXXXXXX";
// :File_Main_ID
SqlParameter fileMainID = new SqlParameter("@FILE_MAIN_ID", SqlDbType.UniqueIdentifier, 16);
fileMainID.Value = new Guid(file.FileMainID);
cmd.Parameters.Add(fileMainID);
//
cmd.CommandType = CommandType.StoredProcedure;
//
SqlDataReader dr = cmd.ExecuteReader();
//
if (dr.Read())
{
//
throw new Exception(" !");
}
//
dr.Close();
//
//SqlCommand cmd = con.CreateCommand();
// Sp ,insert into
cmd.CommandText = "XXXXXXXXXXXXXXX";
cmd.CommandType = CommandType.StoredProcedure;
//
//SqlParameter fileMainID = new SqlParameter("@FILE_MAIN_ID", SqlDbType.UniqueIdentifier, 16);
//fileMainID.Value = new Guid(file.FileMainID);
//
//
SqlParameter data = new SqlParameter("@DATA", SqlDbType.Image,file.Data.Length);
// Base64
data.Value = converter.GetBytes(System.Convert.ToBase64String(file.Data));
data.Value = file.Data;
//
SqlParameter length = new SqlParameter("@DATA_LENGTH", SqlDbType.Int, 4);
length.Value = file.Length;
//
//cmd.Parameters.Add(fileMainID);
cmd.Parameters.Add(data);
cmd.Parameters.Add(length);
//
int i = cmd.ExecuteNonQuery();
//
return i > 0;
}
catch
{
//
throw new Exception(" ");
}
finally
{
if (con != null)
{
//
con.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="fileMainID"> </param>
/// <returns> </returns>
public static TestWork.File GetNormalFileData(string fileMainID)
{
//
SqlConnection con = null;
try
{
//
con = CreateDataBase(fileMainID);
//
con.Open();
// Base64
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
// SqlCommand
SqlCommand cmd = con.CreateCommand();
// ,
cmd.CommandText = "XXXXXXXXXXXXXXX";
// :File_Main_ID
SqlParameter fd = new SqlParameter("@FILE_MAIN_ID", SqlDbType.UniqueIdentifier, 16);
fd.Value = new Guid(fileMainID);
cmd.Parameters.Add(fd);
//
cmd.CommandType = CommandType.StoredProcedure;
// ,
using (System.Data.IDataReader reader = cmd.ExecuteReader())
// File_Main_ID
if (reader.Read())
{
//
TestWork.File file = new TestWork.File();
//
file.FileMainID = fileMainID;
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
//
string da = converter.GetString((byte[])reader["Data"]);
// ,
file.Data = Convert.FromBase64String(da);
//
file.Length = (int)reader["Data_Length"];
//
file.FormatType = GetFileImageFormat(file.Data);
//
return file;
}
//
else
{
// null
return null;
}
}
catch
{
//
throw new Exception(" ");
}
finally
{
if (con != null)
{
//
con.Close();
}
}
}
//
private static TestWork.ImageFormat GetFileImageFormat(byte[] buffer)
{
//
if (buffer == null || buffer.Length < 2)
{
return TestWork.ImageFormat.Other;
}
//
string strFlag = buffer[0].ToString() + buffer[1].ToString();
//
switch (strFlag)
{
//JPG
case "255216":
return TestWork.ImageFormat.Jpg;
//GIF
case "7173":
return TestWork.ImageFormat.Gif;
//BMP
case "6677":
return TestWork.ImageFormat.Bmp;
//PNG
case "13780":
return TestWork.ImageFormat.Png;
//
default:
return TestWork.ImageFormat.Other;
}
}
/// <summary>
///
/// </summary>
/// <param name="file"> </param>
/// <returns></returns>
public void FileRecovery(TestWork.File file)
{
//
string path = @"c:\pics\";
string outfile = System.IO.Path.GetDirectoryName(path) + file.FileMainID + "." + file.FormatType;
FileStream fs = new FileStream(outfile, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(file.Data, 0, file.Data.Length);
fs.Close();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestWork
{
public enum ImageViewType { Normal, Thumbs };
public enum ImageFormat { Other, Jpg, Gif, Png, Bmp };
[Serializable]
//
public class File
{
//
public string FileMainID;
//
public ImageFormat FormatType;
//
public int Length = -1;
//
public byte[] Data = null;
}
}