Mohsin Arif

Mohsin Arif

  • 310
  • 6k
  • 138.6k

Delay response while converting Image?

Jun 23 2020 1:37 AM
Hi everyone I have an issue I am making API on .net core when I call data in this there is filed image which return image path, I want to convert it into byte64, when I converted then there is a lot of time to give response below is my code. 
 
I want a response in no time,  
 
 
  1. var data = await Task.Run(() => context.GetStoreProc.FromSqlRaw("EXECUTE dbo.Nstp_GetTeamBy_ID {0}", ManagerID).Select(x => new TeamManagerResponse  
  2.                {  
  3.                    DepartmentName = x.DepartmentName,  
  4.                    DesignationDesc = x.DesignationDesc,  
  5.                    EmailAdd = x.EmailAdd,  
  6.                    EmpImg = DataHelper.ConvertImageToByteArray(x.EmpImg),  // below is this method
  7.                    EmpName = x.EmpName  
  8.                }).ToListAsync());  
  9.                return data;  
 
  1. public static string ConvertImageToByteArray(string Image_Path)  
  2.         {  
  3.             string imgPathFinal = "";  
  4.             var imgPath = System.Reflection.Assembly.GetEntryAssembly().Location;  
  5.             int location = imgPath.LastIndexOf("\\bin");  
  6.             imgPathFinal = imgPath.Substring(0, location) + Image_Path;  
  7.             byte[] imageByteArray = null;  
  8.             if (File.Exists(imgPathFinal))  
  9.             {  
  10.                 //byte[] result = DataHelper.ConvertImageToByteArray(imgPathFinal);  
  11.                 FileStream fileStream = new FileStream(imgPathFinal, FileMode.Open, FileAccess.Read);  
  12.                 using (BinaryReader reader = new BinaryReader(fileStream))  
  13.                 {  
  14.                     imageByteArray = new byte[reader.BaseStream.Length];  
  15.                     for (int i = 0; i < reader.BaseStream.Length; i++)  
  16.                         imageByteArray[i] = reader.ReadByte();  
  17.                 }  
  18.                 return Convert.ToBase64String(imageByteArray, 0, imageByteArray.Length);  
  19.             }  
  20.             else  
  21.             {  
  22.                 return imgPathFinal;  
  23.             }  
  24.              
  25.         }  
 please help me 
 

Answers (3)