본문 바로가기
카테고리 없음

[c#] DevExpress - Binary 이미지 파일 Byte로 형변환 후 PictureEdit에 보여주기

by hankong 2022. 2. 14.
반응형

 

 

 

 

 

DB에 binary 형태로 저장해둔 이미지 파일을 가져와서 DataTable에 담아둔 상태다.

이 이미지 파일을 pictureEdit에 보여줄 수 있도록 byte 형태로 형 변환 해준 후 이미지를 보여줄 예정이다.

 

 

 

 foreach (Control ctl in layoutControl1.Controls)	// layoutControl에 붙인 모든 컨트롤을 불러온다.
 {
    if(ctl.GetType() == typeof(PictureEdit))	// 컨트롤의 타입이 PictureEdit일 때
    {
        foreach (DataColumn dc in m_dtList.Columns)	// 데이터 테이블의 컬럼을 가져온다.
        {
            if ("ImageFile" == dc.ColumnName.ToString()) // 컬럼명이 ImageFile일 때 (본인이 저장해둔 컬럼명으로 바꾼다)
            {
                foreach (DataRow dr in m_dtList.Rows)	// 데이터 테이블의 한 행씩
                {
                    if (dr["ImageFile"] != null)	// ImageFIle의 값이 null이 아닌 경우
                    {
                        string simage = (string)dr["ImageFile"];	// binary형식을 string으로 변환
                        byte[] byte64 = Convert.FromBase64String(simage);	// string을 byte형식으로 변환
                        MemoryStream ms = new MemoryStream(byte64);	// image 형태로 받이 위해 MemoryStream 이용
                        Image image = Image.FromStream(ms);	// image 형태로 변환

                        pictureEdit1.Image = (Image)image.Clone();	// pictureEdit에 image 복사
                        pictureEdit1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze;	// image SizeMode 변경
                    }
                }
            }
        }
    }
}

 

 

반응형

댓글