2014-06-13 1 views
-2

사진을 추가 할 응용 프로그램이 있으며이 파일은 자동으로 이진 파일로 변환되어 단일 파일로 저장됩니다. 어떻게 여러개의 이미지를 저장할 수 있으며 XML 파일의 시작과 각 refente 세트의 크기를 이미지 바이트로 유지합니다. 그러나 같은 이미지를 열 때마다 다른 바이트 세트를 선택할 때마다 여러 이미지가 바이트 단위로 나타납니다. 귀하의 도움으로이 문제를 해결하고 다른 이미지를 열 수 있기를 바랍니다.C# 이미지를 Byte []로 읽음

코드

// 이미지를

private void btAddImage_Click(object sender, RoutedEventArgs e) 
     { 
      OpenFileDialog op = new OpenFileDialog(); 
      op.Title = "Selecione a Imagem"; 
      op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + 
       "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + 
       "Portable Network Graphic (*.png)|*.png"; 

      if (op.ShowDialog() == true) 
      { 
       imgPatch.Source = new BitmapImage(new Uri(op.FileName)); 
       txtName.Focus(); 
      } 
     } 


//Convert Image 

private void btConvertImage_Click(object sender, RoutedEventArgs e) 
     { 
      if (String.IsNullOrEmpty(txtName.Text)) 
      { 
       txtName.Focus(); 
       MessageBox.Show("Preencha o Nome", "Error"); 
      } 

      else 
      { 
       save(ConvertFileToByteArray(op.FileName), txtName.Text); 
      } 
     } 

//Image to Byte Array 

private static byte[] ConvertFileToByteArray(String FilePath) 
     { 
      return File.ReadAllBytes(FilePath); 
     } 


//Save Binary File and XML File 

public void save(byte[] img, string nome) 
     { 
      FileStream f; 
      long ini, fin = img.Length; 

      if (!File.Exists("Escudos.bcf")) 
      { 
       f = new FileStream("Escudos.bcf", FileMode.Create); 
       ini = 0; 
      } 

      else 
      { 
       f = new FileStream("Escudos.bcf", FileMode.Append); 
       ini = f.Length + 1; 
       bin = new TestBinarySegment(); 

      } 

      bin.LoadAddSave("Escudos.xml", "Brasileiro", nome, ini, fin); 



      BinaryWriter b = new BinaryWriter(f); 
      b.Write(img); 
      b.Close(); 
      f.Dispose(); 



     } 


//Load Image from Byte 
private void btLoad_Click(object sender, RoutedEventArgs e) 
     { 
      getImageFromByte(); 
     } 


//Byte to Image 
public void getImageFromByte(int start, int length) 
     { 
      using (FileStream fs = new FileStream("Escudos.bcf", FileMode.Open)) 
      { 
       byte[] iba = new byte[fs.Length+1]; 
       fs.Read(iba, start, length); 
       Image image = new Image(); 
       image.Source = BitmapFrame.Create(fs, BitmapCreateOptions.None, 
              BitmapCacheOption.OnLoad); 

       imgPatch2.Source = image.Source; 
      } 
     } 
+1

컴파일도됩니까? 'getImageFromByte()'를 호출하지만 매개 변수를 전달하지 않습니다. –

+0

나는 거기에 넣는 것을 잊었지만, 신청서에는 – LucasGuitar

+0

이있다. 그런 다음 * 귀하의 질문을 편집하십시오. 질문에 오해의 소지가있을 때 사람들이 좋은 대답을하는 것은 정말로 어렵습니다. –

답변

1

을 추가 FileStream.Read 매개 변수는 사용자가 데이터를 배치 할 버퍼에 개시 오프셋 (offset) 오프셋. 스트림에서 오프셋에서 읽으려면 해당 위치에 Seek을 입력해야합니다. 나는 당신이 원하는 것이 아래에 있다고 생각하지만, 비록 그것이 읽으려고하는 이미지 너머의 파일에 더 많은 데이터가 있다면 무엇이 BitmapFrame.Create이 할 것인지 완전히 확신하지는 못합니다.

fs.Seek(start, SeekOrigin.Begin); 
Image image = new Image(); 
image.Source = BitmapFrame.Create(fs, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 

그러면 파일 포인터가 올바른 시작 위치로 이동합니다.

구조적으로 사용하지 않는 것으로 보아 바이트 배열을 제거했습니다. 그래도 문제가 해결되지 않으면

, 당신은 MemoryStream를 작성, 바이트 배열로 데이터를 읽을 수 있고, 그에서 비트 맵을 만듭니다 :

byte[] ida = new byte[length]; 
using (FileStream fs = File.OpenRead("Escudos.bcf")) 
{ 
    fs.Seek(start, SeekOrigin.Begin); 
    fs.Read(ida, 0, length); 
} 
using (MemoryStream ms = new MemoryStream(ida)) 
{ 
    Image image = new Image(); 
    image.Source = BitmapFrame.Create(fs, BitmapCreateOptions.None, 
     BitmapCacheOption.OnLoad); 
    imgPatch2.Source = image.Source; 
} 
+0

답장을 보내 주셔서 감사합니다. 명백하게 맞지만 코드를 두 가지 다른 방법으로 호출하려고했습니다. getImageFromByte (0,12161); 및 getImageFromByte (12162,19905); 이론 상으로는 두 개의 서로 다른 이미지를 열었지만 동일한 이미지를 엽니 다. – LucasGuitar

+0

@LucasGuitar : 두 예제를 모두 사용해 보셨습니까? 코드를 단일 단계로 처리하고 그 코드가 무엇인지 조사 했습니까? 또한 저장 코드가 파일을 올바르게 작성하고 있는지 확인하십시오. –

+0

오류가 바이너리 파일을 생성 할 시간이라고 생각합니다 – LucasGuitar

0

가장 간단한 방법은 사용 직렬화입니다 및 역 직렬화

[serializable] 
myImage 
{ 
    Byte[] ImageData; 
} 

Add each image to a List 
List<myImage> 

이 deseriali를 사용하여 목록에 직접 읽기 직렬화를 사용하여 파일에 직접 쓰기 클래스를 확인 zation

+0

나는 이해할 수 없었다. – LucasGuitar

관련 문제