imageとバイトストリームの相互変換
3915 ワード
//
public Byte[] SetImgToByte(string imgPath)
{
FileStream file = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
Byte[] byteData = new Byte[file.Length];
file.Read(byteData, 0, byteData.Length);
file.Close();
return byteData;
}
byte[] buf = (byte[])dataTable.Rows[0]["Img"];
MemoryStream ms = new MemoryStream(buf);
Image img = Image.FromStream(ms);
img.Save(filePath);
public void StreamToFile(Stream stream,string fileName)
{
// Stream byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
//
stream.Seek(0, SeekOrigin.Begin);
// byte[]
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
public Stream FileToStream(string fileName)
{
//
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// byte[] Stream
Stream stream = new MemoryStream(bytes);
return stream;
}