.NET でファイルの着信ストリームを取得し、それをイメージに変換してデータベースに保存する方法を知っている人はいますか? (これが可能かどうかはわかりませんが、確認したかったのです)。
編集:必ずしも画像ストリームではありません
ストリームを に読み込みbyte[]
、それをデータベースに保存する必要があります。
イメージ ストリームをバイト配列に変換し、バイナリまたは varbinary データ型でデータベースに格納できます。
画像をC#のバイト配列に転送する簡単な例を次に示します。
private static byte[] ReadImage(string p_postedImageFileName, string[] p_fileType)
{
bool isValidFileType = false;
try
{
FileInfo file = new FileInfo(p_postedImageFileName);
foreach (string strExtensionType in p_fileType)
{
if (strExtensionType == file.Extension)
{
isValidFileType = true;
break;
}
}
if (isValidFileType)
{
FileStream fs = new FileStream(p_postedImageFileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] image = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return image;
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion