データベース内のBlobフィールドの読み書きの正しい方法について.
6096 ワード
ちょっとした問題で吐き気がした.
写真を残す.
写真を残す.
1 class BolbHelper
2 {
3 /// <summary>
4 ///
5 /// </summary>
6 /// <param name="image"></param>
7 /// <returns></returns>
8 public static byte[] ImageToBinaryBytes(Image image)
9 {
10 byte[] result;
11 using (var ms = new MemoryStream())
12 {
13 image.Save(ms, ImageFormat.Bmp);
14 using(var br = new BinaryReader(ms))
15 {
16 ms.Seek(0, SeekOrigin.Begin);
17 result = br.ReadBytes((int)ms.Length);
18 br.Close();
19 }
20 ms.Close();
21 }
22 return result;
23 }
24
25 /// <summary>
26 ///
27 /// </summary>
28 /// <param name="data"></param>
29 /// <returns></returns>
30 public static Image ImageFromBinaryBytes(byte[] data)
31 {
32 Image result;
33 using (var ms = new MemoryStream())
34 {
35 using(var bw = new BinaryWriter(ms))
36 {
37 bw.Write(data);
38 result = Image.FromStream(ms);
39 bw.Close();
40 }
41 ms.Close();
42 }
43 return result;
44 }
45 }