markus / KCOM / Common / FileStreamToBase64.cs @ 69db7b26
이력 | 보기 | 이력해설 | 다운로드 (3.11 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.IO; |
6 |
using System.Windows.Media.Imaging; |
7 | |
8 |
namespace KCOM.Common.Converter |
9 |
{ |
10 |
public class FileStreamToBase64 |
11 |
{ |
12 |
/// <summary> |
13 |
/// BitMapSource To Base64 |
14 |
/// </summary> |
15 |
/// <param name="bitmap"></param> |
16 |
/// <returns></returns> |
17 |
public string ImageToBase64(BitmapSource bitmap) |
18 |
{ |
19 |
var encoder = new PngBitmapEncoder(); |
20 |
var frame = BitmapFrame.Create(bitmap); |
21 |
encoder.Frames.Add(frame); |
22 |
using (var stream = new MemoryStream()) |
23 |
{ |
24 |
encoder.Save(stream); |
25 |
return Convert.ToBase64String(stream.ToArray()); |
26 |
} |
27 |
} |
28 | |
29 |
/// <summary> |
30 |
/// Base64 To BitmapSource |
31 |
/// </summary> |
32 |
/// <param name="base64"></param> |
33 |
/// <returns></returns> |
34 |
//public BitmapSource Base64ToImage(string base64) |
35 |
//{ |
36 |
// byte[] bytes = Convert.FromBase64String(base64); |
37 |
// using (var stream = new MemoryStream(bytes)) |
38 |
// { |
39 |
// return BitmapFrame.Create(stream); |
40 |
// } |
41 |
//} |
42 |
public BitmapImage Base64ToImage(string base64) |
43 |
{ |
44 |
byte[] bytes = Convert.FromBase64String(base64); |
45 |
using (var stream = new MemoryStream(bytes)) |
46 |
{ |
47 | |
48 |
BitmapSource bitmap = BitmapFrame.Create(stream); |
49 | |
50 |
Stream bmp; |
51 |
BitmapImage bImg = new BitmapImage(); |
52 | |
53 |
using (bmp = new MemoryStream()) |
54 |
{ |
55 |
PngBitmapEncoder encoder = new PngBitmapEncoder(); |
56 | |
57 |
encoder.Frames.Add(BitmapFrame.Create(bitmap)); |
58 |
encoder.Save(bmp); |
59 | |
60 |
bmp.Position = 0; |
61 |
bImg.BeginInit(); |
62 |
bImg.StreamSource = bmp; |
63 |
bImg.EndInit(); |
64 |
} |
65 | |
66 |
System.Windows.Controls.Image image = new System.Windows.Controls.Image(); |
67 |
image.Source = bImg; |
68 | |
69 | |
70 | |
71 |
return bImg; |
72 | |
73 |
//return BitmapFrame.Create(stream); |
74 |
} |
75 |
} |
76 | |
77 |
//public BitmapImage BitmapSource(string base64) |
78 |
//{ |
79 |
// BitmapSource bitmap = Base64ToImage(base64); |
80 |
// Stream bmp; |
81 | |
82 |
// using (bmp = new MemoryStream()) |
83 |
// { |
84 |
// BitmapEncoder enc = new BmpBitmapEncoder(); |
85 |
// enc.Frames.Add(BitmapFrame.Create(bitmap)); |
86 |
// enc.Save(bmp); |
87 |
// } |
88 | |
89 | |
90 |
// //BitmapImage A = bitmap as BitmapImage; |
91 | |
92 |
// //PngBitmapEncoder encoder = new PngBitmapEncoder(); |
93 |
// //MemoryStream memoryStream = new MemoryStream(); |
94 |
|
95 | |
96 |
// //encoder.Frames.Add(BitmapFrame.Create(bitmap)); |
97 |
// //encoder.Save(memoryStream); |
98 | |
99 |
// //memoryStream.Position = 0; |
100 |
// //bImg.BeginInit(); |
101 |
// //bImg.StreamSource = memoryStream; |
102 |
// //bImg.EndInit(); |
103 | |
104 |
// //memoryStream.Close(); |
105 | |
106 |
// return bImg; |
107 |
//} |
108 | |
109 |
} |
110 |
} |