i need to upload image as binary data and retrieve here is my code Web Api
[ResponseType(typeof(tblEmpPicture))]
public IHttpActionResult PosttblEmpPicture(tblEmpPicture tblEmpPicture)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.tblEmpPictures.Add(tblEmpPicture);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (tblEmpPictureExists(tblEmpPicture.intEmpCode))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = tblEmpPicture.intEmpCode }, tblEmpPicture);
}
MainPage.xaml
<Image x:Name="userImage" Source="{Binding Pic.vbrPicture, Mode=TwoWay}" Aspect="AspectFill" WidthRequest="85" HeightRequest="85" >
MainPage.cs
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", ":( No camera available.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg"
});
if (file == null)
return;
await DisplayAlert("File Location", file.Path, "OK");
userImage.Source = ImageSource.FromStream(() =>file.GetStream());
await ((MainViewModel)this.BindingContext).PutUserPicCommand();
MainVewModel
private tblEmpPicture _Pic = new tblEmpPicture();
public tblEmpPicture Pic
{
get { return _Pic; }
set
{
_Pic = value;
OnPropertChanged();
}
}
public async Task PutUserPicCommand()
{
try
{
IsBusy = true;
// Call your web service here
var employeesTaskServices = new TaskServices();
await employeesTaskServices.PutUserPicAsync(_Pic);
}
catch (Exception ex)
{
// Handle exception
}
finally
{
IsBusy = false;
}
}
Kindly i need to convert image to binary data and save it to sql server. I am able to save other data to sql server, but dont know how to convert image to binary and save into database and how to retrieve and show in image.