2012-11-14 4 views
0

.NET에서 파일의 수신 스트림을 가져 와서 데이터베이스로 저장하기 위해 이미지로 변환하는 방법을 아는 사람이 있습니까? (이것이 가능한지 확실하지 않지만 확인하고 싶다면)..NET에서 파일을 이미지로 변환하여 데이터베이스에 저장

편집 : 반드시 이미지 스트림

당신은 다음, byte[]로 스트림을 읽어 데이터베이스에 그것을 저장해야

답변

2

이미지 스트림을 바이트 배열로 변환하고 데이터베이스의 이진 또는 varbinary 데이터 형식으로 저장할 수 있습니다.

private static byte[] ReadImage(string p_postedImageFileName, string[] p_fileType) 
{ 
    bool isValidFileType = false; 
    try 
    { 
     FileInfo file = new FileInfo(p_postedImageFileName); 

     foreach (string strExtensionType in p_fileType) 
     { 
      if (strExtensionType == file.Extension) 
      { 
       isValidFileType = true; 
       break; 
      } 
     } 
     if (isValidFileType) 
     { 
      FileStream fs = new FileStream(p_postedImageFileName, FileMode.Open, FileAccess.Read); 

      BinaryReader br = new BinaryReader(fs); 

      byte[] image = br.ReadBytes((int)fs.Length); 

      br.Close(); 

      fs.Close(); 

      return image; 
     } 
     return null; 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 

#endregion 
: 여기
0

는 C#으로 바이트 배열로 이미지를 전송하는 간단한 예는
관련 문제