2014-10-15 4 views
0

으로 변환합니다. 이것은 테스트 계기와 관련이 있지만 C# 코드에서 더 도움이 필요합니다. 나는 내 질문에 대해 더 많이 설명했다. 나는 계기에 명령을 보내고 나는 계기에서 다만 자료를 받는다. 받은 데이터는 실제 형식 (바이너리)이고 나는 그냥 문자열 변수에 넣습니다. 여기에 내가 C#, gif 데이터를 이진 배열

http://i.stack.imgur.com/UcYqV.png

다음 내가 원하는 내가이 문자열이 바이트 배열을 변환 할 것입니다 .. 문자열 안에 무엇이 캡처. 내 목표는 내 PC에 PNG 파일을 만들기 때문에. 계기 설명서는이 반환 된 데이터가 gif 형식이지만 실제 유형을 반환했다. 문제 포인트는 바이트 배열로 변환 할 때 문제가 있다는 것입니다 ... 누구나 이런 종류의 experiance가 있습니까?
  /// below is just send command to instrument that i want " Returns an image of the display in .gif format " 
      my6705B.WriteString("hcop:sdump:data?", true); 
      string image_format = my6705B.ReadString(); 
      /// what's inside string image_format ??![i attached screenshot png file. this is what i received from instrument. (manual said this is Returns an image of the display in .gif format)][1] ![png file][2] 

http://i.stack.imgur.com/UcYqV.png

  /// now here i think i did something wrong, 
      /// the goal is i want change this return data to gif or png or image file. 
      /// any solution is ok for me 
      /// i just try that change this data to byte array and then try to change image file. 
     /// i think some error here because my code success to convert byte array then create image,,, error 
     //// i believe that i did something wrong in convert byte array..... 

      System.Text.UnicodeEncoding encode = new System.Text.UnicodeEncoding(); 
      byte[] byte_array22 = encode.GetBytes(image_format); 

      MemoryStream ms4 = new MemoryStream(byte_array22); 
      Image image = Image.FromStream(ms4);  //// error point 
      image.Save(@"C:\Users\Administrator\Desktop\imageTest.png"); 

난 내 설명 있다고 생각

코멘트입니다. 내 목표는 gif 데이터를 이미지 파일로 변환한다는 점을 다시 설명하겠습니다. instrument give us 디스플레이 이미지를 .gif 형식으로 반환합니다. 이 데이터를 문자열 배열로 받았습니다. < 이것이 정확한지 아닌지는 모르겠지만 지금은 문자열 배열을 넣은 다음>이 GIF 데이터로 png 또는 jpg 파일로 보내고 싶습니다.

조언을주십시오.

조셉 최

+0

16 비트 유니 코드가 아닌 ASCII 문자열을 읽어야합니다. 현재 encode.GetBytes (image_format)는 { '#', '\ 0', '0', '\ 0', 'G', '\ 0', 'I', '\ 0' '0', '8', '\ 0', '9', '\ 0'...} 다른 API를 사용해야하거나 Encoding.ASCII.GetBytes를 사용하여 image_format를 ASCII로 변환해야합니다.(). – nitrocaster

+0

내가 할 수있는 한 가지는 계측기 데이터 형식이 ASC이므로 REAL 형식으로 변경할 수 있습니다. 그럼 우리는 다른 방법으로 할 수 있습니까 ?? –

+0

필자는 요점은 악기가 오름차순 또는 실제 형식을 반환 할 수 있다고 생각합니다. 그래서 내가 한 일은 오름차순 형식이고 Encoding.ASCII.GetBytes()를 변경합니다. 그러나 MemoryStream ms4에 오류 = 새로운 MemoryStream (byte_array22); 이미지 이미지 = Image.FromStream (ms4, true, true); –

답변

1

시도하십시오 ImageFormat

image.Save(@"C:\Users\Administrator\Desktop\imageTest.png", System.Drawing.Imaging.ImageFormat.Png); 

처럼 업데이트 :

byte[] byte_array22 = Encoding.Unicode.GetBytes(image_format); 
MemoryStream ms4 = new MemoryStream(byte_array22); 
Image image = Image.FromStream(ms4, true, true); 
image.Save(@"C:\Users\Administrator\Desktop\imageTest.png",System.Drawing.Imaging.ImageFormat.Png); 
+0

하지만 코드가 오류가 발생하기 직전입니다. 이미지 이미지 = Image.FromStream (ms4); //// 에러 포인트 ==> 핵심 포인트는 리턴 문자열입니다. 어떻게 처리 할 수 ​​있습니까? –

+0

@Amarsinh Pol 코드를 사용해 보셨습니까? –

+0

예, 코드를 업데이트했지만 여전히 "Image image = Image"에서 동일한 오류가 발생했습니다.FromStream (ms4, true, true); "위의 주석 nitrocaster는 ASCII 문자열을 변환해야한다고 했으므로 지금이 시도는 –

0

흠 ... 여기를 Base64 형식으로 이미지를 변환하는 방법 중 몇 가지가 있습니다 (문자열), 그리고 뒤로.

private string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format) 
    { 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      // Convert Image to byte[] 
      image.Save(ms, format); 
      byte[] imageBytes = ms.ToArray(); 

      // Convert byte[] to Base64 String 
      string base64String = Convert.ToBase64String(imageBytes); 
      return base64String; 
     } 
    } 

    private Image Base64ToImage(string base64String) 
    { 
     // Convert Base64 String to byte[] 
     byte[] imageBytes = Convert.FromBase64String(base64String); 
     using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) 
     { 

      // Convert byte[] to Image 
      ms.Write(imageBytes, 0, imageBytes.Length); 
      Image image = Image.FromStream(ms, true); 
      return image; 
     } 
    } 

당신은 그것을 사용할 수 있고 그들이 당신에게 어울리는지를보기 위해 그들과 함께 놀 수 있습니다.

이미지를 가져 와서 단순히 문자열 배열로 덤핑하면 도움이 될 것입니다.

+0

고마워요 그리고 내가 무엇을 필요 Image6464Image .. 내가 무엇을 필요로하는 것은 리턴 문자열이 오름차순 또는 실제 수 있습니다, 그것은 내가 선택할 수있는 것을 의미합니다 .... 어떤 방법으로 사용해야합니까 ?? –

+0

죄송합니다. 문의하신 내용이 확실하지 않습니다 ... – Noctis