BitmapImage To String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if ((checkStream = openFileDialog.OpenFile()) != null)
                    {
                        BitmapImage src = new BitmapImage(new Uri(openFileDialog.FileName));
                        Profile_image.Source = src; //Image
 
                        image = ImageToStringConvert(src);
                    }
 
 public static string ImageToStringConvert(BitmapImage Bitmapimage)
        {
            //BitmapImage To Byte[] Data
            MemoryStream memStream = new MemoryStream();
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(Bitmapimage));
            encoder.Save(memStream);
 
            byte[] Image_BinaryData = memStream.GetBuffer();
 
            // BinaryData To StringData
            string ImageString = Convert.ToBase64String(Image_BinaryData);
 
            return ImageString;
        }


이미지 파일 자체를 선택하면 BitmapImage 객체로 받을 수 있습니다.

Image객체의 Source Property는 ImageSource 객체인데 BitmapImage 에서 자동 형변환 됩니다.

만약 실제 ImageToString을 구현하고 싶으면 Source값을 넣어준 image객체를 매개변수로 넣는 것으로 바꾸면 됩니다.



String To Image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static Image ConvertStringToImage(string str)
     {
         if (str == null || str=="")
             return null;
 
         Image img = new Image();
 
         byte[] jpgimages = Convert.FromBase64String(str);
         MemoryStream streams = new MemoryStream(jpgimages);
         JpegBitmapDecoder decoders = new JpegBitmapDecoder(streams, BitmapCreateOptions.None, BitmapCacheOption.None);
         BitmapSource imagesources = decoders.Frames[0];
         img.Source = imagesources;
 
         return img;
     }


'Programming with C# > C# 프로그래밍' 카테고리의 다른 글

[C# 프로그래밍] Drag and Drop  (3) 2016.01.31
[C# 프로그래밍] 예외 관련  (0) 2015.07.06
 
블로그 이미지

laboputer

소프트웨어를 전공하는 대학생

카테고리

전체보기 (24)
Programming with C# (15)
storage of informati.. (1)
Algorithm (1)
학교수업 (7)