Question

I want to save an image to my Azure MobileService.

I have been looking around and found that you can use blob and azure storage. But instead of implementing this I would love if you could convert an image to string or stream that could be stored in a normal azure mobile service table.

I am creating images in my app as :

                Canvas found = null;
                try
                {
                    found = FindParentOfType<Canvas>(ViewInteractionCanvas.canvas);
                }
                catch (Exception)
                {
                    //MessageBox.Show(e.Message.ToString(), "ERROR", MessageBoxButton.OK);
                    found = ViewInteractionCanvas.canvas;
                }
                WriteableBitmap writeableBitmap = new WriteableBitmap(found, null);
                var imageBrush = new ImageBrush
                {
                    ImageSource = writeableBitmap,
                    Stretch = Stretch.None
                };
                writeableBitmap = null;
                GC.Collect();

                try
                {
                    FindChildCanvas(found, imageBrush);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message.ToString(), AppResources.ErrorSaving, MessageBoxButton.OK);
                    return false;
                }
                var fileStream = new MemoryStream();
                writeableBitmap = new WriteableBitmap(found, null);
                writeableBitmap.SaveJpeg(fileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 100, 100);
                fileStream.Seek(0, SeekOrigin.Begin);

                string tempJPEG = "My.jpg";
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (myIsolatedStorage.FileExists(tempJPEG))
                    {
                        myIsolatedStorage.DeleteFile(tempJPEG);
                    }
                    IsolatedStorageFileStream IsofileStream = myIsolatedStorage.CreateFile(tempJPEG);
                    /*
                    StreamResourceInfo sri = null;
                    Uri uri = new Uri(tempJPEG, UriKind.Relative);
                    sri = Application.GetResourceStream(uri);

                    BitmapImage bitmap = new BitmapImage();
                    bitmap.SetSource(sri.Stream);
                    WriteableBitmap wb = new WriteableBitmap(bitmap);
                    */
                    // Encode WriteableBitmap object to a JPEG stream.
                    //Extensions.SaveJpeg(wb, IsofileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

                    writeableBitmap.SaveJpeg(IsofileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 100, 100);
                    IsofileStream.Close();
                }
                dialogResult = MessageBox.Show(AppResources.ShieldCreator_SaveShield, AppResources.ShieldCreator_SaveShieldTitle, MessageBoxButton.OKCancel);
                if (dialogResult == MessageBoxResult.OK)
                {

                    MediaLibrary library = new MediaLibrary();
                    library.SavePictureToCameraRoll("picture", fileStream);

                }
                if (dialogResult == MessageBoxResult.Cancel)
                {
                }
                fileStream.Close();

I was thinking that I could send the filestream or something like that? But Have not succeeded in doing so. Maybe this is completely impossible. But just wanted to investigate the possibility instead of starting to learn a new concept.

Hope somebody can help.

Was it helpful?

Solution

By default, Mobile Services data is backed by SQL Database. As long as you can find a way to create a proper data type in your table, you'd be able to do this. Just keep in mind: SQL Database databases are limited to 150GB, which will be eaten up faster if storing content in the database instance vs, say, blob storage with a URL to that blob being stored in your SQL table (which also costs significantly less than SQL Database service).

OTHER TIPS

What you're talking about doing (storing the image data in your SQL database) is possible and not altogether difficult, but definitely not recommended. There are several issues including the size of the data and the inefficiency of storing data like this. At the end of the day though, if that's how you want to implement it, I have posts explaining how you can do so from an Android and an iOS app.:

Due to the data types supported by Mobile Services, you'd need to store the image data as strings (varchars in the database). Again, far from the most efficient but it'll work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top