2011-08-01 2 views
0

파일을 업로드 할 때 파일을 업로드 할 때 임시 파일로 변환하여 임시 폴더에 저장해야합니다. 임시 파일을 읽으면 이미지의 너비가 넓어지고 나중에 데이터베이스가 저장됩니다. . 임시 파일을 이미지로 읽을 때 Out of Memory (메모리 부족)가 내 전체 코드 인 경우 오류가 발생합니다.파일 처리 C#

string img = System.Windows.Forms.Application.StartupPath [email protected]"\"+ path; 
//in path .png file is accessed. 
      string filename = Path.GetFileName(img); 
      string internetCache = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache); 
      string [email protected]"\tmp"; 
      if (!Directory.Exists(IEPath)) 
       Directory.CreateDirectory(IEPath); 


      FileInfo fi = new FileInfo(img); 
      FileStream fs = fi.OpenRead(); 
      //lResult = URLDownloadToFile(0, img.src, TempFolder & strFileName, 0, 0) 
      Stream stream = fs; 
      byte[] buffer = new byte[fs.Length]; 
      stream.Seek(273, SeekOrigin.Begin); 
      stream.Read(buffer, 0, (int)buffer.Length); 

      string tempfile = Path.Combine(IEPath, Math.Abs(filename.GetHashCode()) + ".tmp"); 
      File.WriteAllBytes(tempfile, buffer); 
      fs.Close(); 
      stream.Close(); 

      string _contentType = "application/octet-stream"; 
      FileType Type = FileType.png; 
      string MimeType = "image/png"; 
      FileStream fst = new FileStream(tempfile, FileMode.Open); 
      Image _image = System.Drawing.Image.FromStream(fst, true, true); 

예외가 메모리 부족으로 표시되는 마지막 줄. 해결책 찾기를 도와주세요.

+1

왜 처음 273 바이트를 건너 뛰고 있습니까? – spender

+0

파일 저장을 시도한 다음 저장된 파일을 열어 데이터가 올바른지 확인 했습니까? –

+0

273 방금 테스트를 위해 제공되었습니다. 파일을 열려고 시도하지 않았습니다. – user703526

답변

0

난 그냥 질문의 지시에 따라 : & 높이

  • 폭 받기 일부 파일

  • 복사 이미지로 읽어 임시 파일
  • 에 파일을 읽을

    1. var bytes = File.ReadAllBytes(@"C:\temp\duck.jpg"); 
      var temp = Path.GetTempFileName(); 
      File.WriteAllBytes(temp, bytes); 
      
      var img = Image.FromFile(temp); 
      Console.WriteLine ("width: {0}, height: {0}", img.Width, img.Height); 
      
  • 2

    내가 읽고있는 스트림이 손상되었음을 확인했습니다 (아마도 선행 273 바이트를 제거했기 때문일 수 있습니다).