// Varbinary => Image
public Image ConvertToImage(System.Data.Linq.Binary linqBinary)
{
byte[] picBinary = linqBinary.ToArray();
Image image = null;
using (MemoryStream ms = new MemoryStream(picBinary))
{
image = Image.FromStream(ms);
}
return image;
}
// Image => Byte[]
public byte[] ImageToByteArray(System.Drawing.Image image)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms,image.RawFormat);
return ms.ToArray();
}
}
// Save Image As File
Image image = ConvertToImage(photoBinary);
image.Save("output.jpg", ImageFormat.Jpeg);
// Insert Image As Varbinary To Database
byte[] imageBinary = ImageToByteArray(image);
using(SqlConnection connection = new SqlConnection(/* Connection String */))
using(SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
string query = "INSERT INTO Table(Content) VALUES(@Content)";
SqlParameter param = command.Parameters.Add("@Content", SqlDbType.VarBinary);
param.Value = imageBinary;
command.ExecuteNonQuery();
}
謝謝~ 雖然不知道是不是讀取權限的問題會出現GDI 泛型錯誤
後改寫為這樣就好了
public void ConvertToOCRImage(byte[] picBinary)
{
string imagePath = @"D:\source\NEWGIT\" + "\\ouputOCR.jpg";
Image image = null;
using (MemoryStream ms = new MemoryStream(picBinary))
{
image = Image.FromStream(ms);
image.Save(imagePath, ImageFormat.Jpeg);
using (System.IO.FileStream oFS = System.IO.File.Open(imagePath, FileMode.OpenOrCreate))
{
oFS.Write(ms.ToArray(), 0, ms.ToArray().Length);
}
}
}
```