We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How can I get the byte[] that is the image when editing?

Hello,
In my application I store images in the database table using a Blob record.
I have tried the following code, but it's not having the expected results.

Within my CS code, I have the SfImageEditor object named `editor` I have edited the handler for the ImageSaving `editor_ImageSaving`

            Stream str = await editor.GetStream();
            byte[] bytes = new byte[(int)str.Length];
            str.WriteAsync(bytes, 0, (int)str.Length);
            viewModel.photo.Image = bytes;
            viewModel.photoRep.AddOrUpdate(viewModel.photo);

My viewmodel is as follows.

         public class ImageModel
         {
             public PhotosRepository photoRep => new PhotosRepository();
             public Photos photo { get; set; }
             public ImageSource Image { get; set; }
             public ImageModel(Photos incoming)
             {
                 photo = incoming;
                 Assembly assembly = typeof(ImageModel).GetTypeInfo().Assembly;
                 Image = ImageSource.FromStream(() => new MemoryStream(photo.Image));
             }
         }

and finally my cusom photos class....

          private int id;
          private int flags;
          private int recordType;
          private int recordCategory;
          private int parentrecord;
          private byte[] image;
          private string path;
          private string file;
          private ObservableCollection<Photos> oc = new ObservableCollection<Photos>();

          /// <summary>
          /// Gets or sets pK for the photo being stored in the DB.
          /// </summary>
          [PrimaryKey]
          [AutoIncrement]
          public int Id
          {
              get => id;
              set
             {
                     SetProperty(ref id, value, nameof(Id));
                     OnPropertyChanged(nameof(Id));
             }
          }
          ....... etc etc etc

I am trying to load the image into the editor, allow the user to rotate, crop, or mark up the image, then return to image bytes so I can save them in the database.

Using the code above, I can load the image, and edit it, when I step through the code it appears to be working, but the image becomes corrupt and cannot be re-read from the database. I've also seen that the image goes from being approx 32kb up to 80kb when returned from the GetStream() I suspect that this is not the image bytes, but perhaps the image+JSON?
Can you share a sample of how to get the image bytes? or what method I can use to return the newly modified image to the database?

Thanks!

2 Replies

JK Jesse Knott November 2, 2019 01:00 AM UTC

Im sorry, I spoke too soon... I had another bug in the render portion of my code. That was causing my problem.
The code I provided work's exactly as expected for saving the image to the DB.

One additional question however, Is there anyway to specify a filename to save to when using the editor.Save() function?

Thanks again!



VR Vignesh Ramesh Syncfusion Team November 4, 2019 02:08 PM UTC

Hi Jesse, 

Greetings from Syncfusion! 

Currently we don’t have support to specify the filename in Save method of SfImageEditor. So, we have considered this as feature request and please find the feedback task below here. 


Please cast your vote to make it count. We will prioritize the features every release based on the demands and we do not have immediate plan to implement this feature since we committed with already planned work. So, this feature will be available any of our upcoming release. 

If you have any more specification/suggestions to the feature request, you can add it as a comment in the portal. 

Regards, 
Vignesh. 


Loader.
Live Chat Icon For mobile
Up arrow icon