develop

c# image to binary

파드파드 2024. 9. 20. 16:21
반응형

Q. 이미지 파일을 바이너리 텍스트로 변환 하고 싶다.

richTextBox에 보이게 하고, 텍스트 파일도 만들고 싶다.

 

참고.  binary to image

https://simplain.tistory.com/481

 

c# binary text to image

private void btnTxtToImg_Click(object sender, EventArgs e)         {             string base64String = rTxtBinayText.Text;             try             {                 byte[] imgBytes =

simplain.tistory.com

 

1.

 private void btnImgToTxt_Click(object sender, EventArgs e)
 {
     try
     {
         string base64Image = string.Empty;
         Image img = Image.FromFile(txtFullFileNameImg.Text);
         MemoryStream ms = new MemoryStream();
         img.Save(ms, img.RawFormat);
         byte[] imgBytes = ms.ToArray();
         base64Image = Convert.ToBase64String(imgBytes);
         rTxtBinayText.Text = base64Image;


         MessageBox.Show("Success!");

 

         string newFileName = $"{Path.GetDirectoryName(txtFullFileNameImg.Text)}\\{Path.GetFileNameWithoutExtension(txtFullFileNameImg.Text)}.txt" ;

         using (FileStream fs = new FileStream(newFileName, FileMode.Append, FileAccess.Write))
         {
             using (StreamWriter writer = new StreamWriter(fs))
             {
                 writer.WriteLine(base64Image);
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }

 

 

2. 끝

 

반응형